Skip to content

Bug: concurrent runs of one agent pile into one sandbox session and can exhaust the host #3977

Closed

Description

@yannickmonney

Summary

Tale admits sandbox work per session, but the load comes from runs. A run that a project editor starts for a project agent joins that agent's standing session, from any door: manual start, status move, @mention or auto-retry. Joining a session that is already active is never counted, and only automated and delegated starts refuse a busy agent. So any number of Claude Code processes can share one 4 GiB / 2 CPU session container. Nothing at admission looks at host memory or CPU. When a run dies of the resulting pressure, it is retried immediately, into the same saturated session.

This happened on a production deployment on a 4 vCPU / 16 GB host. The result was 13 concurrent runs in 6 sessions, 43 OOM kills (one of them host-wide), a platform stall of about 4 h 45 min (database pool timeouts, 503s on agent-run requests) and about 64 run-hours spent on runs that failed. The sandbox quota (maxSessionsPerOrg: 6, SANDBOX_MAX_SESSIONS=8) was never exceeded.

What happened

This covers one day on a production deployment. The host had 4 vCPU, 15.6 GiB RAM and 4 GiB swap. Settings: the default agent profile (4 GiB memory, no container swap, 2 CPUs), maxSessionsPerOrg: 6, SANDBOX_MAX_SESSIONS=8, worker concurrency 5. Times are UTC.

  • Bursts on a few agents.
    • 09:41: 9 manual starts on 4 agents within 25 s.
    • From 10:17: failed runs from that burst auto-retried onto the same sessions.
    • 10:59: 5 more manual starts within 18 s.
    • Live runs then reached 13 runs on 6 sessions. They stayed at 13 for a total of 2 h 48 min between 10:59 and 13:52 (with short dips to 12), and at 10 or more for 4.5 h.
    • One session carried 4 concurrent runs for 3 h 19 min; its memory was seen at 3.9 of its 4 GiB. Another session carried 3 runs for 2 h 53 min.
    • In the 09:41 burst, start-to-exec latency was 76–164 s, against the usual 0–3 s.
  • The quota never saw it. 13 runs in 6 sessions is within maxSessionsPerOrg: 6. The quota engaged only once that day: a 7th agent needed a 7th session, and its run was parked for about an hour while 11 other runs were live.
  • Memory.
    • 42 OOM kills inside session containers, spread over 6 containers. Each time the cgroup was at its 4 GiB limit with a swap limit of 0. The killed processes included MainThread, node, tsgolint and headless Chrome.
    • One host-wide OOM (global_oom) at 11:28. About 12 GiB of anonymous memory was in use, and 13 claude and 12 headless-Chrome processes were live. At that moment about 2.1 GiB of swap was still free.
    • MemAvailable fell to 1.6 GiB. Swap-in peaked at 5.8k pages/s, with 5.3k major faults/s, and page-in reached about 1.4 GB/s.
  • CPU (sar, 10-minute samples).
    • The 1-minute load average ran at 26–101 from 11:20 to 14:20. Six samples in that window were at 154–244, peaking at 244 at 14:00.
    • The run queue reached 120, with up to 182 blocked processes.
    • CPU idle stayed below 1 % from 11:20 to 14:20. System CPU ran at 27–54 % and iowait reached 30 %.
  • The control plane stalled for about 4 h 45 min (11:18–16:04).
    • Worker:
      • 2,536 × timeout exceeded when trying to connect from the DB pool, across every pg-boss queue, including task.agent_retry and sandbox.release_idle.
      • task.agent_drive failed with CONNECT_TIMEOUT db:5432.
      • pg-boss LISTEN/NOTIFY heartbeats timed out.
    • API:
      • store "app_db" is unreachable (> 5 s), and Docker DNS lookups of db failed with EAI_AGAIN.
      • GET agent-runs/latest and POST agent-runs/start returned 503.
      • The health endpoint was observed taking about 7 s.
      • Admin calls over SSH timed out after 10 min.
    • Spawner: 15 × docker ps timeouts. One liveness docker inspect failed, and the session was treated as alive.
    • Watchdog: 129 re-attached abandoned turns, 19 refused resume claims, and 9 finalize claims burned while the run was still live.
  • Outcome.
    • 91 runs, of which 51 failed (30 of them harness_error).
    • 52 auto-retries, of which 26 failed again. One task ran 8 times; another spent 554 run-minutes over 7 runs.
    • About 64 run-hours went to runs that failed, against about 21 h and 33 h on the two days before.
    • The p50 duration of settled runs rose from 49 to 71 min over the three days.
  • It recurs. The day before, the same host peaked at 9 concurrent runs, load 26, 1.7 GiB of swap in use and 7 OOM kills. The day before that had 22 OOM kills.

Caveat on attribution. The run row never records an OOM kill: a memory-pressure death settles with the generic harness_error code. A CLI killed outright reads "The harness exited unexpectedly (exit code N)". 20 of the day's 30 harness_error runs say only "the agent run failed". No run recorded exit code 137, so kills can only be matched by timestamp: 6 of the 51 failures ended within 120 s of an OOM kill. 13 failures had other causes: 4 from a scheduled OS reboot, 7 credential_rotated, and 2 vendor rate limits.

Steps to reproduce

  1. On a Docker host with the default agent profile (4 GiB, 2 CPUs per session), have a project editor assign three or four tasks to one project agent.
  2. Start them all within a minute, with Start or by moving each card to In progress.
  3. Every run executes as its own Claude Code process in the agent's one session container. The org's session count stays at 1. With real coding work (builds, linters, headless browsers), the container's memory reaches the 4 GiB ceiling and the kernel OOM-kills processes inside it. Each failed run is retried at once into the same container.

Expected behavior

  • Admission accounts for runs, not only sessions. Extra runs for a busy agent wait with a visible reason instead of piling into one container.
  • Sandbox load cannot starve the platform's own API, database and worker.
  • A run killed for lack of memory is not retried at once into the same saturated container.

Root cause

Line references are against main at 88259af.

  1. Admission counts sessions, and sibling runs join for free.
  2. Concurrent runs in one session are by design, except on the automated lane.
  3. Every run in a container shares its budget, and the container has no swap.
    • agentProfile sets 4 GiB and 2 CPUs (services/sandbox/src/config.ts:483-497), with --memory-swap equal to --memory (services/sandbox/src/session/docker-session-args.ts:419-422).
    • N CLIs, plus their headless browsers and linters, share one 4 GiB cgroup. They exhaust it, and the kernel OOM-kills one of them.
    • Session memory cannot be swapped. Under host-wide pressure, the kernel can only drop page cache and swap out memory outside the sessions, which includes Postgres, the API and the worker. That fits the host-wide OOM with about 2.1 GiB of swap still free, and the heavy swap-in while the database stopped answering.
  4. Limits are ceilings, not reservations, and nothing ties them to the host.
  5. Auto-retry feeds the fire.
  6. The retry budget never runs out under slow thrash.
    • Any attempt that executed for 15 min or more resets the 3-attempt streak (task_auto_retry.ts:78, :219-244). So a run that slows past 15 min and is then OOM-killed never uses up its attempts.
    • Person-started runs skip the per-task hourly breaker (task-list.ts:1120-1131).
  7. Queueing is narrow and hard to see.
  8. Plausible, but not verified in logs: recovery timing assumes a healthy host.

Impact

  • Any deployment where people give agents several tasks at once can stall its own control plane: API, database, worker and admin access. That hits everyone on the deployment, not just the agents.
  • The shipped defaults allow it. Even the default budget of 2 project sessions admits any number of runs. The quota and the spawner cap are validated against counts, never against host RAM or CPU.
  • Direct waste: in one day, about 64 run-hours went to runs that failed, along with the model usage behind them. Retries went back into the saturated sessions.
  • Resource deaths are classified as harness_error, so neither operators nor users can see why runs fail.

Proposed fix

Paths below are relative to services/platform/backend/ unless they start with services/, packages/ or .env.example.

Recommended: one live run per agent session, with the rest queued and a visible reason

Apply the automated lane's agent_busy rule to every door:

  • Before ensureAgentSession, take a per-session advisory lock. If another run on the same session_id is running, park this run with a reason. Reuse waiting_for_capacity_at_ms and add a new waiting_reason column (e.g. agent_busy).
  • When a run settles, fails or is cancelled, wake the oldest run parked on that session. Today's wake fires only on a session release, and that never happens while siblings are waiting.
  • Optionally add a per-agent maxConcurrentRuns setting (default 1) for large hosts.

Where:

  • core/tasks/agent_run_host.ts: start gate (~:967-998), park branch (:1391-1403), settle and release paths (:1885-1935, :2017-2029).
  • domains/tasks/agent-runs.ts: a per-session variant of wakeParkedAgentRuns (:639-667), and the reason in RUN_COLUMNS.
  • domains/sandbox/sessions.ts: a settle-edge wake next to releaseProjectAgentSessionSlot.
  • domains/tasks/watchdogs.ts: a backstop wake per session.
  • domains/tasks/delegated-start.ts:632-649: one shared helper so both lanes agree.
  • domains/tasks/agent-turn-shim.ts:362-373.
  • A new migration, plus the run entry and locales.

Trade-offs:

  • This removes the main multiplier: N runs in one cgroup. It also stops several CLIs from writing in one workspace at once.
  • A busy agent works its tasks one after another, so the queue must be visible (see E).
  • A woken run must keep the exec-fenced single-winner claim so it never double-starts.

Also needed (complementary, in priority order)

B. Treat capacity failures as capacity, and back off retries.

  1. When the spawner answers 429 session_quota (or host_pressure from C) at start, park the run with retry-after as the wake time, instead of failing it with start_failed.
  2. Classify resource deaths as a new code, resource_exhausted. That covers exit 137 from the CLI or an OOM event on the session container. Retry these with exponential backoff through startAfterMs (e.g. 2 / 10 / 30 min). Exempt them from the 15-min progress reset. Route the retry through the busy and capacity admission so it can park.
  3. Cap retries of person-started runs per task per hour as well.
  • Where:
    • core/tasks/task_auto_retry.ts and core/tasks/start_failure.ts.
    • core/tasks/agent_run_host.ts (:1390-1433, :1604-1715).
    • domains/tasks/agent-runs.ts:472-486 and jobs/task-list.ts:1039-1159.
    • core/node_only/sandbox/helpers/session_client.ts:91-102, for a typed capacity error.
    • handleExec in services/sandbox/src/session/session-routes.ts, so the exit event carries the OOM reason.
  • Trade-offs:
    • Genuine transient faults recover more slowly.
    • The at-most-one-retry-arm invariant must hold.
    • The exit reason has to be passed through runnerd and the spawner.

C. Admit by host headroom in the spawner.

  • Before a session create and before an agent exec, refuse with a typed 429 host_pressure plus retry-after when either:

    • MemAvailable is below the session memory plus a platform reserve, or
    • PSI (/proc/pressure/{memory,cpu}) is above a threshold.
  • Use hysteresis so the decision does not flap.

  • At boot, warn (or clamp) when SANDBOX_MAX_SESSIONS × SANDBOX_AGENT_MEMORY is more than host RAM minus the reserve.

  • Where:

    • services/sandbox/src/capacity.ts: reuse parseMemory and the host observation, and add PSI.
    • services/sandbox/src/session/session-routes.ts: admit (:190-207), reserveCreate (:209-235) and handleExec.
    • services/sandbox/src/config.ts: new SANDBOX_MIN_AVAILABLE_MEMORY / SANDBOX_MAX_PRESSURE settings.
    • .env.example and services/sandbox/docs/sessions.md.
  • Trade-offs:

    • It is the only guard that holds whatever the quota configuration.
    • It works on a local Docker host only; on K8s, rely on pod requests.
    • Readings are noisy.
    • It gates only at start, so it needs A to stop growth inside a session.

D. Protect the control plane.

  • Give session containers a CPU weight well below the platform containers (--cpu-shares / --cpu-weight), and optionally set --memory-reservation.

  • Document the sizing rule next to the quota editor's deployment check.

  • Where:

    • services/sandbox/src/session/docker-session-args.ts:419-438 and services/sandbox/src/config.ts:444-497.
    • services/sandbox/docs/sessions.md and .env.example.
    • Optionally domains/governance/routes.ts:186-229, to compare total × memory with host RAM through /v1/limits.
  • Trade-offs:

    • It is cheap, costs nothing when idle, and keeps the API, health checks and admin calls usable.
    • It does not stop oversubscription on its own.

E. Make waiting visible.

  • Include queued runs that have a waiting reason in the ops indicators. Show "waiting for a sandbox slot" or "waiting for " on the board card.

  • Show how many runs share each session in the Sandboxes settings.

  • Show the /v1/capacity pressure snapshot as a banner.

  • Add metrics for parks, wakes and resource failures.

  • Where:

    • domains/tasks/service.ts:4144-4194 and its all-projects sibling.
    • services/platform/app/lib/backend/contract/tasks.ts.
    • The board card, services/platform/app/features/tasks/components/task-agent-run-entry.tsx:271-276 and services/platform/app/features/settings/sandboxes/*.
    • Locales, docs, the platform's manual-test boxes and services/platform/tests/manual/reference/error-codes.md.
  • Trade-offs:

    • It reduces no load by itself.
    • It makes the queueing from A–C understandable, and it stops users from re-starting runs by hand.

Alternatives considered

F. Count runs, not sessions, against the quota.

  • Add maxAgentRunsPerOrg to sandbox_quota, admit each run under lockOrgAdmission, and validate the budget against a ceiling the spawner exposes.
  • This fixes the accounting hole org-wide. Costs:
    • It changes a shipped config schema.
    • A leaked run slot blocks the whole org.
    • On its own it still lets several runs share one 4 GiB cgroup, so it is only worth doing together with A.
  • Where:
    • packages/shared/src/schemas/governance.ts:143-160 and core/sandbox/quota_policy.ts.
    • domains/sandbox/sessions.ts:355-394 and core/node_only/sandbox/agent_session.ts:94-117.
    • domains/governance/routes.ts:186-229 and the spawner's /v1/limits.
    • The quota editor, fixtures and docs.

G. Make recovery robust under load (secondary).

  • Raise the stale-start threshold above the create timeout plus staging time, or give an in-flight start a lease heartbeat.

  • Deduplicate task.agent_drive per exec with a singletonKey.

  • Stop tying the 90 s drive windows to worker batch slots.

  • Where:

    • domains/sandbox/recovery.ts:19-24 and domains/tasks/reattach.ts:99-152, 181-248.
    • domains/tasks/agent-turn-shim.ts:888-900.
    • jobs/runner.ts:33-45 and jobs/tasks.ts.
  • Trade-offs:

    • It removes churn, not the overload.
    • Genuinely dead chains are detected more slowly.
    • It touches the exactly-once machinery.

Acceptance criteria

  • One run per session. Start N ≥ 2 tasks on one agent through any door: manual start, move to In progress, @mention, delegation or auto-retry. At most one of them (or maxConcurrentRuns) runs in that agent's session at a time. The rest are queued with a waiting reason. An integration test covers each door.

  • Queued runs wake correctly.

    • A queued run starts within one wake cycle after the run ahead of it settles, fails or is cancelled, without the session being released.
    • Runs start in FIFO order.
    • A run never double-starts when a settle-edge wake races the watchdog.
    • An integration test covers this.
  • Capacity refusals park. A spawner 429 session_quota or host_pressure at start parks the run, with the reason set and retry-after honoured, instead of failing it. Unit and integration tests.

  • Host-headroom admission. The spawner refuses a create or exec with host_pressure when MemAvailable is below the configured threshold. It warns at boot when SANDBOX_MAX_SESSIONS × SANDBOX_AGENT_MEMORY is more than host RAM minus the reserve. Unit tests with fixture /proc files.

  • OOM deaths are classified and backed off. An OOM-killed agent CLI settles with a distinct resource failure code, not harness_error. Its auto-retry:

    • is delayed with backoff,
    • does not get the 15-min progress reset,
    • goes through admission.

    Unit tests in task_auto_retry and an integration test for the settle path.

  • Person-started retries are capped per task per hour.

  • CPU weight. Session containers run with a lower CPU weight than the platform containers.

  • Waiting is visible. The board card and run entry show why a run is waiting, in every shipped locale and with an accessible label. The Sandboxes settings show runs per session.

  • Load test.

    • Setup: a 4 vCPU / 16 GB host, or a runner with the same cgroup limits, with default settings. Start 12 runs across 7 agents within 30 s, using a harness stub that allocates about 2 GiB and keeps a CPU busy.
    • Expected results:
      • no host-wide OOM;
      • MemAvailable stays above the reserve;
      • no DB pool connection timeouts;
      • /api/health p95 under 1 s;
      • extra runs wait with a visible reason;
      • no run fails for lack of capacity.
  • Docs. Update .env.example, services/sandbox/docs/sessions.md, the quota docs and the platform manual-test suite (new boxes and error codes).

Out of scope

  • Resizing hosts. A bigger host only moves the threshold. The run-per-session gap is the same on any host, and the same defaults accept 8 × 4 GiB sessions whatever the host's RAM.
  • Other failures that day. The scheduled OS reboot that ended 4 runs, the credential_rotated 401s and the vendor rate limits had separate causes, not load.

Activity

  1. added
    priority: high
    perf
    bug
    service: platform
    on Sep 30, 2026
  2. yannickmonney commented on Oct 10, 2026

    @yannickmonney
    ContributorAuthor

    Status on main (73d03a9), from the sandbox footprint and robustness work:

    Follow-ups found on the way: #4717 (final failure announced late after the OOM hold), #4718 (a full but idle session refuses every exec), #4723 (automation nodes' OOM hold), #4726 (memory reservation for the database and platform).

    The premise of this issue — concurrent runs of one agent piling into one session — no longer holds on main; left open for the owner to close or narrow.

  3. added
    agent-ready
    on Oct 29, 2026
  4. tale-code-bot commented on Oct 30, 2026

    @tale-code-bot
    Collaborator

    I re-checked this issue against main at b2560c2 and train at 8b6fb96 (TALE-47).

    Fix A is done on main

    #4603 (4b3664b) did it. That commit is on main, release/0.6, train and feature-train.
    Each run of an agent now claims a worker of its own:

    • Every door goes through one claim. Start, a move to In progress, an @mention, delegation,
      dispatch and the automatic retry all call kickAgentRun. Before anything runs, the
      task.agent_turn job claims a worker (claimAgentWorker,
      services/platform/backend/jobs/task-list.ts:1604, and
      services/platform/backend/domains/tasks/agent-workers.ts). The claim holds the agent's lock
      and the organization's admission lock.
    • The schema allows one live run per worker (project_agent_runs_one_per_worker, migration
      0167).
    • A run that finds no worker parks with its waiting_reason (org_limit, host,
      destroy_pending, exec_limit or model_capacity). The Run row, the timeline and the chat
      tray say so in EN, DE and FR.
    • A parked run is woken when a worker frees. A run's end releases its own worker
      (releaseProjectAgentSessionSlot, services/platform/backend/domains/sandbox/sessions.ts:392),
      and the 2-minute watchdog wakes parked runs as a backstop.
    • The rules are TASK-R24 to TASK-R27 in services/platform/backend/domains/tasks/spec.md.
    • CI proof: Backend integration on chore: merge train 20261030-0747 #5134 (head b30c51e, run 41023898060, job 123969992023)
      passed 2344 of 2344 checks across 290 lanes. The agent workers lane's 15 checks and the busy agent retry lane's 7 are among them.

    Filed as separate issues

    Each is labelled agent-ready and links here.

    These related issues were already open:

    Checked and not filed

    TALE-47 goes back to triage with this evidence. Nothing in this issue's scope is left outside the
    issues above.

  5. tale-code-bot commented on Oct 30, 2026

    @tale-code-bot
    Collaborator

    Closing as completed. Fix A shipped in #4603: each agent run now gets a worker of its own instead of piling into one sandbox session. Its merge commit 4b3664b2562fd827068260fe031d3739be77fd2c (2026-10-09) is an ancestor of main at 49ce42fdd3b20a452a2572554c587f4f89bdb4f8 (release/0.6 is the same commit). The status comment of 2026-10-10 above says the same.

    The parts still missing are tracked in issues of their own, each linking back here: #5147, #5148, #5149, #5150, #5151 and #5152. #4726, #4717, #4723, #5146 and #4720 were already open. The comment of 2026-10-30 above lists what was checked and left unfiled, and why.

    Board task: TALE-47.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent-readybugperfpriority: highservice: platform

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions