Skip to content

perf(sandbox): ask the docker daemon less on every session create - #4116

Merged
yannickmonney merged 1 commit into
mainfrom
perf/sandbox-create-docker-calls
Oct 3, 2026
Merged

yannickmonney merged 1 commit into
mainfrom
perf/sandbox-create-docker-calls

Conversation

@yannickmonney

Copy link
Copy Markdown
Contributor

Why

Every session create on the Docker backend spawned a docker CLI process (~30–80 ms each, a Go binary with its own memory) per package cache volume of the organization — three docker volume inspect calls in a row — although those volumes exist after the organization's first session. A create for a session without a workspace also ran a docker inspect and scanned the session root for a workspace under a blue/green colour directory, a layout the root has not had since it was flattened. In a burst of creates these calls also compete for the 12 docker CLI slots the spawner allows.

What changes

  • Cache volumes are remembered. A volume found or made ready is taken to still be for five minutes; the organization's teardown forgets its volumes at once, and one pruned behind the spawner's back is made ready again after the window. Concurrent ensures still share one check, and the three volumes are ensured at once instead of one after the other.
  • The legacy workspace lookup runs only where it can find something. The session root is read once for a colour directory; a root without one (every deployment since the flattening) skips the docker inspect and the scan.

A second create of the same organization now asks the daemon one thing: docker run.

Verification

  • volume.test.ts (new, against a fake docker CLI that logs every call): a ready volume is not asked about again within the window, is checked again past it without being remade, is forgotten by the organization's teardown, and concurrent ensures share one check.
  • docker-session-create-calls.test.ts (new): the first create asks for the three cache volumes and the second none, neither runs the legacy mount lookup on a flat root, and a root that still holds a colour directory keeps it. Both fail on the old code.
  • services/sandbox: 750 tests green; lint and typecheck clean.

Part of the sandbox efficiency work after #4097, #4098 and #4099.

Each session create on Docker spawned a docker CLI process per package
cache volume of its organization — three `docker volume inspect` calls, one
after the other — and, for a session with no workspace yet, a `docker
inspect` and a scan of the session root looking for a workspace under a
colour directory the root has not had since it was flattened.

A cache volume found or made ready is now taken to still be for five
minutes (an organization's teardown forgets it at once, and a volume pruned
behind the spawner's back is made ready again after the window), and the
three are ensured at once. The legacy lookup runs only while the session
root still holds a colour directory, which is read once. A second create of
the same organization now asks the daemon one thing: `docker run`.
@yannickmonney

Copy link
Copy Markdown
Contributor Author

Post-merge independent review of #4116 at d5453d02f (merged by root at 08:20:54Z with no review on record). Tale agent #2 3f9fdcee, TALE-359 run 6b60fb49-b479-4eeb-850a-951812ec31f3. This is a source review by one read-only review agent, cross-checked by me; nothing was executed for it.

MEDIUM: within the 5-minute "volume ready" cache, a deleted cache volume is recreated without its label or permissions, and the spawner never repairs it.

  • Where:
    • the cache early-return at volume.ts:162-163;
    • :178-182, which reads any existing volume as ready, with no label or permission check;
    • docker-session-args.ts:346-353 (--mount type=volume,…).
  • Scenario:
    1. A create marks an organization's three cache volumes ready until T0+5m.
    2. The organization's sessions stop.
    3. An operator runs docker volume prune -a or volume rm.
    4. The next create inside the window skips the ensure, and docker run --mount lets Docker create the volume itself: no tale.sandbox-cache label, root-owned, mode 0755. The runtime image has no /cache to copy permissions from.
    5. Session processes run as 10001/65534 with every capability dropped, so writes to /cache/{pip,npm,bun} fail with EACCES.
    6. After the window, the ensure finds the volume, never applies chmod 1777, and never adds a label. The damage is permanent.
  • Knock-on effects: the unlabelled volume is invisible to listCacheVolumeOrganizations and refused by removeCacheVolumes, so platform teardown sweeps can answer 502.
  • The code's own comment (volume.ts:138-142, "made ready again once this has passed") is wrong.
  • Before the PR, the same end state needed the removal to land within milliseconds.
  • Status: from source, relying on Docker's documented creation of a missing named volume on run. Not executed here.
  • Fix: have the ensure check the label. If it's missing, volume rm and recreate the volume with its label and chmod; if it's in use, at least chmod and warn.
  • Test: a fake run that creates an unlabelled volume when one is missing.

LOW

  • Teardown can leave the cache marking a deleted volume ready. It clears the entry (volume.ts:100) before it removes the volume (:101-125). An ensure already in flight sets "ready" again for a volume that is gone. Reaching this needs a create racing the organization's deletion.
  • A destroy can miss workspace data after a root change. The no-colour-directories shortcut (docker-session-backend.ts:153) skips the docker inspect mount lookup for destroy too. After SANDBOX_HOST_SESSION_ROOT or a device's stateDir changes, destroy reports hadWorkspace=false and the old data stays while the platform counts it erased. Keep the lookup on destroy.
  • INFO: test gaps. Mutants of the boundary check, the set-before-await, and the dot-directory and lost+found filters would survive.

Verified correct:

Next owner: the manager, for a follow-up issue on the MEDIUM.

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