Skip to content

fix(sandbox): end stalled execs and refuse new ones in a session out of memory - #4627

Merged
yannickmonney merged 6 commits into
mainfrom
fix/sandbox-run-guards
Oct 9, 2026
Merged

yannickmonney merged 6 commits into
mainfrom
fix/sandbox-run-guards

Conversation

@yannickmonney

Copy link
Copy Markdown
Contributor

What

Two runtime guards for agent runs that run away inside one sandbox session. The owner approved both.

Stall detection. runnerd watches every running exec. An exec that publishes no output for the stall window and whose processes use under 1% of one CPU over that same window gets ended through the cancel path (SIGTERM, then SIGKILL after the grace). Its exit event carries failure: 'EXEC_STALLED'.

  • CPU is counted per exec from /proc/<pid>/stat: the subreaper shim and its descendants, or else the exec's process group. The cgroup is not used because every exec, runnerd and the inner dockerd share it, so a busy sibling would hide a hung exec.
  • CPU used by the inner Docker engine's containers counts for every live exec, so a client waiting on a silent container does not look idle.
  • An unreadable process table judges nothing.
  • The window is SANDBOX_EXEC_STALL_MINUTES (default 45, 0 off), passed to runnerd as TALE_EXEC_STALL_MS on Docker and Kubernetes.

On the spawner, the result is failed with errorCode: EXEC_STALLED. On the platform, the run fails with turn_stalled, the run detail shows a localized reason (EN/DE/FR), and the run is not retried automatically.

Memory backstop for new execs. Before starting an exec, runnerd reads the session cgroup's memory.current, memory.max and inactive_file. If the working set has reached 90% of the limit, it answers 429 session_memory_busy with retry-after, and nothing starts. Running execs are never touched, and a session without a limit or without readable files is never refused.

  • The spawner now opens the runnerd exec before answering, so it can forward this refusal as a real 429 instead of a failed stream.
  • The platform raises SessionMemoryBusyError, a SessionExecLimitError. It restarts the exec after the hint within the reconnect budget; past that, the lane waits for room as it does for any refused start.

Not in this PR. OOM attribution (memory.events oom_kill and State.OOMKilled mapped to resource_exhausted) follows separately.

Docs

SANDBOX_EXEC_STALL_MINUTES is in .env.example, the env reference (EN/DE/FR) and the sandbox README. The runtime README describes both guards. Two coverage rows are in reference/automation/automations.md.

Tests

  • Runtime: exec-stall.test.ts, exec-manager.stall.test.ts, session-memory.test.ts, and a main.test.ts case for the 429.
  • Spawner: runnerd-client, runnerd-protocol, session-routes, config, docker-session-args, k8s-session-pod-spec.
  • Platform: session_client.memory.test.ts, the *.sandbox_ended suites for the automation, task and chat hosts, run_park_reason, task-run-failure.
  • The legacy agent-flow guard pins retryResumePrompt by section now. The pinned function is unchanged.

Run locally on macOS: the platform type-check is clean, and every touched suite passes except the known macOS-only failures, which are identical on main (runnerd-client staging ×3, daemon main.test.ts ×9).

An agent CLI that hangs on a socket or on input nobody sends holds its
session, its memory and one of the four live-exec places for as long as
its drainer keeps attaching, and every attach re-arms the orphan
deadline, so nothing ever ends it.

runnerd now watches every live exec. One that publishes no output for
TALE_EXEC_STALL_MS (default 45 min, 0 turns it off) and whose processes
used under 1% of one CPU over that window (27 CPU-seconds in 45 min) is
ended through the cancel path, SIGTERM then SIGKILL after the 5 s grace,
and its exit event carries failure: EXEC_STALLED.

CPU comes from /proc/<pid>/stat (utime+stime+cutime+cstime) of the exec's
shim and its descendants, not from the session cgroup: the cgroup is
shared with runnerd, the other execs and the inner dockerd, so a busy
sibling would hide a hung exec. The inner engine's /docker cgroup
usage_usec counts for every live exec, so a client waiting on a silent
container is never read as idle. The table is sampled at most once a
minute (window/10), stat only, never environ; an unreadable table judges
nothing. Both runnerd-protocol copies gain the optional exit field and
the mirror test pins the exit event shape.
The spawner passes SANDBOX_EXEC_STALL_MINUTES (default 45, 0 turns the
watch off, whole minutes up to a day) to every session container as
TALE_EXEC_STALL_MS, on Docker and Kubernetes alike, so operators size the
window runnerd ends hung execs at.

An exit event runnerd marks failure: EXEC_STALLED becomes a failed result
with the new EXEC_STALLED error code on both the exec and the attach
stream, even when the command caught the SIGTERM and exited 0, so the
platform can tell a hang from a crash instead of reading it as an
ordinary non-zero exit.

Documented in .env.example, the environment reference (EN/DE/FR) and the
spawner and runtime READMEs.
A stalled exec (EXEC_STALLED: no output and under 1% of one CPU for the
sandbox's 45-minute window) used to read as an ordinary crash: the task
lane settled it harness_error and retried at once, and a resume window
that hung was even relaunched fresh on the spot, so a hang that repeats
held a worker for another 45 minutes per attempt.

sandboxEndOf() names the sandbox's own end beside classifyHarnessEnd,
which stays byte-identical because the released legacy agent flow
still reads it. Task runs settle turn_stalled with a new localized
failure class ("the agent stopped responding", EN/DE/FR) and no
automatic retry; automation turns settle turn_stalled, never re-kicked,
reported on the wire as turn_crashed so the public Run.failureCode enum
does not move. The stall reason keeps the harness's last stderr line.

agent_retry.ts was a whole-file pin of the legacy agent flow; it is now
a sections pin of retryResumePrompt, the only declaration that flow
reuses (unchanged, sha256 0366a702…).
A session is one cgroup with a hard limit (8 GiB with Docker inside by
default). Starting another exec into a session at its limit makes the
kernel's OOM killer end one of the running processes, most often the
agent itself, the largest one, so the work that was going well dies for
the work that was about to start.

Before it starts an exec, runnerd now reads the session cgroup's
memory.current, memory.max and memory.stat. When the working set
(current minus inactive_file, as Docker and Kubernetes count it, so a
session full of droppable file cache is not refused) has reached
TALE_EXEC_ADMISSION_MEMORY_PERCENT of the limit (default 90), POST
/execs answers 429 session_memory_busy / SESSION_MEMORY_BUSY with a
five-second retry-after header. Running execs are never touched; a
session without a limit or a readable cgroup is never refused. Both
protocol copies name the refusal; the mirror test keeps them equal.
The spawner answered every exec with an SSE stream before runnerd had
seen it, so runnerd's new session_memory_busy refusal could only have
reached the platform as a failed result, which it reads as a crash.

handleExec now starts the exec on runnerd before it answers: a memory
refusal becomes HTTP 429 { error: session_memory_busy, code:
SESSION_MEMORY_BUSY } with runnerd's retry-after, the exec id is
unregistered and free for the retry, and any other failure is still
reported on the stream as before. runnerdOpenExec splits accepting an
exec from pumping its NDJSON; runnerdExec keeps its shape on top of it.
Every session container gets TALE_EXEC_ADMISSION_MEMORY_PERCENT=90 on
Docker and Kubernetes.

The replacement test that held the old peer's POST open now starts that
exec without awaiting its answer, since the answer waits on runnerd.
The spawner now answers an exec start with 429 session_memory_busy when
the session's working set has reached 90% of its limit. Read as before,
that 429 would have counted as a spawner outage and the drain would have
attached to an exec that never ran, then failed the start after five
"not found" answers.

sessionExec reads the 429 body: a memory refusal becomes
SessionMemoryBusyError, a SessionExecLimitError, so the unchanged
sandboxCapacityRefusal (pinned by the legacy agent flow) gives it the
session scope every lane already waits out. The drain starts the exec
again after the refusal's hint (capped at 5 s) within its five-failure
budget, never attaching; past it, a task run parks as "Waiting for room"
(the host reason, not "an earlier process") and an automation turn
waits as sandbox_capacity, saying its sandbox is short of memory. Any
other 429 keeps reading as the spawner saying not now.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant