Skip to content

sandbox/v1 relay + mocactl: a client releases its sandbox's workspace at rm (part of #338) - #951

Merged
pdettori merged 6 commits into
rossoctl:mainfrom
webchang:pr/i338-release-relay
Nov 20, 2026
Merged

pdettori merged 6 commits into
rossoctl:mainfrom
webchang:pr/i338-release-relay

Conversation

@webchang

Copy link
Copy Markdown
Member

Summary

The client half of #338's release, per the answers on #338: a sandbox's client tells its worker the sandbox has ended, so the worker frees the workspace's standbys and disk at once instead of at idle reclaim. That is #879's standby booking, fixed for every client that follows the documented delete order, mocactl sbx rm included.

Stacked on #949 (the worker's Release frame). Review the commits after #949's; this PR's base will be main once #949 merges.

What changes

  • sandbox/v1: SandboxExec.Release(ReleaseRequest{sandbox_id}) → ReleaseResponse{outcome}, additive. The outcome is sandbox/v1: a Release frame frees a workspace key on the worker (part of #338) #949's ReleaseOutcome (RELEASED, KEPT_BUSY, UNKNOWN_KEY). Errors are gRPC statuses a client can act on: FAILED_PRECONDITION release_unsupported: … and INVALID_ARGUMENT (don't retry); RESOURCE_EXHAUSTED busy: …, DEADLINE_EXCEEDED and UNAVAILABLE (retry, or leave it to idle reclaim).
  • sandbox-relay:
    • Authorized as an Exec is, for sandbox-token callers only: sandbox_id must be the token's worker, and the frame carries the token's own workspace key, never one the caller names. The harness exec token is refused: it is unrestricted, and the control plane's release will come with its own release-only bearer (the next PR).
    • A ReleaseFrame goes only to a worker that advertises sbx:release-v1. An older worker drops the frame unread, so the relay answers release_unsupported without sending anything.
    • Every call ends: at the worker's ReleaseResult (by ctl_id), at a 15 s timeout, or when the worker goes away. At most 2 in flight per sandbox.
    • One audit line per call, marker sandbox_workspace_release (sandbox_release was taken by the drain grace).
  • sandbox-client: SandboxExecConn.release(), under the sandbox token with the lease calls' token-refusal retry, not counted as use.
  • mocactl: sbx rm releases every sandbox's workspace before its DELETE, after a lease sandbox's sandbox_deleted, best effort within the existing 15 s bound. Any error (including a worker that predates release) is ignored and the delete goes ahead. When the worker took the release but the DELETE then fails, rm says on stderr that the workspace was released and the sandbox still exists, and to re-run mocactl sbx rm.
  • docs: a "Releasing the workspace when you delete" section in the direct-API guide (its codes include UNIMPLEMENTED from a relay that predates Release), the client guide's sbx rm row, and the runbook's audit markers.

Tests

Test Before (#949's sources) After
release.transport.test.ts (10): the token's own key; each outcome; another worker's sandbox refused; harness and unauthenticated refused; release_unsupported without a frame for a worker without the capability; timeout; each worker error string → its status; no worker / worker gone; busy past 2 in flight fail pass
relay-release.test.ts (6): routeRelease's bookkeeping (a fresh ctl_id; no frame without the capability; a write that throws; abort; a lease answer with the same ctl_id doesn't settle a release; a worker that goes away) fail pass
exec-conn.transport.test.ts: release() under the sandbox token, activity: false, the release_unsupported mapping, an aborted signal sends nothing fail pass
sbx-cli.test.ts: sbx rm sends sandbox_deleted (a lease sandbox), then Release (every sandbox), then DELETE; a failing release still deletes; a failed DELETE after a taken release says to re-run rm fail pass

Suites: sandbox-relay 345, sandbox-client 272, mocactl 2182 (4 skipped), all pass; pnpm -r typecheck clean; go vet and go test ./... in remote-worker (two Go test fakes gain the generated Release method) and gen/go; buf lint and buf breaking against #949 pass; the stubs regenerate with no drift; the docs link check is clean.

Not in this PR

  • The control plane's release on sandbox end and session delete, with its release-only bearer (next).
  • The worker's startup sweep of unknown directories (after that).
  • The Go sbx client and the Python example client don't send Release yet; small follow-ups if wanted.

Questions

  1. The relay refuses Release from the harness exec token. The control plane's sender will need a new bearer kind on the relay (the next PR); is a separate listener-independent bearer list (like the exec token's) the shape you want, or a scope on the sandbox token?
  2. 2 in flight per sandbox and a 15 s timeout: a sandbox sends one release at its end, and the worker answers at once (its removal is off the request path). Enough?
  3. sbx rm releases before its DELETE, so a DELETE that fails leaves a sandbox that exists but takes no more commands for its workspace's idle period; rm says so and to re-run it. The alternative, for per-exec sandboxes: mint the token first, DELETE, then release with that token after a successful DELETE (a lease sandbox's sandbox_deleted still has to come first). Is the current order the one you want?
  4. The intended scope of a sandbox token's Release: it can release only its own sandbox's own workspace key. A token stays valid for up to one token lifetime (+30 s) after rm, so a late or leaked token can still call Release for that sandbox, whose key the delete already released; before a delete, a token holder can release its own live workspace, which refuses its Execs for the idle period. That's self-denial only, not a cross-tenant path; is that the scope you want?

Part of #338
Part of #879

Assisted-By: Claude Code

…h tiers

The worker half of rossoctl#338's explicit release. pdettori's answer on rossoctl#338: a dedicated
Release frame (not the lease op sandbox_deleted), with an ack from the worker; keep
the busy rule but free the workspace when the Exec ends; refuse new Execs for a
released key.

- proto (additive): ServerFrame.release = ReleaseFrame{ctl_id, workspace_key};
  WorkerFrame.release_result = ReleaseResult{ctl_id, outcome, error};
  ReleaseOutcome RELEASED | KEPT_BUSY | UNKNOWN_KEY. A worker that serves it
  advertises sbx:release-v1 in Hello (both tiers). The comments give the error
  forms a sender acts on and the sender contract (only at the owner's terminal
  end: the harness's workspace_key is the session id). Stubs regenerated (buf).
- session: a Releaser runner interface; Release runs off the receive loop like
  LeaseControl (releaseCap 32, busy past it) and is acked as release_result; a
  runner without it answers release_unsupported.
- vmpool: Pool.Release frees the run's standbys and workspace (Reclaim's steps,
  under one lock hold) and marks the key released in the same critical section.
  An Exec racing it either already uses the run (kept_busy) or is refused, at
  ExecPhased's early check or where it would create the run again (runAtLocked),
  so it cannot re-create a released run. A kept_busy run gets no new standby
  (scheduleReplenishLocked, replenishOne; a warming in flight destroys its VM,
  and one that fails frees the run at once),
  and its workspace is freed when it is no longer busy (the Exec's own unwind,
  a no-lock check when no release is pending; or the next sweep after a lease
  ends), not at WorkspaceIdle. Destroys and tree removal go to the reclaim
  goroutine (inline after the lock is released if its queue is full, as the
  sweep's are). An Exec for a released key is refused "released: ..."
  (RefuseReleased) for WorkspaceIdle; the set is pruned by the sweep. Releases
  are counted by outcome in Stats.Releases.
- container tier (WorkspaceRunner): the same outcomes and refusal; the removal
  after a busy command runs off the Exec's goroutine; tombstone naming shared
  with Sweep; a key this process has not run is unknown_key and its directory
  is left alone (Root is also the shared workspace); a symlinked session
  directory loses the link, never its target.
- Pool.Reclaim's and the sweep's comments no longer say reclaiming "never"
  costs data (rossoctl#338).
- The P4 spec's §4.4 and §9 note the worker half.

Part of rossoctl#338
Part of rossoctl#879

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
Four manual-only gates for rossoctl#338's worker-side Release, on the lease-pool gate's
real Firecracker launcher (pooled VM cgroups under a gate-private parent whose
cleanup reports a leaked VM), with the sweep and idle timers set an hour out so
only a Release, or an Exec's unwind after one, can free anything:

- TestGateReleaseOfAnIdleRunFreesItsVMsAndWorkspace: both standbys' jail,
  sockets, cgroup and VMM, and the workspace and its image, gone within 10 s;
  Stats ActiveRuns 0, StandbysResident 0, Releases[released] 1.
- TestGateReleaseDuringAnExecIsKeptBusyThenFreed: kept_busy with a command
  running; the standbys go, the workspace and the running VM stay, the command
  ends normally (exit 0, "done"), then its unwind frees the VM and workspace.
- TestGateExecAfterReleaseIsRefused: Execs after a release (and on an unknown
  key released first) are refused released, restore no VM and re-create no
  workspace; Refusals[released] 2.
- TestGateKeptBusyReleaseRestoresNoStandby (review M1 on real VMs): A running,
  B queued on the run's gate, a kept_busy release; StandbysResident stays 0 and
  restores grow by at most B's own, through 5 x ReplenishDelay after A ends.

Each starts with requireKVM and is listed in manualOnlyGates; without /dev/kvm
they skip.

Part of rossoctl#338

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
… empty

On bm1 two Release gates failed only on "lease cgroup .../pool-0 still present":
a per-exec VM's cgroup is a POOLED slot that cgroupPool returns to the idle pool,
empty but kept, unlike a lease's retired cgroup, so requireLeaseVMGone's "cgroup
absent" check doesn't apply to standbys. requirePooledVMGone keeps the VMM-pid,
jail and socket checks and requires the cgroup empty (cgroup.procs has no pid)
or absent, naming the leftover cgroup's pids when it fails.

Part of rossoctl#338

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
…kspace at its end

The client half of rossoctl#338's release, on the worker Release frame (stacked on rossoctl#949):

- sandbox/v1: SandboxExec.Release(ReleaseRequest{sandbox_id}) -> ReleaseResponse{outcome},
  additive. Its errors are gRPC statuses a client can act on (release_unsupported and a refused
  key: don't retry; busy, a timeout, unavailable: retry; idle reclaim stays the backstop).
- sandbox-relay: authorized as an Exec is, for sandbox-token callers only: the token's worker,
  and the token's own workspace key, never one the caller names. The harness exec token is
  refused (it is unrestricted; the control plane's release comes with its own bearer). A
  ReleaseFrame goes only to a worker that advertises sbx:release-v1 (an older one drops it
  unread: FAILED_PRECONDITION release_unsupported, nothing sent); every call ends at the
  worker's answer, a 15 s timeout, or the worker going away; at most 2 in flight per sandbox.
  One audit line per call, marker sandbox_workspace_release.
- sandbox-client: SandboxExecConn.release(), under the sandbox token with the lease calls'
  token-refusal retry, not counted as use.
- mocactl: sbx rm releases every sandbox's workspace before its DELETE (after a lease
  sandbox's sandbox_deleted), best effort within the existing 15 s bound; the token for it is
  minted with activity false.
- docs: the direct-API guide's Release section, the client guide's sbx rm row, the runbook's
  audit markers.

Part of rossoctl#338
Part of rossoctl#879

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
`sbx rm` keeps its order, Release then DELETE. A workspace the worker
took the release for (released or kept_busy) takes no more Execs for its
idle period, so when the DELETE then fails, or Ctrl-C stops rm before it,
rm now says on stderr that the workspace was released and the sandbox
still exists, and to re-run `mocactl sbx rm`. Nothing changes when the
worker held no workspace (unknown_key) or the release failed.

direct-api.md: the codes table adds UNIMPLEMENTED (a relay that predates
Release; nothing was sent), the refusal of later Execs is bounded by the
workspace's idle period, and a client whose DELETE fails after a release
retries the delete.

Part of rossoctl#338
Part of rossoctl#879

Assisted-By: Claude Code
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.

Review — sandbox/v1 relay + mocactl: a client releases its sandbox's workspace at rm (part of #338)

This is the sender half of #949's frame, and it is closed end to end: the worker advertises, the relay gates on it, the client calls it, and sbx rm sends it.

  • Authorization is the Exec's. The relay accepts only a sandbox token (execCaller), refuses the harness token explicitly (HARNESS_NO_RELEASE) and any other caller as unauthenticated, and sandboxRelease requires sandbox_id === claims.wrk and always releases claims.wsk — a caller cannot name another sandbox, another workspace, or a workload sandbox. Each refusal has a test.
  • The worker's answer is not trusted blindly. Error strings map by prefix (release_unsupported/invalid key → don't retry, busy → retry later, anything else → UNAVAILABLE), an unknown outcome with no error is reported rather than guessed, and the capability check happens before a frame is written, so a pre-#338 worker is a clean FAILED_PRECONDITION with nothing sent.
  • Bookkeeping is the lease path's. routeRelease takes a fresh ctl_id, removes its waiter, timer and listener on every exit, fails its waiters when the worker goes away, and leaves a late or duplicate answer with no waiter; the audit line (sandbox_workspace_release, distinct from the drain grace's sandbox_release) carries the outcome, and the client's 30 s backstop sits outside the relay's 15 s.
  • sbx rm now opens the worker connection for every sandbox (per-exec included) with a no-activity token — deleting is not use — ends a lease first, then releases, all inside the existing 15 s bound; any failure is ignored and the DELETE decides. When the worker took the release and the DELETE then fails, it says the sandbox still exists and re-running rm finishes: the one case a user could otherwise be surprised by, since the workspace now refuses Execs.
  • Docs put the contract where a direct-API client will look (the Release section with the status table, and the client-guide row), and the runbook's audit-line list gains the new marker.

One thing to watch when merging: the spec update (from #949) still ends "Who sends it (the relay, on a sandbox's or session's end) is #338's follow-up work". With this PR the relay's sender exists for a client's call; only the automatic sender at a session's or sandbox's own end is left — worth half a sentence in whichever of the two merges last.

The inline comment is a corner of the interrupted path.

Checks 18/18 green. Author: webchang (MEMBER — maintainer). Six commits, one a merge of #949's branch, signed off.

const released = await endOnWorker(c, id, o.factory);
if (c.signal?.aborted) {
c.io.err(`sbx: interrupted (exit ${SBX_EXIT.interrupted}): nothing was deleted`);
if (released) c.io.err(releasedNotDeleted(id));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion. released is true only when the release answered, but a Ctrl-C during the release can leave the worker holding it: the relay writes the ReleaseFrame before it waits, and cancelling the call afterwards only drops the answer (routeRelease removes its waiter; the frame is already on the stream). In that window this branch prints only "nothing was deleted", which is true, and skips the warning — yet the next sbx run is refused released: … until the sandbox is deleted. A conservative line when the connection was opened and the release had started ("if its workspace was released, the sandbox takes no more commands until you delete it") would cover it; or endOnWorker could report "attempted" beside "took".

@pdettori
pdettori merged commit 6568144 into rossoctl:main Nov 20, 2026
18 checks passed
@pdettori pdettori added this to the v0.11.0 milestone Nov 20, 2026
webchang added a commit to webchang/moca that referenced this pull request Nov 20, 2026
Carries rossoctl#941-rossoctl#946, rossoctl#948, rossoctl#950 and rossoctl#338's rossoctl#949/rossoctl#951 (API 1.33.0).
One conflict: docs/operate/sandbox-service/install.md's settings
table; upstream's table kept (its rossoctl#945 SBX_STORAGE_CLASS wording,
the OIDC and proxy-hop rows) plus the fork's SBX_GATE_RECORD_SCHEMA
row. The fork-only set is unchanged: the same 30 files.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
webchang added a commit that referenced this pull request Nov 20, 2026
…it got no answer

Follow-up to #951 (#338): a release that ends without an answer (Ctrl-C during the call, the 15 s
bound, an unavailable relay) may still have reached the worker, because the relay writes the
ReleaseFrame before it waits and a cancel only drops the answer. rm used to stay silent then, though
the sandbox's next command would be refused `released:` until it is deleted. endOnWorker now tells
`took` (released or kept_busy), `unknown` (cancelled, timeout or unavailable) and `none` apart; on an
interrupted rm or a failed DELETE, `unknown` prints "may have been released on its worker … re-run
mocactl sbx rm", and a refused or unsupported release still says nothing.

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
webchang added a commit that referenced this pull request Nov 20, 2026
mocactl sbx rm: a release with no answer says it may have reached the worker (follow-up to #951)
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.

3 participants