Skip to content

Commit 140e951

Browse files
committed
fix(sandbox): avoid killing reused process ids
1 parent c8ee5c5 commit 140e951

2 files changed

Lines changed: 57 additions & 3 deletions

File tree

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,12 @@ async def _terminate_process_group_and_reap_process(proc: asyncio.subprocess.Pro
110110
# The process group can contain descendants owned by another user when the
111111
# command was launched through sudo. Kill the direct child separately so
112112
# that a permission failure for one group member cannot leave the sudo
113-
# process running and holding the pipes open.
114-
with suppress(OSError):
115-
proc.kill()
113+
# process running and holding the pipes open. Only signal it while asyncio
114+
# still reports the tracked child as running; otherwise its PID may have
115+
# been reused by an unrelated process after the leader exited.
116+
if proc.returncode is None:
117+
with suppress(OSError):
118+
proc.kill()
116119
try:
117120
await asyncio.wait_for(proc.communicate(), timeout=_PROCESS_CLEANUP_TIMEOUT_SECONDS)
118121
except asyncio.TimeoutError:

‎tests/sandbox/test_unix_local.py‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ def close(self) -> None:
200200

201201
class _Process:
202202
pid = 123
203+
returncode = None
203204
_transport = _Transport()
204205

205206
async def communicate(self) -> tuple[bytes, bytes]:
@@ -246,6 +247,7 @@ def close(self) -> None:
246247

247248
class _Process:
248249
pid = 123
250+
returncode = None
249251
_transport = _Transport()
250252

251253
async def communicate(self) -> tuple[bytes, bytes]:
@@ -277,6 +279,54 @@ def _deny_group_signal(*_args: object) -> None:
277279
assert transport_closed
278280

279281

282+
@pytest.mark.asyncio
283+
async def test_unix_local_exec_cleanup_does_not_kill_exited_direct_child(
284+
monkeypatch: pytest.MonkeyPatch,
285+
) -> None:
286+
communicate_started = asyncio.Event()
287+
wait_called = asyncio.Event()
288+
direct_kill_called = False
289+
transport_closed = False
290+
291+
class _Transport:
292+
def close(self) -> None:
293+
nonlocal transport_closed
294+
transport_closed = True
295+
296+
class _Process:
297+
pid = 123
298+
returncode = 0
299+
_transport = _Transport()
300+
301+
async def communicate(self) -> tuple[bytes, bytes]:
302+
communicate_started.set()
303+
await asyncio.Event().wait()
304+
return b"", b""
305+
306+
def kill(self) -> None:
307+
nonlocal direct_kill_called
308+
direct_kill_called = True
309+
310+
async def wait(self) -> int:
311+
wait_called.set()
312+
return self.returncode
313+
314+
def _deny_group_signal(*_args: object) -> None:
315+
raise PermissionError("not allowed to signal a group member")
316+
317+
monkeypatch.setattr(unix_local_module.os, "killpg", _deny_group_signal)
318+
monkeypatch.setattr(unix_local_module, "_PROCESS_CLEANUP_TIMEOUT_SECONDS", 0.01)
319+
320+
process = cast(asyncio.subprocess.Process, _Process())
321+
task = asyncio.create_task(unix_local_module._terminate_process_group_and_reap(process))
322+
await communicate_started.wait()
323+
324+
await asyncio.wait_for(task, timeout=1)
325+
assert not direct_kill_called
326+
assert wait_called.is_set()
327+
assert transport_closed
328+
329+
280330
@pytest.mark.asyncio
281331
async def test_unix_local_exec_cleanup_bounds_unreapable_direct_child(
282332
monkeypatch: pytest.MonkeyPatch,
@@ -292,6 +342,7 @@ def close(self) -> None:
292342

293343
class _Process:
294344
pid = 123
345+
returncode = None
295346
_transport = _Transport()
296347

297348
async def communicate(self) -> tuple[bytes, bytes]:

0 commit comments

Comments
 (0)