Skip to content

fix(sandbox): release resources safely under concurrent load - #4150

Merged
yannickmonney merged 1 commit into
mainfrom
fix/sandbox-parallelism
Oct 3, 2026
Merged

yannickmonney merged 1 commit into
mainfrom
fix/sandbox-parallelism

Conversation

@yannickmonney

@yannickmonney yannickmonney commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

High concurrency exposed retained output consumers, cancelled Docker work that could still start, serial Kubernetes recovery, and stale cleanup decisions. Bound session adoption to eight endpoint reads, stop adopting when drain begins, remove cancelled CLI waiters immediately, and disconnect stalled exec/attach consumers while keeping their commands attachable. Idle/TTL cleanup now atomically checks the work generation and activity cutoff on capable runtimes before freezing compute; workspace preservation and older-image compatibility remain intact.

A Node 24 container stress comparison ran 16 concurrent executions in four identical read-only containers with 24 paused output readers. The fixed bundle released all paused readers, kept eight healthy streams, and completed all 16 reattachments; observed aggregate memory fell from 1,216 MiB to 875 MiB (~28% lower in this stalled-reader scenario). This is a resource/backpressure test, not a measurement of model throughput or maximum production agent capacity.

Checklist

  • Root gate passes through its equivalent CI jobs: format/conflict/link checks, lint, typecheck, unit tests, UI tests, build, browser tests and Knip are green. Fresh platform UI run: 5,198 tests across 567 files, zero cached tasks. Backend integration also passes. Fresh local sandbox suite: 766 passed. Earlier local broad runs hit unrelated docs/UI timing failures; the fresh CI UI suite passed and the CI unit gate is green. No frontend code, helper, configuration or timeout was changed.
  • bun run lint:sast passes: 476 rules / 4,473 files / 0 findings; final adoption-source scan also clean.
  • Translations: N/A — no UI strings changed.
  • Product docs: N/A — no platform route, environment setting, public field or default changed. Sandbox service/runtime lifecycle docs and the automated coverage register are updated.
  • Root READMEs: N/A — internal sandbox behavior; affected service READMEs are updated.

Test plan

  • Regression tests cover bounded/duplicate/failed adoption, create and drain races, acquire/write races during idle and TTL expiry, protocol compatibility and malformed cutoffs, shared/priority Docker cancellation, and stalled HTTP consumers whose executions survive and replay.
  • Daemon tests on Linux: 271 passed, 0 failed, 0 skipped, 911 assertions with pinned Bun 1.4.2 in an existing Node 24 runtime/compiler image, including all 14 cases skipped on macOS. The disposable container used an init reaper, an unprivileged user, read-only source/root, no network or capabilities, no-new-privileges, and bounded CPU/memory/pids/tmpfs; it was removed afterward. Lint/typecheck clean.
  • Manual test structure gate clean. Independent adversarial review found the adoption/drain race, which is fixed and covered; no remaining blockers.
  • Node 24 stress used the existing runtime image with baseline/fixed locally bundled daemon mounts, four 512 MiB / one-CPU sessions, four execs each, 16 MiB paced stdout per exec, 24 deliberately paused consumers, and healthy reattachments. All disposable containers were removed.
  • Kubernetes cluster recovery and live-provider agent throughput remain deployment checks. Runtimes without idleReclaim retain the prior expiry checks until replaced.

Bound and fence session adoption across creates and drain transitions. Remove cancelled Docker CLI waiters immediately, close backpressured output consumers while executions remain attachable, and atomically fence idle/TTL cleanup by generation and activity time on capable runtimes.

Add regression coverage, update the sandbox lifecycle contract and make the egress nofile-warning fixture independent of host limits. Preserve workspace data, resource ceilings and older-runtime compatibility.
@yannickmonney

Copy link
Copy Markdown
Contributor Author

Post-merge independent review of #4150 at 8cd10d7c1: root merged it at 16:35Z with no review on record. Tale agent #2 3f9fdcee, TALE-359 run a721a46a-c362-49ac-8936-9fa055181d29. This is a source review by one read-only reviewer, plus my composition run with #4094.

Verdict: source accepted (INFO only).

Interactions

INFO

  • The daemon's backpressure ceiling counts the line being written (daemon/src/main.ts:167). A single event over 8 MiB would reconnect forever, but exec events are one pipe chunk each, so this can't happen today.
  • adoptExisting deduplicates by session id (:932). It is safe, because pod and container names are fixed per session.
  • The TTL cutoff compares the spawner's and the daemon's clocks (:1345). The idleForMs path already mixed the two.

Verified correct

  • Docker CLI slots (spawn-util.ts:193-219,285,299): cancellation is cleaned up, a slot granted at cancel time is released in finally, and release() is idempotent.
  • Daemon consumers: the exec is registered live before the first await, so there is no gap, and consumer.end() is idempotent.
  • Atomic idle claim: generation, cutoff, pinned, active and live are checked with no await between them.
  • Adoption: adoptSession re-checks the registry, in-flight creates and drain after the endpoint read.

Open: the disconnect detection relies on Bun's aborted/close events, and the platform's handling of the new error SSE event on a dropped backpressured stream wasn't traced.

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