Repository navigation
fix(sandbox): end the processes an exec leaves behind - #4097
Conversation
|
Independent review of PR #4097 at
MEDIUM: once the exec's own process has exited, a cancel or deadline no longer SIGKILLs an untagged process left in its group.
LOW
INFO
Checked and correct:
Repairs go through the manager. I'll re-review the next exact head. |
9f9b1fc to
1fc63f8
Compare
|
Independent re-review of PR #4097 at
Local probe [local]. An untracked
The PR's own tests [local]: Disposition of the previous review
MEDIUM 1: a rotation leaves an untagged process that ignores SIGTERM running in the exec's group. Regression against
MEDIUM 2: a second steer in the same turn ends the servers the first exec handed over [source, re-read by me; the platform timing is likely, not proven].
MEDIUM 3: stuck environment reads can take the whole fs pool in a default session, and the PR's own test fails there.
LOW / INFO [source]
Checked and correct
Repairs go through the manager. I'll re-review the next exact head. |
1fc63f8 to
c863a0c
Compare
Backend integration on 2e62a81 stopped at lane 49 of 217: "checkBellHintWire did not settle within 10 min", then the 30-minute step timeout (job 111128211899). The same hang, at the same lane, failed #4094 (run 37056656989) and #4097 (run 37086605668), neither of which changes a dependency. The integration run sets no SENTRY_DSN, and importing @sentry/node 10.48.0 or 10.54.0 without init subscribes to no diagnostics channel and patches nothing in node:http or fetch. No code change.
runnerd spawned each exec in a process group of its own but signalled that group only on a cancel or at the exec's deadline. When the command exited on its own, everything it had backgrounded (a `cmd &`, a `nohup` worker, a browser its driver started detached) kept running in a session that read idle, holding memory and pids until the container stopped, and a leftover holding the inherited stdout made every such exec wait out the 2 s drain grace. Each exec now carries its id in the environment (TALE_EXEC_ID). When its command exits, and on a cancel or at the deadline, runnerd sends SIGTERM to the exec's process group and to every process carrying the id, which also reaches a descendant in a group or session of its own, then SIGKILL five seconds later. A forced drain closes runnerd's ends of the pipes.
A dropped /attach left its subscriber on the exec and kept its request open, and with it one active operation, until the exec ended. The platform re-attaches every drain window, so a long agent turn piled up one stale subscriber and one phantom operation per window (40 reconnects read as 41 active operations) and runnerd's memory grew with each. The attach now follows its consumer: when the request or response closes, it leaves the exec's subscribers and settles at once.
tini -g forwards the container stop's SIGTERM to runnerd's process group only, and every exec runs in a group of its own, so on a Pod deletion a harness never got to write its transcript or a wrapper to remove what it staged before the kill; the comment claiming otherwise was wrong. runnerd now sends every live exec its SIGTERM before it exits. The egress sidecar idled as a bare `sleep infinity` when it found no proxy; as PID 1 that ignores SIGTERM, so every Pod deletion waited out the sidecar's whole grace period. It now idles under tini.
Python's default SIGTERM disposition ends the process without running any `finally`, so a cancelled or timed-out turn of these three wrappers left its staged settings in TMPDIR and its per-exec context file under ~/.gemini or ~/.qwen, which sit on the workspace volume for good. A cancel now raises an exception that unwinds through the cleanup, as tale-openclaw-run already did.
tale-steer-hook runs after every tool call of every Claude Code turn and spawned jq on the payload before checking whether anything was queued at all. The empty-queue answer now comes first and only drains the payload: about 20 ms instead of 265 ms per tool call in the runtime image.
Each process an exec leaves gets one SIGTERM: its group as a whole, and on their own only the tagged processes that left it. A second signal used to land inside the cleanup the first one started. Leftovers of an exec that ends while another exec of the session runs now wait for the session's last running exec, which may still use them (a dev server, a build daemon). A delayed signal reaches a group only while a process tagged with the exec is still in it, so a reused number is never signalled. The /proc scan is asynchronous and no longer blocks runnerd, and runnerd's own stop waits for its SIGTERMs to go out before it closes.
The gemini, qwen and pi wrappers ignore further TERM, INT and HUP once a first one started their cleanup, so a Ctrl-C after a group SIGTERM no longer leaves the staged settings or context file behind. One test now covers the cancel path of all three wrappers, after one and two signals.
The delayed SIGKILL of a cancel or a deadline reached the exec's group only while the /proc scan found a tagged process in it, so a leader with a scrubbed or unreadable environment that ignored SIGTERM never ended: the exec stayed live, held a slot and kept the session from going idle. While the exec's own process runs its pid holds the group's number, so the SIGKILL goes to the group then, as before. An exec whose leftovers wait for the session's last exec no longer has its pipes closed after the drain grace, which killed a waiting dev server at its next line of output; its output is read and dropped. A cancel or deadline during that drain ends the waiting leftovers at once instead of doing nothing. Tests now cover the daemon's stop of waiting leftovers and the prune past 256.
A cancel, the deadline and runnerd's own stop sent their SIGTERM only after a scan of /proc had read the environment of every process. That read takes the target's memory lock, so one process stuck under memory pressure held back the signal of every exec, and each later round tied up another thread of libuv's pool on the same process until runnerd's file routes stalled as well. A group known to be the exec's (a live exec, one whose leader just exited, the delayed SIGKILL while the leader runs) is now signalled before the scan. A scan answers with what it read after two seconds, and a process whose read did not come back is skipped until the read returns or its pid names a new process, so a stuck process holds one thread at most.
process.exit waits for every thread of libuv's pool, so a /proc read stuck on a process's memory lock kept runnerd's two-second hard exit from ever completing; the container lived on until the orchestrator killed it. No timer can step in, since the wait blocks the event loop itself. With a process-table read still out, runnerd now ends itself by SIGKILL instead, which waits for nothing; otherwise it exits with its code as before.
A round that comes after an exec's end signalled the exec's group only while a process in it still carried the exec's tag, as the group's number may be someone else's by then. So a leftover that stayed in the group without the tag (a child started with `env -i` or a cleared environment) was ended when its exec ended alone, but never once its leftovers had waited for another exec, and the prune dropped it as gone. For the same reason such a process that ignored SIGTERM outlived the SIGKILL of an exec that ended alone. An exec whose leftovers wait now records its group's processes, pid and start time read from stat alone, and a SIGTERM round passes on the ones it saw in the group to its SIGKILL. A later round counts the group as the exec's while one of them is still in it, and the prune keeps such a target.
A steer's restart cancels the running exec and starts a new one that continues the conversation in the same workspace. Since a cancel ends every process tagged with the exec, it also ended the servers the turn had started from its tool calls, which most harnesses run in a session of their own; the restarted turn then found them gone. POST /execs/:id/cancel?leftovers=keep now ends only the exec's own group (SIGTERM, then SIGKILL while its leader still runs) and holds what it left outside the group. The hold lifts once an exec started after it ends; the leftovers then end with the session's last running exec, as other leftovers do, or when runnerd stops. A plain cancel, a user's Stop, ends everything as before, and an older runnerd ignores the flag.
The spawner's exec cancel route now takes ?leftovers=keep and forwards it to runnerd's cancel, which then ends only the exec's own process group and holds what the exec left outside it for the exec that takes over. Without the flag a cancel ends everything the exec started, as before; an older spawner ignores the flag, so the platform's rotation falls back to a full cancel there.
A comment on a task whose agent cannot be steered live restarts the turn: the old exec is cancelled and a new one continues the conversation in the same workspace. That cancel ended everything the turn had started, including a dev server it was testing against from its shell tool, so the restarted turn found it gone and could take the site for down. The restart lane now cancels the old exec as a rotation (sessionCancelExec with keepLeftovers): the harness's own processes end, and what it started outside them stays for the restarted turn. A Stop, a crash and every other cancel still end everything.
The sessions guide now says how a later round proves an exec's group is still the exec's (a tagged process, or a recorded member still in it), why a known group is signalled before the process table is read and how a stuck read is bounded, and what a rotation's cancel (?leftovers=keep) ends and holds for the exec that takes over.
The exec process-lifetime row now names what the specs own beyond the earlier checks: signals that do not wait on a stuck /proc read, the scan's deadline and runnerd's SIGKILL exit, the group record that reaches an untagged leftover in the exec's group, and a steer restart's rotation cancel at runnerd, spawner and platform.
Each process-table scan read every environment itself, so scans that ran at once (a cancel's round, a deferral's group record, a prune) each tied up a thread of libuv's pool on the same stuck process before the first of them reached its deadline; four such reads leave runnerd without a thread for any file work. Concurrent scans now wait on the one read of a process already out. The listing of /proc is held to the scan's deadline as well.
After a steer's restart the platform's superseded drive still reaps the old exec it no longer owns, with a plain cancel. Arriving while the old exec drained (a child kept its pipes), that cancel ended what the rotation had just held for the restarted turn. A cancel or deadline of an exec already handed over now ends none of what it holds; a person's Stop goes to the exec that took over, whose end ends the held processes too.
A rotation's cancel holds what the exec left outside its group for the exec that takes over. A restart whose new exec never started kept that hold until some later exec of the session ended, keeping a dev server running in a session that otherwise had nothing to do. The hold now lifts after ten minutes, and with no exec running its leftovers end at once. The listing of the process table can be swapped out, so the bound on a listing that never answers has a test.
A process that both left an exec's process group and rewrote its environment (a double-forked `setsid env -i` server, a daemon that sets its own title) escaped cleanup: neither the group nor the TALE_EXEC_ID tag reached it, and it ran on in a session that read idle until the container stopped. runnerd now starts each exec under tale-exec-shim, a small C program built in its own image stage. It registers as a child subreaper, runs the command in a process group of its own, and keeps every process the command starts its descendant: orphans are reparented to the shim, not to tini. runnerd finds an exec's processes by walking down from the shim (reading /proc/<pid>/stat alone while every exec of a round has its shim, so no process's memory lock is taken) as well as by the tag. The shim reports on a status pipe (fd 3) the command's pid, how it ended (128 + N for signal N), why it could not be executed, or that the kernel refused the subreaper; the pipe, not the shim's own exit, decides the exit code, since the shim outlives the command for as long as anything it started runs. It ignores SIGTERM, SIGINT and SIGHUP, so a `pkill -f` aimed at the command cannot take the subreaper away, and blocks them across the fork so a group signal sent before the child restored its defaults still ends it. A cancel that comes before the shim named the group waits for it, so the group still gets one signal as a whole. Without the shim (a development host, a kernel that refuses the subreaper, logged once) execs run as before.
The runtime image conformance test boots runnerd under the daemon entrypoint, checks its startup line names /usr/local/bin/tale-exec-shim, and runs an exec that leaves a double-forked `setsid env -i` sleep behind: the process must be gone once the exec ends.
The exec-reaping ledger row now covers the subreaper shim: a process that left the exec's group, session and tag is found below the shim; the status pipe's exit, signal, spawn-error and refused-subreaper lines; the cancel that comes before the group is named; and where each is automated.
resolveExecShim: the path TALE_EXEC_SHIM names when it is executable, none when it is not, empty or off Linux, and the runtime image's own shim unset. Also gives the export the use knip asks for.
2afa0a4 to
2bc516e
Compare
|
Post-merge independent reevaluation of #4097 at My earlier findings (records 5963058115, 5963888996):
The subreaper shim (
New, LOW:
Next owner: the manager, for follow-ups: the second steer lifting the first one's hold, the no-shim rotation path, and the per-exec environ scan. |
Why
Sandbox sessions did not free what their work started. runnerd spawns each exec in a process group of its own but signalled that group only on a cancel or at the exec's deadline: when a command exited on its own, everything it had backgrounded — a
cmd &, anohupworker, a dev server, a browser its driver started detached — kept running in a session that read idle, holding memory and pids until the container stopped (pinned sessions: never). Reproduced in a live container:(sleep 1000 &); nohup sleep 999 &left both alive with/healthzreportingliveExecs: 0, and the exec waited out the 2 s drain grace.Part of the sandbox resource work; the spawner half is #4098 and the platform half #4099.
What changes
TALE_EXEC_IDin its environment. When its command exits, runnerd sends SIGTERM to what it left running — the exec's process group as a whole, and on its own each tagged process that moved to a group or session of its own (found through an async/procscan) — then SIGKILL 5 s later. Each process gets one signal, so a second SIGTERM never lands inside the cleanup the first one started. The delayed SIGKILL reaches the group while the exec's own process still runs (its pid holds the group's number, so it also ends a leader whose environment the scan cannot read), and afterwards only while a tagged process is still in it, so a reused number is never signalled.tini -gforwards SIGTERM only to runnerd's group; runnerd now passes it on to every live exec and to what exited execs left waiting, and closes only once those signals are sent. The egress sidecar's idlesleep infinityruns under tini so a Pod deletion no longer waits out the grace period.tale-openclaw-run, and ignore further signals once their cleanup started.jqon every tool call (265 → 20 ms per call in the image)./procis read; a scan answers within 2 s with what it read, and a process whose read never returns is skipped (concurrent scans share its read). A blocked read also no longer hangs runnerd's exit: with one still out, runnerd ends itself by SIGKILL.POST /execs/:id/cancel?leftovers=keepends only the exec's own group and holds what it left outside it for the exec that takes over; they end when that successor (or the session's last exec) ends, and a hold that sees no successor within ten minutes (a restart whose new exec never started) lifts and ends them. The spawner forwards the flag and the platform's steer restart lane — only that lane — sends it; a person's Stop still ends everything. Older spawners or runnerds ignore the flag and cancel in full.tale-exec-shim(daemon/exec-shim/tale-exec-shim.c, built in its own image stage), a child subreaper (PR_SET_CHILD_SUBREAPER) that runs the command in a process group of its own: whatever the command orphans is reparented to the shim, not to tini, so a double-forkedsetsid env -iserver (no group, no session, no tag) is still found — by walking down from the shim through/proc/<pid>/stat— and ended with the exec. While every exec of a round has its shim, the scan readsstatalone and takes no process's memory lock. The shim reports the command's pid, exit (128 + N for signal N), spawn error or a refused subreaper on a status pipe (fd 3); the pipe, not the shim's own exit, decides the exit code. It ignores SIGTERM/SIGINT/SIGHUP (apkill -faimed at the command must not take the subreaper away) and blocks them across the fork, so a group signal sent before the child restored its defaults still ends it. A cancel that arrives before the shim named the group waits for it and still signals the group once. Without the shim (a non-Linux dev host, a kernel that refuses the subreaper — logged once) execs run as before; runnerd's startup line names it (execShim=).stat); a later round treats the group as the exec's while one of them is still in it, so an untagged process in the group (a scrubbed environment, a rewritten title) is ended too.Behaviour change
A process an exec backgrounds no longer survives the session's last running exec. Nothing on the platform relies on that (no preview ports; checked), and session stops already ended such processes. Out of reach, as documented: the entrypoint's daemons and inner-dockerd containers, and — only where an exec runs without the shim — a process that both left the exec's group and replaced its environment.
Verification
bun test(daemon, 243 tests) green on Linux inside the runtime image, both with every exec under the shim and without it; the two host-only reds on macOS (file-ops stalled URL, main "partial exec request body") fail identically onmain.exec-shim.test.tsbuilds the shim from its source wherever Linux has a C compiler (CI's Unit job included; a failing build fails the run) and checks output and exit status, 128 + N for a signal, a spawn error, asetsid env -iescapee ended with its exec, while another exec runs and after a rotation, the control case that the same escapee survives without the shim, and a cancel before the group is named; stand-in shims check runnerd's reading of the status pipe on every platform.-Wall -Wextra -Werror, and the image conformance test now boots runnerd under the daemon entrypoint and checks that it names/usr/local/bin/tale-exec-shimand that a double-forkedsetsid env -iescapee is gone once its exec ends: green on this PR's image (91 checks, 0 failed).setsiddescendant (Linux), a dropped attach, the reaper's one-signal and no-stale-group rules, and each wrapper's cleanup after one and after two signals (the two-signal case failed 8 of 9 runs before the fix).activeOperationsat 1.