fix: close leaked file handles in sandbox_program_utils.py#1311
Open
vominh1919 wants to merge 1 commit intoPrimeIntellect-ai:mainfrom
Open
fix: close leaked file handles in sandbox_program_utils.py#1311vominh1919 wants to merge 1 commit intoPrimeIntellect-ai:mainfrom
vominh1919 wants to merge 1 commit intoPrimeIntellect-ai:mainfrom
Conversation
Replace json.loads(open(PATH).read()) with json.loads(Path(PATH).read_text()) in 4 locations to prevent file descriptor leaks. The bare open() pattern creates file handles that are never explicitly closed, relying on garbage collection which is unreliable especially in CPython alternative implementations (PyPy, GraalPy). Using Path.read_text() is both more idiomatic and ensures proper resource cleanup.
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.
Problem
In
verifiers/v1/utils/sandbox_program_utils.py, there are 4 instances ofjson.loads(open(PATH).read())which create file handles that are never explicitly closed:json.loads(open(TOOL_DEFS_BY_PROTOCOL_PATH).read())json.loads(open(RUNNER_CONFIG_PATH).read())json.loads(open(TASK_PATH).read())json.loads(open(STATE_INPUT_PATH).read())These rely on garbage collection to close the file descriptors, which is unreliable — especially on alternative Python implementations (PyPy, GraalPy) where GC behavior differs from CPython.
Fix
Replace all 4 instances with
json.loads(Path(PATH).read_text()), which:pathlib.Path.read_text()— the idiomatic Python 3 way to read filesfrom pathlib import PathimportBefore vs After
Tests
This is a pure refactor with no behavioral change. All existing tests should continue to pass since
Path.read_text()returns the samestrthatopen().read()does.Note
Medium Risk
Touches the sandbox runner’s startup/config loading path; while intended as a no-op refactor, it changes how multiple JSON files are read and could break execution if imports/paths are incorrect.
Overview
Replaces several
json.loads(open(PATH).read())reads insandbox_program_utils.pywithPath(PATH).read_text()to ensure file handles are not leaked when loading tool defs, runner config, task, and state.Adds a
pathlib.Pathimport to support the new file-reading approach.Reviewed by Cursor Bugbot for commit cc76283. Bugbot is set up for automated code reviews on this repo. Configure here.