Skip to content

Commit c8ee5c5

Browse files
committed
fix(sandbox): bound cancellation cleanup when signaling fails
1 parent 626f206 commit c8ee5c5

2 files changed

Lines changed: 103 additions & 1 deletion

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,20 @@
107107
async def _terminate_process_group_and_reap_process(proc: asyncio.subprocess.Process) -> None:
108108
with suppress(OSError):
109109
os.killpg(proc.pid, signal.SIGKILL)
110+
# The process group can contain descendants owned by another user when the
111+
# command was launched through sudo. Kill the direct child separately so
112+
# 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()
110116
try:
111117
await asyncio.wait_for(proc.communicate(), timeout=_PROCESS_CLEANUP_TIMEOUT_SECONDS)
112118
except asyncio.TimeoutError:
113119
# A descendant can escape the process group and keep the output pipes
114120
# open. The direct child still needs to be reaped, but waiting for the
115121
# escaped descendant would make cancellation unbounded.
116-
await proc.wait()
122+
with suppress(asyncio.TimeoutError):
123+
await asyncio.wait_for(proc.wait(), timeout=_PROCESS_CLEANUP_TIMEOUT_SECONDS)
117124
finally:
118125
# asyncio does not expose a public Process method for closing pipe
119126
# transports after communicate() is cancelled.

‎tests/sandbox/test_unix_local.py‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ async def communicate(self) -> tuple[bytes, bytes]:
207207
await asyncio.Event().wait()
208208
return b"", b""
209209

210+
def kill(self) -> None:
211+
pass
212+
210213
async def wait(self) -> int:
211214
wait_called.set()
212215
return -signal.SIGKILL
@@ -227,6 +230,98 @@ async def wait(self) -> int:
227230
assert transport_closed
228231

229232

233+
@pytest.mark.asyncio
234+
async def test_unix_local_exec_cleanup_kills_direct_child_when_group_signal_fails(
235+
monkeypatch: pytest.MonkeyPatch,
236+
) -> None:
237+
communicate_started = asyncio.Event()
238+
wait_called = asyncio.Event()
239+
direct_kill_called = False
240+
transport_closed = False
241+
242+
class _Transport:
243+
def close(self) -> None:
244+
nonlocal transport_closed
245+
transport_closed = True
246+
247+
class _Process:
248+
pid = 123
249+
_transport = _Transport()
250+
251+
async def communicate(self) -> tuple[bytes, bytes]:
252+
communicate_started.set()
253+
await asyncio.Event().wait()
254+
return b"", b""
255+
256+
def kill(self) -> None:
257+
nonlocal direct_kill_called
258+
direct_kill_called = True
259+
260+
async def wait(self) -> int:
261+
wait_called.set()
262+
return -signal.SIGKILL
263+
264+
def _deny_group_signal(*_args: object) -> None:
265+
raise PermissionError("not allowed to signal a group member")
266+
267+
monkeypatch.setattr(unix_local_module.os, "killpg", _deny_group_signal)
268+
monkeypatch.setattr(unix_local_module, "_PROCESS_CLEANUP_TIMEOUT_SECONDS", 0.01)
269+
270+
process = cast(asyncio.subprocess.Process, _Process())
271+
task = asyncio.create_task(unix_local_module._terminate_process_group_and_reap(process))
272+
await communicate_started.wait()
273+
274+
await asyncio.wait_for(task, timeout=1)
275+
assert direct_kill_called
276+
assert wait_called.is_set()
277+
assert transport_closed
278+
279+
280+
@pytest.mark.asyncio
281+
async def test_unix_local_exec_cleanup_bounds_unreapable_direct_child(
282+
monkeypatch: pytest.MonkeyPatch,
283+
) -> None:
284+
communicate_started = asyncio.Event()
285+
wait_started = asyncio.Event()
286+
transport_closed = False
287+
288+
class _Transport:
289+
def close(self) -> None:
290+
nonlocal transport_closed
291+
transport_closed = True
292+
293+
class _Process:
294+
pid = 123
295+
_transport = _Transport()
296+
297+
async def communicate(self) -> tuple[bytes, bytes]:
298+
communicate_started.set()
299+
await asyncio.Event().wait()
300+
return b"", b""
301+
302+
def kill(self) -> None:
303+
raise PermissionError("not allowed to signal the direct child")
304+
305+
async def wait(self) -> int:
306+
wait_started.set()
307+
await asyncio.Event().wait()
308+
return -signal.SIGKILL
309+
310+
def _deny_group_signal(*_args: object) -> None:
311+
raise PermissionError("not allowed to signal a group member")
312+
313+
monkeypatch.setattr(unix_local_module.os, "killpg", _deny_group_signal)
314+
monkeypatch.setattr(unix_local_module, "_PROCESS_CLEANUP_TIMEOUT_SECONDS", 0.01)
315+
316+
process = cast(asyncio.subprocess.Process, _Process())
317+
task = asyncio.create_task(unix_local_module._terminate_process_group_and_reap(process))
318+
await communicate_started.wait()
319+
320+
await asyncio.wait_for(task, timeout=1)
321+
assert wait_started.is_set()
322+
assert transport_closed
323+
324+
230325
@pytest.mark.asyncio
231326
async def test_unix_local_inherits_host_environment_by_default(
232327
tmp_path: Path,

0 commit comments

Comments
 (0)