Skip to content

Commit 37d0344

Browse files
committed
fix(sandbox): close cancellation cleanup races
1 parent 4120b59 commit 37d0344

2 files changed

Lines changed: 167 additions & 4 deletions

File tree

‎src/agents/sandbox/sandboxes/unix_local.py‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ async def _terminate_process_group_and_reap_process(
516516
status_task: asyncio.Task[int] | None = None,
517517
control_write_fd: int | None = None,
518518
terminate_write_fd: int | None = None,
519+
signal_process_group: bool = True,
519520
) -> None:
520521
cleanup_deadline = asyncio.get_running_loop().time() + _PROCESS_CLEANUP_TIMEOUT_SECONDS
521522

@@ -550,7 +551,7 @@ def _remaining_cleanup_time() -> float:
550551
# Keep the keeper alive until after the group signal. Closing control first
551552
# would release the only stable owner of this group and allow its ID to be
552553
# reused before killpg runs.
553-
if group_id is not None:
554+
if signal_process_group and group_id is not None:
554555
with suppress(OSError):
555556
os.killpg(group_id, signal.SIGKILL)
556557
# Ask the supervisor to terminate the direct child and, when applicable,
@@ -571,9 +572,14 @@ def _remaining_cleanup_time() -> float:
571572
# own reaping protocol. The target PID is still safe to use while the
572573
# supervisor remains unreaped; ask its requested UID as well when the
573574
# command was launched through sudo.
574-
if target_user is not None and group_id is not None:
575+
if signal_process_group and target_user is not None and group_id is not None:
575576
await _kill_process_as_user(target_user, ("-KILL", f"-{group_id}"))
576-
if target_pid is not None and status_task is not None and not status_task.done():
577+
if (
578+
signal_process_group
579+
and target_pid is not None
580+
and status_task is not None
581+
and not status_task.done()
582+
):
577583
with suppress(OSError):
578584
os.kill(target_pid, signal.SIGKILL)
579585
if proc.returncode is None:
@@ -621,6 +627,7 @@ async def _terminate_process_group_and_reap(
621627
status_task: asyncio.Task[int] | None = None,
622628
control_write_fd: int | None = None,
623629
terminate_write_fd: int | None = None,
630+
signal_process_group: bool = True,
624631
) -> None:
625632
# Keep process cleanup in an owned task so a second cancellation cannot
626633
# interrupt the drain, direct-child reap, or transport close.
@@ -635,6 +642,7 @@ async def _terminate_process_group_and_reap(
635642
status_task=status_task,
636643
control_write_fd=control_write_fd,
637644
terminate_write_fd=terminate_write_fd,
645+
signal_process_group=signal_process_group,
638646
)
639647
)
640648
while not cleanup_task.done():
@@ -847,6 +855,7 @@ async def _exec_internal(
847855
process_group_id: int | None = None
848856
target_pid: int | None = None
849857
target_exit_code: int | None = None
858+
signal_process_group = True
850859
try:
851860
control_read_fd, control_write_fd = os.pipe()
852861
terminate_read_fd, terminate_write_fd = os.pipe()
@@ -888,10 +897,12 @@ async def _exec_internal(
888897

889898
assert proc is not None
890899
if proc_cancelled:
900+
process_info_task = asyncio.create_task(_read_process_group_info(info_read_fd))
891901
cleanup_control_fd, control_write_fd = control_write_fd, None
892902
cleanup_terminate_fd, terminate_write_fd = terminate_write_fd, None
893903
await _terminate_process_group_and_reap(
894904
proc,
905+
process_info_task=process_info_task,
895906
control_write_fd=cleanup_control_fd,
896907
terminate_write_fd=cleanup_terminate_fd,
897908
)
@@ -940,10 +951,14 @@ async def _exec_internal(
940951
else asyncio.wait_for(asyncio.shield(communication_task), communicate_timeout)
941952
)
942953
communication_task = None
954+
# The status protocol means the supervisor has already reaped
955+
# the target. From this point on, cleanup must release the
956+
# keeper without signaling its possibly reusable group ID.
943957
_close_fd_quietly(control_write_fd)
944958
control_write_fd = None
945959
_close_fd_quietly(terminate_write_fd)
946960
terminate_write_fd = None
961+
signal_process_group = False
947962
wait_timeout = (
948963
None
949964
if deadline is None
@@ -969,6 +984,7 @@ async def _exec_internal(
969984
status_task=status_task,
970985
control_write_fd=cleanup_control_fd,
971986
terminate_write_fd=cleanup_terminate_fd,
987+
signal_process_group=signal_process_group,
972988
)
973989
raise ExecTimeoutError(command=command, timeout_s=timeout, cause=e) from e
974990
except asyncio.CancelledError:
@@ -984,6 +1000,7 @@ async def _exec_internal(
9841000
status_task=status_task,
9851001
control_write_fd=cleanup_control_fd,
9861002
terminate_write_fd=cleanup_terminate_fd,
1003+
signal_process_group=signal_process_group,
9871004
)
9881005
raise
9891006
except Exception:
@@ -999,6 +1016,7 @@ async def _exec_internal(
9991016
status_task=status_task,
10001017
control_write_fd=cleanup_control_fd,
10011018
terminate_write_fd=cleanup_terminate_fd,
1019+
signal_process_group=signal_process_group,
10021020
)
10031021
raise
10041022
except ExecTimeoutError:
@@ -1010,6 +1028,8 @@ async def _exec_internal(
10101028
control_read_fd = None
10111029
_close_fd_quietly(control_write_fd)
10121030
control_write_fd = None
1031+
_close_fd_quietly(terminate_read_fd)
1032+
terminate_read_fd = None
10131033
_close_fd_quietly(terminate_write_fd)
10141034
terminate_write_fd = None
10151035
_close_fd_quietly(status_read_fd)

‎tests/sandbox/test_unix_local.py‎

Lines changed: 144 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import tarfile
1010
import threading
1111
import time
12+
from collections.abc import Coroutine
1213
from contextlib import suppress
1314
from pathlib import Path
1415
from types import SimpleNamespace
@@ -17,7 +18,7 @@
1718
import pytest
1819

1920
from agents.sandbox import SandboxPathGrant
20-
from agents.sandbox.errors import PtySessionNotFoundError
21+
from agents.sandbox.errors import ExecTransportError, PtySessionNotFoundError
2122
from agents.sandbox.manifest import Environment, Manifest
2223
from agents.sandbox.sandboxes import unix_local as unix_local_module
2324
from agents.sandbox.sandboxes.unix_local import (
@@ -371,6 +372,41 @@ def record_close(fd: int | None) -> None:
371372
assert len(closed_fds) == len(set(closed_fds))
372373

373374

375+
@pytest.mark.asyncio
376+
async def test_unix_local_exec_closes_all_fds_when_process_launch_fails(
377+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
378+
) -> None:
379+
workspace = tmp_path / "workspace"
380+
workspace.mkdir()
381+
closed_fds: list[int] = []
382+
383+
async def create_subprocess(*_args: object, **_kwargs: object) -> object:
384+
raise OSError("launch failed")
385+
386+
close_fd = unix_local_module._close_fd_quietly
387+
388+
def record_close(fd: int | None) -> None:
389+
if fd is not None:
390+
closed_fds.append(fd)
391+
close_fd(fd)
392+
393+
monkeypatch.setattr(unix_local_module.asyncio, "create_subprocess_exec", create_subprocess)
394+
monkeypatch.setattr(unix_local_module, "_close_fd_quietly", record_close)
395+
session = UnixLocalSandboxSession(
396+
state=UnixLocalSandboxSessionState(
397+
manifest=Manifest(root=str(workspace)),
398+
snapshot=NoopSnapshot(id="noop"),
399+
)
400+
)
401+
402+
with pytest.raises(ExecTransportError) as exc_info:
403+
await session._exec_internal("echo", "hello")
404+
405+
assert str(exc_info.value.__cause__) == "launch failed"
406+
assert len(closed_fds) == 8
407+
assert len(closed_fds) == len(set(closed_fds))
408+
409+
374410
@pytest.mark.parametrize(
375411
("command", "expected_user"),
376412
[
@@ -385,6 +421,113 @@ def test_unix_local_exec_extracts_target_user_only_from_command_prefix(
385421
assert unix_local_module._sudo_user_from_command(command) == expected_user
386422

387423

424+
@pytest.mark.asyncio
425+
async def test_unix_local_exec_discovers_process_group_before_startup_cancellation(
426+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
427+
) -> None:
428+
process = SimpleNamespace(pid=123, returncode=None)
429+
cleanup_kwargs: dict[str, object] = {}
430+
431+
async def settle(awaitable: object) -> tuple[object, bool]:
432+
cast(Coroutine[object, object, object], awaitable).close()
433+
return process, True
434+
435+
async def read_info(_fd: int) -> tuple[int, int]:
436+
return 456, 789
437+
438+
async def cleanup(_process: object, **kwargs: object) -> None:
439+
cleanup_kwargs.update(kwargs)
440+
info_task = cast(asyncio.Task[tuple[int, int]], kwargs["process_info_task"])
441+
assert await info_task == (456, 789)
442+
443+
monkeypatch.setattr(unix_local_module, "_settle_subprocess_awaitable", settle)
444+
monkeypatch.setattr(unix_local_module, "_read_process_group_info", read_info)
445+
monkeypatch.setattr(unix_local_module, "_terminate_process_group_and_reap", cleanup)
446+
session = UnixLocalSandboxSession(
447+
state=UnixLocalSandboxSessionState(
448+
manifest=Manifest(root=str(tmp_path)),
449+
snapshot=NoopSnapshot(id="noop"),
450+
)
451+
)
452+
453+
with pytest.raises(asyncio.CancelledError):
454+
await session._exec_internal("echo", "hello")
455+
456+
assert cleanup_kwargs["process_info_task"] is not None
457+
458+
459+
@pytest.mark.asyncio
460+
async def test_unix_local_exec_does_not_signal_released_group_after_target_completion(
461+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
462+
) -> None:
463+
wait_started = asyncio.Event()
464+
release_wait = asyncio.Event()
465+
killpg_calls: list[tuple[int, int]] = []
466+
467+
class _Transport:
468+
def close(self) -> None:
469+
return
470+
471+
class _Process:
472+
pid = 123
473+
returncode = None
474+
_transport = _Transport()
475+
wait_calls = 0
476+
477+
async def wait(self) -> int:
478+
self.wait_calls += 1
479+
if self.wait_calls == 1:
480+
wait_started.set()
481+
await release_wait.wait()
482+
self.returncode = 0
483+
return 0
484+
485+
async def communicate(self) -> tuple[bytes, bytes]:
486+
return b"", b""
487+
488+
def kill(self) -> None:
489+
raise AssertionError("completed target cleanup should not kill the supervisor")
490+
491+
process = _Process()
492+
493+
async def create_subprocess(*_args: object, **_kwargs: object) -> _Process:
494+
return process
495+
496+
async def read_status(_fd: int) -> int:
497+
return 0
498+
499+
async def read_info(_fd: int) -> tuple[int, int]:
500+
return 456, 789
501+
502+
async def read_output(_process: object) -> tuple[bytes, bytes]:
503+
return b"", b""
504+
505+
def record_killpg(group_id: int, signum: int) -> None:
506+
killpg_calls.append((group_id, signum))
507+
508+
monkeypatch.setattr(unix_local_module.asyncio, "create_subprocess_exec", create_subprocess)
509+
monkeypatch.setattr(unix_local_module, "_read_process_exit_code", read_status)
510+
monkeypatch.setattr(unix_local_module, "_read_process_group_info", read_info)
511+
monkeypatch.setattr(unix_local_module, "_read_process_output", read_output)
512+
monkeypatch.setattr(unix_local_module.os, "killpg", record_killpg)
513+
session = UnixLocalSandboxSession(
514+
state=UnixLocalSandboxSessionState(
515+
manifest=Manifest(root=str(tmp_path)),
516+
snapshot=NoopSnapshot(id="noop"),
517+
)
518+
)
519+
520+
task = asyncio.create_task(session._exec_internal("echo", "hello"))
521+
await wait_started.wait()
522+
task.cancel()
523+
release_wait.set()
524+
525+
with pytest.raises(asyncio.CancelledError):
526+
await task
527+
528+
assert killpg_calls == []
529+
530+
388531
@pytest.mark.asyncio
389532
async def test_unix_local_exec_keeps_command_out_of_host_session(
390533
tmp_path: Path, monkeypatch: pytest.MonkeyPatch

0 commit comments

Comments
 (0)