Skip to content

Commit fa631df

Browse files
committed
fix(sandbox): orphan process group keeper
1 parent 4d85bca commit fa631df

2 files changed

Lines changed: 76 additions & 17 deletions

File tree

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

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -105,26 +105,47 @@
105105

106106

107107
def _keep_process_group_alive(control_fd: int) -> None:
108-
"""Keep the spawned process group identity stable until the parent closes control_fd."""
109-
keeper_pid = os.fork()
110-
if keeper_pid == 0:
111-
for fd in (0, 1, 2):
112-
if fd != control_fd:
113-
with suppress(OSError):
114-
os.close(fd)
115-
max_fd = os.sysconf("SC_OPEN_MAX")
116-
os.closerange(3, control_fd)
117-
os.closerange(control_fd + 1, max_fd)
118-
while True:
119-
try:
120-
if not os.read(control_fd, 4096):
108+
"""Keep the process-group identity stable without becoming a child of the command."""
109+
intermediate_pid = os.fork()
110+
if intermediate_pid == 0:
111+
try:
112+
keeper_pid = os.fork()
113+
except BaseException:
114+
os._exit(1)
115+
116+
if keeper_pid == 0:
117+
for fd in (0, 1, 2):
118+
if fd != control_fd:
119+
with suppress(OSError):
120+
os.close(fd)
121+
max_fd = os.sysconf("SC_OPEN_MAX")
122+
os.closerange(3, control_fd)
123+
os.closerange(control_fd + 1, max_fd)
124+
while True:
125+
try:
126+
if not os.read(control_fd, 4096):
127+
break
128+
except InterruptedError:
129+
continue
130+
except OSError:
121131
break
122-
except InterruptedError:
123-
continue
124-
except OSError:
125-
break
132+
os._exit(0)
133+
134+
# The keeper is now reparented away from the command when this
135+
# intermediate process exits. It remains in the command's process
136+
# group, while the command cannot observe it with waitpid(-1, ...).
126137
os._exit(0)
127138

139+
while True:
140+
try:
141+
_, status = os.waitpid(intermediate_pid, 0)
142+
break
143+
except InterruptedError:
144+
continue
145+
146+
if not os.WIFEXITED(status) or os.WEXITSTATUS(status) != 0:
147+
raise ChildProcessError("failed to create the process-group keeper")
148+
128149
os.close(control_fd)
129150

130151

‎tests/sandbox/test_unix_local.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,44 @@ def is_alive(pid: int) -> bool:
249249
await asyncio.gather(task, return_exceptions=True)
250250

251251

252+
@pytest.mark.asyncio
253+
async def test_unix_local_exec_does_not_expose_keeper_to_child_waitpid(
254+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
255+
) -> None:
256+
# The keeper must not remain a child of the command. Otherwise a command
257+
# that waits for all of its own children can wait forever for the keeper's
258+
# cancellation control pipe to close.
259+
monkeypatch.setattr(unix_local_module.sys, "platform", "linux")
260+
workspace = tmp_path / "workspace"
261+
workspace.mkdir()
262+
command = (
263+
"import os\n"
264+
"child = os.fork()\n"
265+
"if child == 0:\n"
266+
" os._exit(0)\n"
267+
"while True:\n"
268+
" try:\n"
269+
" os.waitpid(-1, 0)\n"
270+
" except ChildProcessError:\n"
271+
" break\n"
272+
"os.write(1, b'done\\n')\n"
273+
)
274+
session = UnixLocalSandboxSession(
275+
state=UnixLocalSandboxSessionState(
276+
manifest=Manifest(root=str(workspace)),
277+
snapshot=NoopSnapshot(id="noop"),
278+
)
279+
)
280+
281+
result = await asyncio.wait_for(
282+
session._exec_internal(sys.executable, "-c", command),
283+
timeout=1,
284+
)
285+
286+
assert result.stdout == b"done\n"
287+
assert result.stderr == b""
288+
289+
252290
@pytest.mark.asyncio
253291
async def test_unix_local_exec_cleanup_survives_repeated_cancellation(
254292
monkeypatch: pytest.MonkeyPatch,

0 commit comments

Comments
 (0)