fix(python): prevent stdout/stderr race on timeout
Add cancelled flag to prevent timed-out thread's finally block from overwriting stdout/stderr when a subsequent execution has already started capturing output.
This commit is contained in:
@@ -122,6 +122,7 @@ class PythonInstance:
|
|||||||
result_container: dict[str, Any] = {}
|
result_container: dict[str, Any] = {}
|
||||||
stdout_capture = io.StringIO()
|
stdout_capture = io.StringIO()
|
||||||
stderr_capture = io.StringIO()
|
stderr_capture = io.StringIO()
|
||||||
|
cancelled = threading.Event()
|
||||||
|
|
||||||
old_stdout, old_stderr = sys.stdout, sys.stderr
|
old_stdout, old_stderr = sys.stdout, sys.stderr
|
||||||
|
|
||||||
@@ -138,6 +139,7 @@ class PythonInstance:
|
|||||||
except Exception as e: # noqa: BLE001
|
except Exception as e: # noqa: BLE001
|
||||||
result_container["error"] = e
|
result_container["error"] = e
|
||||||
finally:
|
finally:
|
||||||
|
if not cancelled.is_set():
|
||||||
sys.stdout = old_stdout
|
sys.stdout = old_stdout
|
||||||
sys.stderr = old_stderr
|
sys.stderr = old_stderr
|
||||||
|
|
||||||
@@ -146,6 +148,8 @@ class PythonInstance:
|
|||||||
exec_thread.join(timeout=timeout)
|
exec_thread.join(timeout=timeout)
|
||||||
|
|
||||||
if exec_thread.is_alive():
|
if exec_thread.is_alive():
|
||||||
|
cancelled.set()
|
||||||
|
sys.stdout, sys.stderr = old_stdout, old_stderr
|
||||||
return self._handle_execution_error(
|
return self._handle_execution_error(
|
||||||
TimeoutError(f"Code execution timed out after {timeout} seconds")
|
TimeoutError(f"Code execution timed out after {timeout} seconds")
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user