Skip to content

fix(sandbox): delete a destroyed workspace in the background - #4088

Merged
yannickmonney merged 2 commits into
mainfrom
fix/sandbox-destroy-in-background
Oct 2, 2026
Merged

yannickmonney merged 2 commits into
mainfrom
fix/sandbox-destroy-in-background

Conversation

@yannickmonney

Copy link
Copy Markdown
Contributor

Summary

A Destroy deleted the session's workspace before it answered, with no time limit (rm(workspaceHostDir, { recursive: true }), step 5 of #4086). A workspace of tens of GB in over a million files, on network block storage, takes longer than the 30 s the platform gives DELETE /v1/sessions/:id. Settings → Sandboxes then reported "action failed" and kept the row stopped, while the spawner went on deleting. This happened on the production platform on 2026-10-02.

Once the container is confirmed gone, the Docker backend now renames the workspace into <root>/.trash/ses-<id>.<uuid>. That is one directory entry on the same filesystem, however much the workspace holds. Then it answers, and one background pass per session root empties the trash one entry at a time, so a burst of destroys doesn't flood the disk under running sessions.

  • Restart or crash: the boot sweep empties what the last process left in the trash, without holding up boot. The 5-minute periodic sweep retries anything a pass could not remove.
  • Repeated DELETE: finds nothing and answers destroyed: false.
  • Same id reused: a new session gets a fresh ses-<id>, and every trash entry has its own random suffix, so the two never meet.
  • Fallback: where the rename cannot happen (another filesystem, no room for a directory entry), the workspace is deleted in place as before, and a failure there still answers 502.
  • Kubernetes: unchanged. A deleted PVC already goes to its provisioner.

This complements #4087, which queues the platform's Destroy and retries it. With this change, the spawner call that job makes answers within seconds instead of running into the 30 s cap and its retry ladder. The two PRs touch disjoint files. Refs #4086.

Checklist

  • bun run check passes — run for the touched workspace, not the whole monorepo: bun run --filter @tale/sandbox test (640 pass, 0 fail; 630 before), typecheck, lint and format:check all clean, plus bunx knip --workspace services/sandbox, bun run lint:conflicts and bun run lint:links. CI runs the full gate.
  • bun run lint:sast passes — Opengrep on the changed files: 0 findings.
  • Translations — N/A: no user-visible string changes.
  • Docs — N/A for docs/{en,de,fr}/: nothing a user sees changes. fix(platform): run a sandbox Destroy as a job, not in the request #4087 covers the admin page. The sandbox's own docs/sessions.md and README.md describe the trash.
  • README.md / README.de.md / README.fr.md — N/A.

Test plan

New tests use the fake Docker CLI, as the existing ones do:

  • DELETE answers 200 while a held deletion still has all 2,000 files on disk, and the tree is gone afterwards.
  • Starting the spawner empties trash left by a crash and leaves workspaces and pin markers alone.
  • A second destroy finds nothing to do, both during the first one's deletion and after it.
  • A new workspace under the same id is fresh, and the old deletion doesn't touch it.
  • The in-place fallback deletes the workspace, and still fails loudly when it can't.
  • Trash unit tests: one pass at a time, and a removal that fails is retried by the next pass.

Each of these was checked to catch the bug: making the destroy wait for the deletion, removing the boot sweep, or giving trash entries non-unique names each made the matching test fail.

Not observed yet: a real spawner deleting a large workspace. After deploy, destroy a stopped session with a realistic tree. The DELETE should answer in about a second, <root>/.trash/ should hold the entry briefly, and the volume's free space should come back.

Behaviour to weigh: once the caller is told "destroyed", a failed background deletion shows only as a logged and retried entry in .trash/, not as a 502. A destroy of a running session still waits for docker rm --force (up to 30 s) and, with Docker-in-Docker, the inner volume removal (up to 10 s). Those were not the reported failure, which was on stopped sessions.

A destroy deleted the session's workspace before it answered. A workspace
of tens of GB in over a million files on network block storage takes
longer than the 30 s the platform gives DELETE /v1/sessions/:id, so
Settings > Sandboxes reported "action failed" and kept the row stopped
while the spawner went on deleting.

Once the container is confirmed gone, the Docker backend now renames the
workspace into <root>/.trash/ under a name of its own and answers; one
background pass per session root then empties the trash, entry by entry.
The boot sweep empties what a restart or crash left there, and the
periodic sweep retries what a pass could not remove. A repeated destroy
finds nothing (destroyed: false), and a new session under the same id
gets a fresh workspace beside the one still being deleted. Where the
rename cannot happen (another filesystem, a full disk), the workspace is
deleted in place as before, and a failure there still answers 502.
Kubernetes already hands a deleted PVC to its provisioner.
The platform's hourly workspace cleanup reads this inventory. A destroyed workspace waiting in .trash/ under a ses- name must never be listed as a workspace, or the sweep could count it or ask for it to be deleted again. The colour-root walk already skips every dot-dir; this pins .trash/ in its test beside .pins/.
@yannickmonney
yannickmonney merged commit e1e1751 into main Oct 2, 2026
@yannickmonney
yannickmonney deleted the fix/sandbox-destroy-in-background branch October 2, 2026 16:43
@yannickmonney

Copy link
Copy Markdown
Contributor Author

Independent post-merge review of #4088 at 5dd6c55f3e648883c4d6e8f79441d18ba99ae9e8 (merged as e1e175170): sound, with one MEDIUM to route as a follow-up.

  • Reviewer: Tale agent update dashboard ui (#508) #2 3f9fdcee-79e7-430b-a430-ba179d5b5132, TALE-359 run 026eb423-c02e-4379-92dd-b152250ef1d9. Implementation: not a cycle agent; identity unrecorded.
  • The merge wasn't this run's. The shared principal merged it at 16:43:04Z, without an agent review.
  • Squash checked: a single parent 1f0009cc3, and its tree equals git merge-tree with the head. The lines match per file in all 11 files, there are no trailers, and the signature is valid.
  • Method: a read-only review agent covered the trash, the background pass, the boot sweep, the route and the platform at e1e175170. I re-checked the MEDIUM in source.

What holds

  • Rename: atomic, to ses-<id>.<uuid> under <root>/.trash, so names never collide.
  • Errors: ENOENT on the source and on the destination are told apart.
  • A new session under the same id is never touched. A create waits for a destroy already under way, two destroys of one id run in turn, and the drain reads only .trash.
  • Session ids are limited to [a-zA-Z0-9_-]{1,64}.
  • The event loop: no synchronous fs call was added, and one tree per root is deleted at a time.
  • The boot sweep runs after the spawner lock and touches only .trash, which the inventory and the resolver skip.
  • The route answer is unchanged: 200 {destroyed, busy}, or 502.
  • Local: services/sandbox passes 640/640 at e1e175170, including the 34 tests in the three changed files.
  • Main CI on e1e175170: Checks, Build, Commitlint and Scorecard succeeded. SAST was cancelled, superseded by fix(platform): run a sandbox Destroy as a job, not in the request #4087's push.

MEDIUM: an erasure now records "deleted" before the bytes are gone, and hears nothing if the delete keeps failing.

  • Where:
    • services/sandbox/src/session/workspace-trash.ts: discard() resolves after the rename and then calls void this.empty().
    • A background failure is only console.warn("[sandbox.trash] removing … failed; the next pass retries it").
  • The platform's contract (backend/domains/sandbox/workspace-cleanup.ts, eraseMemberWorkspaces): "the erasure receipt must be true. THROWS when one could not be deleted … the receipt reads partial and a Retry runs it again".
  • Result: a member erasure, an org retirement or the sandbox_workspace.deleted audit row records success while the tree still sits in .trash.
    • On large or slow storage, that can last minutes.
    • If the delete fails permanently (EIO, a read-only filesystem, an undeletable file), it lasts forever. Nothing lists it, and the only signal is a warning every 5 minutes.
    • Before this change, that failure answered 502 and the erasure stayed retryable.
  • Fix:
    • report the trash backlog (count, oldest age, last error) in /v1/workspaces or /health, and escalate after N failed passes;
    • and either give erasure a strict "wait until gone" delete, or document asynchronous deletion in the erasure contract.

LOW

  1. The in-place fallback brings the 30 s failure back. Any rename error other than "nothing there" deletes the whole tree inside the request, with no limit. That covers EXDEV, for example a legacy path outside the root, and ENOSPC on a full disk, which is when an admin is freeing space. The standard layout takes the fast path. Renaming within the same parent first would fix it.
  2. A deletion can wait up to 5 minutes. drain() returns on a readdir error without checking passAgain, and an empty() that lands between the last check and .finally joins a finished pass. No data is lost. Fix: if (this.passAgain) void this.empty(); in .finally. The second case is reasoned and unverified.
  3. A stuck or slow pass is invisible. It logs only when an entry finishes, so one hung rm freezes every later deletion with nothing in the logs.
  4. .trash is trusted without checks. mkdir({recursive:true}) accepts a symlink there, and the boot pass empties an existing .trash under an operator-chosen root whatever it holds. This is hardening only, since planting either needs write access to the root.
  5. Other destroy steps can still pass 30 s: docker rm --force (30 s) plus the DinD volume removal. fix(platform): run a sandbox Destroy as a job, not in the request #4087 absorbs this on the platform side.
  6. Unpinned guards:
    • the 5-minute retry empty();
    • the boot test plants a fresh .trash, so it can't catch a host-dir sweep that starts taking it;
    • "rename ENOENT but the source still exists".
  7. Docs: the README's "answers at once however much the workspace holds" doesn't hold on the fallback path.

No production contact, and nothing was merged by this run.

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