Skip to content

fix: close leaked file handles in sandbox_program_utils.py#1311

Open
vominh1919 wants to merge 1 commit intoPrimeIntellect-ai:mainfrom
vominh1919:fix/resource-leaks-sandbox-program-utils
Open

fix: close leaked file handles in sandbox_program_utils.py#1311
vominh1919 wants to merge 1 commit intoPrimeIntellect-ai:mainfrom
vominh1919:fix/resource-leaks-sandbox-program-utils

Conversation

@vominh1919
Copy link
Copy Markdown

@vominh1919 vominh1919 commented May 8, 2026

Problem

In verifiers/v1/utils/sandbox_program_utils.py, there are 4 instances of json.loads(open(PATH).read()) which create file handles that are never explicitly closed:

  • Line 372: json.loads(open(TOOL_DEFS_BY_PROTOCOL_PATH).read())
  • Line 570: json.loads(open(RUNNER_CONFIG_PATH).read())
  • Line 616: json.loads(open(TASK_PATH).read())
  • Line 617: 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:

  1. Uses pathlib.Path.read_text() — the idiomatic Python 3 way to read files
  2. Properly closes the file handle after reading (context manager internally)
  3. Also adds from pathlib import Path import

Before vs After

# Before (file handle leaked)
defs = json.loads(open(TOOL_DEFS_BY_PROTOCOL_PATH).read())

# After (properly closed)
defs = json.loads(Path(TOOL_DEFS_BY_PROTOCOL_PATH).read_text())

Tests

This is a pure refactor with no behavioral change. All existing tests should continue to pass since Path.read_text() returns the same str that open().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 in sandbox_program_utils.py with Path(PATH).read_text() to ensure file handles are not leaked when loading tool defs, runner config, task, and state.

Adds a pathlib.Path import 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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant