You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Codeflash CLI currently generates some test files for establishing code behavior and performance. In certain cases, these files remain in the filesystem which shows up in git status. We should be deleting all such files no matter how the code exited.
The code checks for the trace file with exists() before calling unlink(), which can lead to a TOCTOU race condition. Consider using Path.unlink(missing_ok=True) or catching FileNotFoundError directly.
trace_file=Path(self.args.benchmarks_root) /"benchmarks.trace"iftrace_file.exists():
#Trace file not deleted all the timestrace_file.unlink()
If an exception is thrown before unlink(), the trace file might not be deleted. It may be better to place cleanup in a finally block to guarantee deletion in all exit scenarios.
try:
instrument_codeflash_trace_decorator(file_to_funcs_to_optimize)
trace_file=Path(self.args.benchmarks_root) /"benchmarks.trace"iftrace_file.exists():
#Trace file not deleted all the timestrace_file.unlink()
The newly added comment #Trace file not deleted all the times lacks a leading space and could be clarified. Follow the project's comment style and improve the grammar for clarity.
Switch to tempfile.TemporaryDirectory to ensure the temporary directory is automatically cleaned up even if exceptions occur. Assign the manager to an instance attribute so it remains alive for the needed scope.
Why: Switching to TemporaryDirectory ensures the temp dir is cleaned up automatically, preventing resource leaks while maintaining the same behavior.
Medium
Possible issue
Make trace_file deletion robust
Replace the manual existence check and unlink with a single unlink(missing_ok=True) call wrapped in a try/except to avoid race conditions and suppress any OSError if deletion fails.
Why: Using unlink(missing_ok=True) and catching OSError removes the race condition and safely ignores deletion failures, improving robustness without major functional changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
User description
Codeflash CLI currently generates some test files for establishing code behavior and performance. In certain cases, these files remain in the filesystem which shows up in
git status. We should be deleting all such files no matter how the code exited.PR Type
Documentation
Description
Changes walkthrough 📝
optimizer.py
Document trace file cleanupcodeflash/optimization/optimizer.py