dspy icon indicating copy to clipboard operation
dspy copied to clipboard

Add optional timeout to PythonInterpreter to fix infinite/long hangs

Open adaminsky opened this issue 1 month ago • 1 comments

Summary:

Adds a timeout parameter to dspy.primitives.python_interpreter.PythonInterpreter to cap how long sandboxed code is allowed to run. When a timeout is provided, stdout is read via a background thread; if no output arrives before the deadline, the Deno subprocess is killed and an InterpreterError is raised. This guards against code hanging indefinitely.

Changes:

  • Added timeout: float | None in the constructor and store it on the instance.
  • Wrap the stdout read in _read_output_line, which either blocks normally or enforces the timeout by joining a reader thread and, on timing out, terminating the sandbox process.
  • Ensure timed-out executions clean up the underlying Deno process so future runs can restart cleanly.

adaminsky avatar Nov 09 '25 03:11 adaminsky

@isaacbmiller Mind taking a look?

chenmoneygithub avatar Nov 13 '25 23:11 chenmoneygithub