Skip to content

remote-worker: a worker sweeps leftover workspaces at start (part of #338) - #954

Merged
webchang merged 2 commits into
rossoctl:mainfrom
webchang:pr/i338-startup-sweep
Nov 20, 2026
Merged

webchang merged 2 commits into
rossoctl:mainfrom
webchang:pr/i338-startup-sweep

Conversation

@webchang

Copy link
Copy Markdown
Member

Summary

A restarted worker starts with an empty runs map, so its idle sweep never reaches a workspace the previous process left; on a host with no janitor (deploy/microvm), those stayed on disk for good. This is the startup sweep from the #338 design note, approved alongside the release frame (#949, #951).

What changes

  • microVM tier (vmpool.New): before anything is served, one sweep of WorkspaceRoot:
    • every .reclaiming-* tombstone is removed: at startup none is this process's;
    • every directory named as a workspace key, owned by no run, whose newest mtime (the directory's or its workspace.img's) is older than WorkspaceIdle is renamed to a tombstone under p.mu and removed on the reclaim goroutine, as the idle sweep does. This is the host janitor's rule for an unknown directory (deploy/sbx/janitor.sh). The key's lease state is retired as the idle sweep retires it;
    • younger directories are kept (a session may be between turns), and entries no key can name (.lease-state, _probe) are left alone;
    • a symlink named as a key is removed as the link: its target survives, and its age is the link's own.
    • It logs a count when it frees anything.
  • Container tier (cmd/worker): removes tombstones only at startup. Its root doubles as the shared workspace (an empty-key session's git clone sits beside the keys' directories), so a name alone cannot tell a key's workspace from shared content; it still never adopts an unknown directory.
  • Docs: the P4 page's "Workspace lifetime" section, and the P4 spec's P4 microVM tier: WorkspaceIdle=30min deletes a HITL session's workspace mid-session, silently #338 update.

Tests

Test Without the change With it
TestStartupSweepFreesAnOldUnknownWorkspace fails: the workspace stays passes
TestStartupSweepRemovesAnOldSymlinkNotItsTarget fails: the link stays passes; the target's file survives
TestStartupSweepRemovesTombstonesAndKeepsNonKeys fails: the tombstone stays passes; _probe and .lease-state kept
TestStartupSweepKeepsAYoungWorkspace passes passes (incl. an old directory whose image is recent)
TestStartupSweepKeepsAKnownRunsWorkspace n/a (calls the sweep directly) passes
TestStartupSweepRemovesTombstonesOnly (container) fails: removed 0 passes; unknown and shared directories kept, a symlinked tombstone's target survives

go vet ./... is clean; go test ./... passes (15 packages); -race passes on internal/vmpool and internal/exec.

Known interactions

  • With the host janitor on sandbox-service hosts (deploy/sbx): a worker that restarts while the janitor is removing a workspace can remove the janitor's own .reclaiming-<wsk>-<s> tombstone first. RemoveAll tolerates the race, so nothing fails, but the janitor's remove_tree then fails its directory re-check (remove_tree in deploy/sbx/janitor.sh) and prints one "not a directory inside" line to stderr. That line is noise from the race, not a failure; nothing is left behind.
  • A read-only session: a session that only reads for longer than SH_WORKSPACE_IDLE keeps an old mtime, so a restart at that moment frees its workspace. The janitor already applies the same rule on sandbox-service hosts, so this is new behavior only on deploy/microvm hosts, which have no janitor. The idle sweep of a running worker is unchanged (it judges by the run's last Exec, not by mtime).

A question

No setting turns the sweep off: its threshold is SH_WORKSPACE_IDLE, already configurable, and it never touches anything younger. Say if you'd want an off switch anyway.

Part of #338

Assisted-By: Claude Code

…ss left

A restarted worker's runs map starts empty, so its idle sweep never reached a
workspace the previous process left, and on a host with no janitor
(deploy/microvm) those stayed on disk for good (rossoctl#338's restart leak).

- microVM tier: vmpool.New sweeps WorkspaceRoot once before serving. It
  removes every .reclaiming-* tombstone, and every key-named directory no run
  owns whose newest mtime (the directory's or its workspace.img's) is older
  than WorkspaceIdle: the host janitor's rule for an unknown directory. The
  rename happens under p.mu, the delete on the reclaim goroutine; a symlink
  is removed as the link itself; the key's lease state is retired as the idle
  sweep does; entries no key can name (.lease-state, _probe) are left alone.
- Container tier: the worker removes tombstones only at startup. Its root is
  also the shared workspace, so a name alone cannot tell a key's workspace
  from shared content, and it still never adopts an unknown directory.
- Docs: the P4 page's workspace-lifetime section and the P4 spec's rossoctl#338 note.

Part of rossoctl#338

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
@webchang
webchang requested a review from pdettori as a code owner November 20, 2026 13:44
@pdettori pdettori added the ready-for-ai-review Request automated AI code review from clawgenti label Nov 20, 2026
@pdettori pdettori added this to the v0.11.1 milestone Nov 20, 2026
@pdettori pdettori added the bug Something isn't working label Nov 20, 2026

@ingpaolodettori-dev ingpaolodettori-dev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review — remote-worker: a worker sweeps leftover workspaces at start (part of #338)

The leak is real and the shape is right: a restarted worker's runs map is empty, so nothing ever reached the previous process's directories, and on a host with no janitor they stayed for good.

  • Order in New is correct: Normalize (so WorkspaceIdle is the 30-minute default, never zero), then markLeaseBoot, then endLeasesOpenAtStart, then the sweep — and reclaimLoop is already running, so the detach happens under p.mu and the recursive deletes run off the constructor, as the idle sweep's do.
  • The rule is the janitor's for an unknown directory: a name checkKey accepts, in no run here, whose newer mtime — the directory's or its workspace.img's — is older than WorkspaceIdle. workspace.img matters because the guest's writes land in the image, not the directory. A younger one is kept, which is the case that would otherwise eat a session between turns.
  • What cannot be adopted is left alone: checkKey refuses .lease-state, _probe and anything else not a key, live runs are skipped, and the container tier removes tombstones only — its root doubles as the shared workspace, so a name alone cannot tell a key from shared content. That asymmetry is the right call and both docs say so.
  • Lease state: retireLeaseState with no state file is removeLeaseState, a no-op when the file is absent, so calling it unconditionally is safe on a lease-off pool; when the file exists the mode and epoch outlive the tree, as the idle sweep has it.
  • A symlink is judged by the link's own mtime and removed as the link (Lstat, rename, RemoveAll never follows) — tested, target intact. Files: newestWorkspaceMtime also accepts a non-directory named as a key, so an old plain file at the root would be removed too.

One optional note, not blocking: that last case is the one place the "the janitor's rule" claim is a little broader than the janitor, which leaves every non-directory alone ("not a directory (symlink or file); left alone"). The symlink half is deliberate and documented; if the plain-file half is too, a clause in the comment would keep the comparison exact — or require IsDir and skip files, which costs nothing since the worker only ever creates directories.

Tests cover the sweep and its edges well: old, young, old-with-recent-image, a live run's workspace, an old symlink, tombstones, and _probe/.lease-state kept. Checks 18/18 green. Author: webchang (MEMBER — maintainer). One commit, signed off.

Keeps both tests in workspace_release_test.go: the startup sweep's and rossoctl#953's failed-rename Release.

Signed-off-by: Rong Chang <[email protected]>

@ingpaolodettori-dev ingpaolodettori-dev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up review — head moved to 8b1c4c72 (merge of main only)

Nothing of the PR's own changed since the APPROVE at e8be2aed. git log --no-merges origin/main..refs/pr/954 is still the single commit e8be2aed; this head adds one merge of main (39aa27ed, through #957).

  • Merge resolution: git show --remerge-diff 8b1c4c72 reports one conflict, in remote-worker/internal/exec/workspace_release_test.go. It is resolved the right way — both tests are kept, this PR's TestStartupSweepRemovesTombstonesOnly and #953's TestReleaseWhoseDetachFailsAnswersAnErrorNotReleased, with the closing braces landing on the right functions and no assertion dropped. Nothing else was resolved silently, so there is no evil merge.
  • State: mergeable CLEAN, 18/18 checks green at this head.

Still open from the first round, non-blocking and unchanged: the sweep also removes an old plain file named as a key, while the janitor leaves every non-directory alone. The symlink half is deliberate and documented; if the plain-file half is too, one clause in the comment keeps the "the janitor's rule" comparison exact.

This approval is a formality — the repo does not dismiss stale approvals, so e8be2aed still governs. Author: webchang (MEMBER — maintainer).

@webchang
webchang merged commit 80008da into rossoctl:main Nov 20, 2026
18 checks passed
webchang added a commit that referenced this pull request Nov 23, 2026
…the janitor does

newestWorkspaceMtime judged a plain file by its own mtime, so an old file whose name
checkKey accepts was detached and removed at startup. The host janitor leaves every
non-directory alone, and no workspace is ever a plain file: the sweep now keeps it. A
symlink named as a key is still removed as the link, as documented. Follow-up to #954's
review note.

Signed-off-by: Rong Chang <[email protected]>
Assisted-By: Claude (Anthropic AI) <[email protected]>
webchang added a commit that referenced this pull request Nov 23, 2026
remote-worker: the startup sweep keeps a plain file named as a key, as the janitor does (follow-up to #954)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ready-for-ai-review Request automated AI code review from clawgenti

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants