Skip to content

Commit c004807

Browse files
committed
fix(codex): terminate subprocess trees on cancellation
1 parent da45e04 commit c004807

2 files changed

Lines changed: 322 additions & 20 deletions

File tree

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

Lines changed: 110 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import os
66
import platform
77
import shutil
8+
import signal
9+
import subprocess
810
import sys
911
from collections.abc import AsyncGenerator
1012
from dataclasses import dataclass
@@ -128,6 +130,8 @@ async def run(self, args: CodexExecArgs) -> AsyncGenerator[str, None]:
128130
# default 64 KiB readline limit.
129131
limit=self._subprocess_stream_limit_bytes,
130132
env=env,
133+
# Give POSIX descendants a process-group boundary that this execution owns.
134+
start_new_session=os.name != "nt",
131135
)
132136

133137
stderr_chunks: list[bytes] = []
@@ -204,8 +208,6 @@ async def _read_stdout_line() -> bytes:
204208

205209
if args.signal is not None:
206210
args.signal.set()
207-
if process.returncode is None:
208-
process.terminate()
209211

210212
raise RuntimeError(
211213
f"Codex stream idle for {args.idle_timeout_seconds} seconds."
@@ -230,19 +232,44 @@ async def _read_stdout_line() -> bytes:
230232
f"Codex exec exited with code {process.returncode}: {stderr_text}"
231233
)
232234
finally:
233-
if cancel_task is not None:
234-
if not cancel_task.done():
235-
cancel_task.cancel()
236-
with contextlib.suppress(asyncio.CancelledError):
237-
await cancel_task
238-
if process.returncode is None:
239-
with contextlib.suppress(ProcessLookupError):
240-
process.kill()
241-
await _cleanup_process()
242-
if not stderr_task.done():
243-
stderr_task.cancel()
244-
with contextlib.suppress(asyncio.CancelledError):
245-
await stderr_task
235+
original_error = sys.exc_info()[1]
236+
237+
async def _cleanup_resources() -> None:
238+
cleanup_error: BaseException | None = None
239+
240+
if cancel_task is not None:
241+
try:
242+
await _cancel_and_wait(cancel_task)
243+
except BaseException as exc:
244+
cleanup_error = exc
245+
246+
if original_error is not None or process.returncode is None:
247+
try:
248+
await _terminate_process_tree(process)
249+
except BaseException as exc:
250+
if cleanup_error is None:
251+
cleanup_error = exc
252+
253+
try:
254+
await _cleanup_process()
255+
except BaseException as exc:
256+
if cleanup_error is None:
257+
cleanup_error = exc
258+
259+
try:
260+
await _cancel_and_wait(stderr_task)
261+
except BaseException as exc:
262+
if cleanup_error is None:
263+
cleanup_error = exc
264+
265+
if cleanup_error is not None:
266+
raise cleanup_error
267+
268+
cleanup_task = asyncio.create_task(_cleanup_resources())
269+
await _await_cleanup_task(
270+
cleanup_task,
271+
preserve_exception=original_error is not None,
272+
)
246273

247274
def _build_env(self, args: CodexExecArgs) -> dict[str, str]:
248275
# Respect env overrides when provided; otherwise copy from os.environ.
@@ -264,10 +291,74 @@ def _build_env(self, args: CodexExecArgs) -> dict[str, str]:
264291
return env
265292

266293

267-
async def _watch_signal(signal: asyncio.Event, process: asyncio.subprocess.Process) -> None:
268-
await signal.wait()
269-
if process.returncode is None:
270-
process.terminate()
294+
async def _cancel_and_wait(task: asyncio.Task[object]) -> None:
295+
if not task.done():
296+
task.cancel()
297+
with contextlib.suppress(asyncio.CancelledError):
298+
await task
299+
300+
301+
async def _await_cleanup_task(
302+
cleanup_task: asyncio.Task[None],
303+
*,
304+
preserve_exception: bool,
305+
) -> None:
306+
cancellation_error: asyncio.CancelledError | None = None
307+
308+
while not cleanup_task.done():
309+
try:
310+
await asyncio.shield(cleanup_task)
311+
except asyncio.CancelledError as exc:
312+
if cleanup_task.cancelled():
313+
break
314+
cancellation_error = exc
315+
except BaseException:
316+
break
317+
318+
cleanup_error: BaseException | None = None
319+
try:
320+
cleanup_task.result()
321+
except BaseException as exc:
322+
cleanup_error = exc
323+
324+
if preserve_exception:
325+
return
326+
if cancellation_error is not None:
327+
raise cancellation_error
328+
if cleanup_error is not None:
329+
raise cleanup_error
330+
331+
332+
def _terminate_windows_process_tree(pid: int) -> None:
333+
with contextlib.suppress(OSError, subprocess.TimeoutExpired):
334+
subprocess.run(
335+
["taskkill", "/PID", str(pid), "/T", "/F"],
336+
check=False,
337+
stdin=subprocess.DEVNULL,
338+
stdout=subprocess.DEVNULL,
339+
stderr=subprocess.DEVNULL,
340+
timeout=_SUBPROCESS_CLEANUP_TIMEOUT_SECONDS,
341+
)
342+
343+
344+
async def _terminate_process_tree(process: asyncio.subprocess.Process) -> None:
345+
pid = getattr(process, "pid", None)
346+
try:
347+
if pid is not None:
348+
if os.name == "nt":
349+
await asyncio.to_thread(_terminate_windows_process_tree, pid)
350+
else:
351+
with contextlib.suppress(ProcessLookupError):
352+
os.killpg(pid, signal.SIGKILL)
353+
finally:
354+
if process.returncode is None:
355+
with contextlib.suppress(ProcessLookupError):
356+
process.kill()
357+
358+
359+
async def _watch_signal(signal_event: asyncio.Event, process: asyncio.subprocess.Process) -> None:
360+
await signal_event.wait()
361+
await _terminate_process_tree(process)
271362

272363

273364
def _platform_target_triple() -> str:

0 commit comments

Comments
 (0)