Skip to content

Commit da45e04

Browse files
committed
fix(codex): bound subprocess cleanup
1 parent 8e7af59 commit da45e04

2 files changed

Lines changed: 91 additions & 2 deletions

File tree

‎src/agents/extensions/experimental/codex/exec.py‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
_MIN_SUBPROCESS_STREAM_LIMIT_BYTES = 64 * 1024
2222
_MAX_SUBPROCESS_STREAM_LIMIT_BYTES = 64 * 1024 * 1024
2323
_SUBPROCESS_DRAIN_CHUNK_BYTES = 64 * 1024
24+
_SUBPROCESS_CLEANUP_TIMEOUT_SECONDS = 1.0
2425

2526

2627
@dataclass(frozen=True)
@@ -147,6 +148,28 @@ async def _drain_remaining_stdout() -> None:
147148
while await process.stdout.read(_SUBPROCESS_DRAIN_CHUNK_BYTES):
148149
pass
149150

151+
async def _drain_stdout_and_wait() -> None:
152+
await _drain_remaining_stdout()
153+
await process.wait()
154+
155+
async def _cleanup_process() -> None:
156+
try:
157+
await asyncio.wait_for(
158+
_drain_stdout_and_wait(),
159+
timeout=_SUBPROCESS_CLEANUP_TIMEOUT_SECONDS,
160+
)
161+
except asyncio.TimeoutError:
162+
# A descendant may outlive the direct child while holding a pipe open.
163+
# Close the transport so cleanup does not wait indefinitely for EOF.
164+
transport = getattr(process, "_transport", None)
165+
if transport is not None:
166+
transport.close()
167+
with contextlib.suppress(asyncio.TimeoutError):
168+
await asyncio.wait_for(
169+
process.wait(),
170+
timeout=_SUBPROCESS_CLEANUP_TIMEOUT_SECONDS,
171+
)
172+
150173
stderr_task = asyncio.create_task(_drain_stderr())
151174
cancel_task: asyncio.Task[None] | None = None
152175
try:
@@ -215,8 +238,7 @@ async def _read_stdout_line() -> bytes:
215238
if process.returncode is None:
216239
with contextlib.suppress(ProcessLookupError):
217240
process.kill()
218-
await _drain_remaining_stdout()
219-
await process.wait()
241+
await _cleanup_process()
220242
if not stderr_task.done():
221243
stderr_task.cancel()
222244
with contextlib.suppress(asyncio.CancelledError):

‎tests/extensions/experiemental/codex/test_codex_exec_thread.py‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from __future__ import annotations
22

33
import asyncio
4+
import contextlib
45
import importlib
56
import inspect
67
import json
78
import os
9+
import signal
810
import sys
911
from dataclasses import fields
1012
from pathlib import Path
@@ -558,6 +560,71 @@ async def create_output_heavy_subprocess(
558560
await process.wait()
559561

560562

563+
@pytest.mark.asyncio
564+
async def test_codex_exec_run_close_is_bounded_when_descendant_holds_stdout(
565+
monkeypatch: pytest.MonkeyPatch,
566+
) -> None:
567+
original_create_subprocess_exec = asyncio.create_subprocess_exec
568+
process: asyncio.subprocess.Process | None = None
569+
helper_pid: int | None = None
570+
helper_code = "import time; time.sleep(10)"
571+
child_code = (
572+
"import os\n"
573+
"import subprocess\n"
574+
"import sys\n"
575+
f"helper = subprocess.Popen([sys.executable, '-c', {helper_code!r}], "
576+
"stdin=subprocess.DEVNULL, stdout=sys.stdout, stderr=subprocess.DEVNULL)\n"
577+
"os.write(1, f'ready {helper.pid}\\n'.encode())\n"
578+
"chunk = b'x' * 65536\n"
579+
"while True:\n"
580+
" os.write(1, chunk)\n"
581+
)
582+
583+
async def create_output_heavy_subprocess(
584+
*_args: Any, **kwargs: Any
585+
) -> asyncio.subprocess.Process:
586+
nonlocal process
587+
process = await original_create_subprocess_exec(sys.executable, "-c", child_code, **kwargs)
588+
return process
589+
590+
monkeypatch.setattr(
591+
exec_module.asyncio, "create_subprocess_exec", create_output_heavy_subprocess
592+
)
593+
monkeypatch.setattr(exec_module, "_SUBPROCESS_CLEANUP_TIMEOUT_SECONDS", 0.05, raising=False)
594+
595+
exec_client = exec_module.CodexExec(
596+
executable_path="unused",
597+
subprocess_stream_limit_bytes=exec_module._MIN_SUBPROCESS_STREAM_LIMIT_BYTES,
598+
)
599+
stream = exec_client.run(exec_module.CodexExecArgs(input="hello"))
600+
601+
try:
602+
ready_line = await asyncio.wait_for(anext(stream), timeout=5)
603+
helper_pid = int(ready_line.removeprefix("ready "))
604+
assert process is not None
605+
assert process.stdout is not None
606+
stdout = cast(Any, process.stdout)
607+
for _ in range(500):
608+
if stdout._paused:
609+
break
610+
await asyncio.sleep(0.01)
611+
assert stdout._paused
612+
613+
await asyncio.wait_for(stream.aclose(), timeout=1)
614+
assert process.returncode is not None
615+
finally:
616+
if helper_pid is not None:
617+
with contextlib.suppress(ProcessLookupError, PermissionError):
618+
os.kill(helper_pid, signal.SIGTERM)
619+
if process is not None:
620+
if process.returncode is None:
621+
process.kill()
622+
if process.stdout is not None:
623+
while await process.stdout.read(exec_module._SUBPROCESS_DRAIN_CHUNK_BYTES):
624+
pass
625+
await process.wait()
626+
627+
561628
@pytest.mark.asyncio
562629
async def test_codex_exec_run_raises_without_stdin(monkeypatch: pytest.MonkeyPatch) -> None:
563630
process = FakeProcess(stdout_lines=[], returncode=None, stdin_present=False)

0 commit comments

Comments
 (0)