Skip to content

fix(sandbox): repair pruned cache volumes, keep the destroy mount lookup - #4154

Merged
yannickmonney merged 3 commits into
mainfrom
fix/sandbox-cache-volume-repair
Oct 3, 2026
Merged

yannickmonney merged 3 commits into
mainfrom
fix/sandbox-cache-volume-repair

Conversation

@yannickmonney

@yannickmonney yannickmonney commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Why

Agent #2's post-merge review of #4116 (record 5967320911) found one MEDIUM and two LOWs on main.

The MEDIUM. A package cache volume pruned inside the five-minute "volume ready" window comes back from the next session's docker run --mount the way Docker creates a missing named volume: no tale.sandbox-cache label, root-owned, mode 0755.

  • Session uids 10001 and 65534, with every capability dropped, then cannot write /cache/{pip,npm,bun}.
  • The ensure past the window read any existing volume as ready, so the volume was never repaired.
  • The teardown refused it as unlabelled, so the platform's sweep answered 502.

What changes

The ensure checks the label (volume.ts).

  • It reads the labels with the same volume inspect --format '{{json .Labels}}' the teardown used. That read is now one helper, cacheVolumeLabel.
  • Labelled: ready.
  • Missing: created with its label, then chmod 1777, as before.
  • Unlabelled: volume rm, then recreated with its label and mode.
  • Held by a running session (rm refused): the same busybox chmod 1777, plus a [sandbox.volume] … lacks the tale.sandbox-cache label and could not be replaced (…) warning. It then counts as ready for the window, and a later check replaces it once it is free. If the chmod cannot run either (busybox out of reach), the ensure only warns and the create goes on, as it did before this PR, with that cache still unwritable.

Two more cases.

  • An inspect that fails for any reason other than "no such volume" now fails the ensure. Before, it read as missing.
  • On "already exists" from volume create (a refusal Podman gives; Docker answers success for an existing volume), the ensure succeeds only if the volume carries the label.

The window comment at volume.ts:138-142 now says what really happens.

LOW 1, teardown ordering (fixed). removeCacheVolumes forgets a volume in a finally after the removal, not before. A counter of the volumes forgotten so far stops an ensure that was in flight across a removal from marking the removed volume ready.

LOW 2, destroy lookup (fixed). The no-colour-directory shortcut is now opt-in (flatRootShortcut).

INFO: tests for the named mutants. A clock going backwards, a failed ensure not counted as ready (set-before-await), and the dot-directory, lost+found, workspace-directory and plain-file filters of hasLegacyRoots.

Docs. The DELETE /v1/organizations/:id bullet in services/sandbox/docs/sessions.md now says what a cache label means, why a volume can lack it, and how it is repaired.

Verification

No Docker daemon was used. Everything runs against fake docker CLIs that model Docker's documented behaviour:

  • a --mount of a missing named volume creates it unlabelled and 0755;
  • volume create of an existing name succeeds and leaves its labels unchanged;
  • volume rm refuses a volume that is in use.

Acceptance test (new docker-session-cache-volumes.test.ts): a real DockerSessionBackend with only runnerd mocked, against a stateful fake whose run creates a missing volume unlabelled.

  1. A create, then a stop.
  2. A prune.
  3. A create inside the window: Docker creates the volumes, and that session cannot write them.
  4. A create after the window: the session writes, and the volumes are labelled.
  5. The teardown removes all three.

A second case keeps the window's session running, so the volumes are made writable at once, with three warnings. They are replaced at a later check once nothing holds them, and the teardown then succeeds.

Failing before (the final tests at 33a288bb6, run against main's volume.ts and docker-session-backend.ts at the merge base 8cd10d7c1): 14 of 60 fail across the 5 files. They include:

  • both acceptance cases (checkedCreate reads {write: false, labelled: false});
  • the label, in-use, "already exists" and inspect-failure cases;
  • both teardown-ordering cases;
  • the moved-root destroy (exists(mounted) stays true).

Four of the 14 fail only because the inspect call now carries --format.

Passing after, at head 33a288bb6 on 8cd10d7c1:

  • the 4 files plus docker-workspaces: 60/60;
  • the full services/sandbox bun test: 778/778.

At 2eaefc3b0, before the third commit, exact-head CI read 28 pass, 13 skipped, 0 fail. Its Unit job ran the sandbox suite at 777/777, including the new tests by name.

Static checks:

  • oxlint --type-aware --type-check on the changed files: clean. A planted probe file proved it reports.
  • oxfmt --check: clean.
  • tsc --noEmit -p services/sandbox: clean.
  • Opengrep: 162 rules, 0 findings.

Mutation checks: 15 mutants, all killed:

  • the label check dropped;
  • no chmod on the in-use path;
  • an in-use chmod failure failing the create;
  • "already exists" accepted without the label;
  • the removal guard dropped;
  • the teardown forgetting only before the removal;
  • set-before-await;
  • the clock-backwards guard dropped;
  • an inclusive window boundary;
  • an unknown inspect failure read as missing;
  • destroy taking the shortcut;
  • each of the four hasLegacyRoots filters dropped.

The third commit comes from an adversarial pre-review by a separate read-only subagent (not the independent review).

  • It drops a cleanup I had added: removing a volume whose chmod failed. With busybox out of reach, that turned every create of an organization without caches into a failure.
  • It also softens the in-use chmod failure, adds tests for "already exists", and tidies the fakes and one log line.

Decisions and left open

  1. The window itself stays. A session created between an out-of-band prune and the next check still starts with an unwritable cache. That lasts until the next check: the organization's first create at least 5 minutes after the last check, and its chmod reaches the running session too. Closing the window needs one of these, each a separate decision:
  2. The teardown still refuses an unlabelled volume under a cache name. That is feat(platform): delete sandbox workspaces whose owner is gone or unused #4028's rule, pinned in docker-workspaces.test.ts:682-705. An organization deleted before any check repairs its volume gets a 502 on every sweep. The refusal also stops the teardown before it reaches that organization's later cache names, which was already the case. Making the teardown judge such a volume as the ensure does would reverse feat(platform): delete sandbox workspaces whose owner is gone or unused #4028's rule; that is the maintainer's call.
  3. listCacheVolumeOrganizations still lists by label only, so such a volume stays invisible to the orphan sweep until it is repaired.
  4. hasWorkspace keeps perf(sandbox): ask the docker daemon less on every session create #4116's shortcut. A held session never reaches it, and a stopped one has no container to ask.
  5. No manual register row. The repair is internal to the spawner and has no UI. Row 179 (workspace cleanup) is also in fix(sandbox): keep erasure pending until workspace bytes are deleted #4094's diff.
  6. A labelled volume whose chmod never ran stays unwritable, as on main. This happens when the chmod fails, when the create times out after the daemon made the volume, or when the spawner restarts between the two. A removal on failure only helps once the chmod no longer depends on a lazily pulled busybox:1.36. It could run in the always-present runtime image instead, or busybox could be pulled at boot.
  7. The create keeps perf(sandbox): ask the docker daemon less on every session create #4116's shortcut. Consider a session root that moved, then a session container that died on its own but is still present. On resume, that session gets a fresh flat workspace instead of its old mount. Before perf(sandbox): ask the docker daemon less on every session create #4116, the create adopted that mount when the spawner could see it. The fix would be to read the mount in the name-conflict reap branch.

Closes #4123

A package cache volume pruned inside the five-minute "volume ready" window
(#4116) came back from the next session's `docker run --mount` as Docker
makes a missing volume: no `tale.sandbox-cache` label, root-owned, 0755.
The sessions' unprivileged uids could not write `/cache/{pip,npm,bun}`, and
the ensure past the window read any existing volume as ready, so it was
never repaired; the teardown refused it as unlabelled.

The ensure now reads the volume's label, through the same inspect the
teardown used, now one helper. A volume without it is removed and made
again with its label and mode; one a session still holds is made
writable (`chmod 1777`) with a warning, and replaced at a later check
once free. An inspect that fails for another reason fails the ensure
instead of reading as missing, and a volume whose chmod failed is
removed, so a labelled volume always means a writable one.

The teardown forgets a volume only after removing it, and an ensure in
flight across a removal no longer marks the removed volume ready.

Refs #4123
#4116 skipped the legacy workspace lookups (the container's /agent mount,
the colour-directory scan) for every caller whenever the session root
holds no colour directory. A destroy therefore stopped asking the
container where its workspace is mounted: after a move of
SANDBOX_HOST_SESSION_ROOT or of a device's state directory, a live
session still mounted from the old root was destroyed without its data,
and the answer counted it gone.

The shortcut is now the caller's choice: a create and the hub's
placement check take it, a destroy keeps the lookups. New tests also
hold the filters that decide what counts as a colour directory (dot
directories, lost+found, workspaces, plain files).

Refs #4123
The removal of a volume whose chmod failed is dropped again: with busybox
out of reach (pruned, rate-limited, a daemon without registry access) it
turned every create of an organization without caches into a failure,
where the previous code fails the first one and runs the rest without a
writable cache. A volume a session holds that cannot be made writable
now only warns, rather than failing creates that went through before.

The "already exists" branch gets its tests, the volume fake clears its
gates between tests, the state fake spins only on EEXIST, and the mount
adoption log no longer says "resuming" on a destroy.

Refs #4123
@yannickmonney

Copy link
Copy Markdown
Contributor Author

Independent review of PR #4154 at 33a288bb69d10ea49452176e7e4d8cc7f69df9e7: accepted, with two LOW follow-ups.

Runtime (mine)

  • Composition with current main cce5b59f4, which includes fix(sandbox): keep erasure pending until workspace bytes are deleted #4094, fix(sandbox): refresh and harden all bundled harnesses #4155 and fix: reduce stream retention and add performance telemetry #4156 in services/sandbox: on a local main + this head (9679f7cc2, never pushed), the full services/sandbox suite passes 810/810. git merge-tree is clean, and nothing on main touches volume.ts.
  • CI at the exact head (merge ref c6fe159, onto 9fa2bf23e):
    • Workflows: Checks 37139117739, Build 37139117733, SAST 37139117799 and Commitlint 37139117743 all ran at attempt 1 and succeeded. E2E, CLI and Security were not triggered (path filters); I don't count them.
    • Check runs: 28 success, 12 skipped by design and Trivy neutral. Backend integration was skipped through Integration scope ("touches nothing the backend integration suite runs"), and Storybook through its path filter (build.yml:840).
    • Unit: it executed src/volume.test.ts and docker-session-cache-volumes.test.ts; they were not replayed.

Source review, cross-checked

  • The repair is right: an unlabelled cache-named volume is removed and recreated labelled at 1777.
    • What the rm can reach: names are ${prefix}-${orgId} with the id validated (volume.ts:11-41), the prefixes don't overlap, and there is no --force, so Docker refuses a volume in use. Every unlabelled cache-named volume was made by Docker's --mount or by an operator: labels have existed since feat(sandbox): code-execution sandbox + runnable artifacts #1727. The rm is safe.
    • Concurrent creates serialize on ensureInFlight. Every interleaving ends writable, at worst unlabelled, which the next check replaces.
    • "In use counts as ready" is safe: the chmod runs first, so the volume is 1777 for the window, and a failed chmod warns and is retried.
  • The forgotten counter is conservative. It is never cached on failure, runs in a finally, and across organizations its only cost is the old 3 inspects; it never fails a create.
  • LOW 2: none of destroySession's lines changed, and it still takes the mount lookup. A moved-root destroy stays tracked by fix(sandbox): keep erasure pending until workspace bytes are deleted #4094's workspaceDeletion (basename match).
  • The fakes model all three daemon assumptions: run --mount creates an unlabelled volume, a create of an existing name keeps its labels, and an in-use rm is refused.
  • Mutants:
    • (a) no label check, (b) no rm and (d) destroy using the shortcut are each killed by several tests;
    • (c) never incrementing forgotten is killed only by volume.test.ts:399.

LOW (follow-ups; both are among your "decisions left open")

  1. A chmod that fails during a repair leaves a labelled, permanently unwritable volume.
    • Path: rm, then create labelled, then the chmod throws (volume.ts:220,237-240,259). The third commit removed the cleanup, so every later ensure reads "labelled" as ready.
    • Trigger: busybox pruned and its pull refused.
    • Effect: the same end state as main (EACCES on /cache), and docs/sessions.md:385-392's "replaces it with a labelled, writable one" overclaims on this path.
    • Fix: chmod the existing volume first (it proves busybox runs), then rm, create and chmod. Or run the chmod from the runtime image.
    • Test: an unlabelled volume with fail-run stays unlabelled, and the next ensure retries.
  2. The ensure and the teardown disagree about unlabelled cache volumes.
    • The ensure removes them on live organizations (volume.ts:214-220), while the teardown refuses them (:109-113,127-129).
    • Scenario: an organization deleted while a pruned cache volume stayed unlabelled (in use at every check) 502s on every sweep. Its later cache names leak, silently if all three are unlabelled.
    • Since the ensure already deletes such volumes, the teardown judging them the same way would be the lower-risk rule. That is the maintainer's call; it doesn't block.

INFO

  • An inspect failure now fails the create. At most the first create per organization per window is affected; you disclosed this.
  • The third commit's behaviour change (no cleanup after a failed chmod) has no test.
  • A create racing its own organization's teardown can leave an unlabelled volume. The same race exists on main.

Merge: squash with --match-head-commit through my gate, which dry-runs first.

@yannickmonney
yannickmonney merged commit 7063f99 into main Oct 3, 2026
@yannickmonney
yannickmonney deleted the fix/sandbox-cache-volume-repair branch October 3, 2026 20:04
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.

Follow-ups from the #4116 review: a cache volume deleted inside the 5-minute ready window comes back root-owned and unlabelled

1 participant