fix(sandbox): reap killed subprocess on UnixLocal exec timeout#3208
Draft
adityasingh2400 wants to merge 2 commits intoopenai:mainfrom
Draft
fix(sandbox): reap killed subprocess on UnixLocal exec timeout#3208adityasingh2400 wants to merge 2 commits intoopenai:mainfrom
adityasingh2400 wants to merge 2 commits intoopenai:mainfrom
Conversation
When an exec timeout fires, the killed subprocess is never awaited, leaving the asyncio SubprocessTransport (pipes, child-watcher entry) attached to a returncode-still-None Process object until GC eventually collects it. The unconditional os.killpg also signals a pid that may already have exited and been reused, hitting an unrelated process group. Skip the kill when proc.returncode is already set, narrow the swallowed exception to ProcessLookupError, and await proc.wait() (bounded) so the transport tears down inside the failing call.
- raise asyncio.TimeoutError (not bare TimeoutError) in the wait_for fixture: on Python 3.10 these are distinct classes, so the source's except asyncio.TimeoutError clause was missing the bare one and the exception was upgraded to ExecTransportError. - import asyncio/os directly instead of reaching through unix_local_module.asyncio / .os, which mypy strict export rejects with attr-defined errors. - type captured[] as list[asyncio.subprocess.Process] so proc.returncode is statically known and the type: ignore comments are no longer needed.
Contributor
Author
|
Fixed CI - typecheck and 3.10 test pass locally now. Thanks for the heads up. |
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.
Summary
UnixLocalSandboxSession._exec_internalhandlesasyncio.TimeoutErrorby SIGKILL-ing the subprocess group, but never awaits the killedasyncio.subprocess.Process. Two issues fall out of that:proc.wait()is never awaited,proc.returncodestaysNoneand the underlyingSubprocessTransport(pipes + child-watcher entry) hangs around until GC eventually finds it. Repeated timeouts queue up live transports for the lifetime of the event loop.os.killpg(proc.pid, SIGKILL)runs unconditionally. If the process happened to exit betweenwait_forcancellation and the kill, the pid may already have been reused, and we'd sendSIGKILLto an unrelated process group. Theexcept Exception: passmasks this.The fix mirrors what
_terminate_pty_entryalready does for PTY entries (if process.returncode is None ... ProcessLookupError suppressed):proc.returncode is None(andproc.pid is not None).ProcessLookupErrorinstead ofException.await asyncio.wait_for(proc.wait(), timeout=5.0)so asyncio actually closes the transport beforeExecTimeoutErroris raised.Two regression tests cover both behaviors:
test_timeout_reaps_subprocess_so_returncode_is_setcaptures the asyncio Process used by_exec_internaland assertsreturncodeis populated after the timeout fires (fails on main:returncode is None).test_timeout_skips_killpg_for_already_exited_pidsimulates the "process exited just before the timeout handler" race and asserts nokillpgis issued (fails on main: a stalekillpgis recorded).Test plan
pytest tests/sandbox/test_unix_local.py -v(all 10 pass)pytest tests/sandbox/(765 passed, 19 skipped)ruff check+ruff format --checkpyright src/agents/sandbox/sandboxes/unix_local.py tests/sandbox/test_unix_local.pymain(bug repros), pass with the fix