fix: use context manager for file reads in code_projects_api.py - #93
Open
nwalio wants to merge 1 commit into
Open
fix: use context manager for file reads in code_projects_api.py#93nwalio wants to merge 1 commit into
nwalio wants to merge 1 commit into
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Background
In
backend/app/modules/code/code_projects_api.py, two file reads use the patternjson.loads(open(path, encoding="utf-8").read()), which opens a file handle without explicitly closing it. The handle is eventually collected by the garbage collector, but ifjson.loadsraises before the handle is released, it leaks. Under PyPy or high-concurrency workloads, these accumulate.Changes
Replaced both occurrences with the
with open(...) as f:context manager pattern:get_pipeline_results): readingpipeline_results.jsonauto_fix_pipeline): reading previous pipeline resultsEach change adds one line (the
withstatement) and re-indents thejson.loadscall inside the block. No behavioral change — the same data is read and parsed.Before/After
Notes
The existing
encoding="utf-8"parameter is preserved — this was already correct. The only change is ensuring the file handle is deterministically closed via the context manager protocol rather than relying on GC timing.Verification
py_compileconfirms valid Python syntaxtests/test_pr_06_code_projects_files.pyruns two assertions:open(...).read()patterns remain in the moduleresults_pathopens exist (2 reads + 1 write)