fix: use context managers for file reads in sandbox_program_utils.py#1332
Open
vominh1919 wants to merge 1 commit into
Open
fix: use context managers for file reads in sandbox_program_utils.py#1332vominh1919 wants to merge 1 commit into
vominh1919 wants to merge 1 commit into
Conversation
Four instances of open().read() without a context manager, which leaks file descriptors. While these are short-lived sandbox processes, proper resource management is still important: - load_tool_defs(): json.loads(open(PATH).read()) → with open() as f: json.load(f) - run_base(): json.loads(open(RUNNER_CONFIG_PATH).read()) → with open() as f - main(): json.loads(open(TASK_PATH).read()) → with open() as f - main(): json.loads(open(STATE_INPUT_PATH).read()) → with open() as f
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
Four instances of
open().read()without a context manager inverifiers/v1/utils/sandbox_program_utils.py. Each call opens a file, reads its contents, but never explicitly closes the file descriptor. While Python's GC will eventually close them, this is a resource leak — especially if the code is called repeatedly or under memory pressure.Fix
Replace with
with open() as f: json.load(f)context managers, which guarantee the file is closed even ifjson.load()raises:Tests
ast.parse()Note
Low Risk
Low risk: behavior is unchanged aside from ensuring file descriptors are always closed during JSON reads. Impact is limited to sandbox runner initialization/config loading paths.
Overview
Ensures the sandbox runner code in
verifiers/v1/utils/sandbox_program_utils.pyreads JSON inputs (tool defs,runner config,task, andstate) usingwith open(...)context managers andjson.load()instead ofopen().read()+json.loads().This eliminates potential file-descriptor leaks during repeated sandbox executions without changing the parsed data or control flow.
Reviewed by Cursor Bugbot for commit 4be5213. Bugbot is set up for automated code reviews on this repo. Configure here.