Repository navigation
fix(vmpool): run the wrapped command in a subshell so its exit cannot skip the sync - #562
Conversation
… skip the sync The Firecracker wrapCommand ran the command in a brace group and appended sync afterwards. A command ending in its own exit — the shape of every writeFileCommand-based put (packages/sandbox-client's put/edit scripts end exit 0/exit 1) — terminated the wrapper shell before the sync ran. The VM is SIGKILLed as soon as the command's shell is gone, so the guest page cache, still holding the just-written file, died with it: puts vanished while plain writes without exit survived. Caught live by the SBX0 smoke (2026-10-20); the KVM write-durability gate never caught it because its own command contained no exit. Run the command in a subshell instead: exit ends the subshell, the sync after it always runs, and (exit $__fc_rc) still reports the command's own status. The guest agent's parked-shell path sources this script inside its own ( . file ) subshell, so this covers both exec paths. - new non-KVM gate TestWrapCommandSyncSurvivesCommandExit: bash -x trace must show the sync after a command that calls exit (failed pre-fix) - TestGateWriteDurability's Exec N now ends in exit 0 so the KVM gate covers this class end-to-end Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Paolo Dettori <[email protected]>
ingpaolodettori-dev
left a comment
There was a problem hiding this comment.
Verdict: APPROVE
Reviewed f5bf784 against main dc1c25f (base is current, 0 commits behind). CI: all 15 checks green, including microvm-gates. DCO signed. No .claude/ or .vscode/ changes.
The fix is correct. I checked the wrapper semantics in bash with both the old brace group and the new subshell, run both as a plain script (fresh-child / bash -c shape) and sourced inside ( . file ) </dev/null (the guest agent's parked-shell shape, parkedShellLine, agent.go:422):
| command | { } reaches sync |
( ) reaches sync |
rc preserved |
|---|---|---|---|
echo hi; exit 3 |
no | yes | 3 |
set -e; false |
no | yes | 1 |
exec true |
no | yes | 0 |
trap 'echo T' EXIT; exit 2 |
no | yes (trap fires first) | 2 |
return 4 (parked path) |
no | yes | 4 |
kill -9 $BASHPID |
no | yes | 137 |
So the subshell also closes the set -e and exec variants of the same bug, not just exit; the PR body could mention those. Nothing after the group depended on state the command sets (only sync and the rc follow), so moving the command into a subshell loses nothing. The stdin path (runFreshChild, bash -c req.Command) gets the wrapped command too, so both guest-agent paths are covered, as the PR says. Leaving chvWrapCommand alone is right: nothing after its brace group is load-bearing.
The new test runs under the regular go test -race ./... in check, not the microvm-gates job (its name doesn't match TestGate*). That's fine, since it doesn't need a hypervisor.
Non-blocking: docs/notes/2026-09-23-307-deferred-reap-design.md:118 still quotes the brace-group wrapper. It's a dated design note, so leaving it is defensible.
One nit inline.
| t.Fatalf("wrapper exited %d, want %d (the command's own code)", code, tc.want) | ||
| } | ||
| trace := stderr.String() | ||
| if !strings.Contains(trace, "sync") { |
There was a problem hiding this comment.
nit: strings.Contains(trace, "sync") would also match any traced line that merely contains the word, e.g. a future case whose command is echo sync; exit 0, which would pass against the broken brace-group wrapper too. Matching the xtrace line itself (e.g. regexp.MustCompile((?m)^++ sync$)) pins that the sync command ran.
… OpenCode gap named, the legend matched to the rows Per review on rossoctl#565: name the exact commits the live acceptance used (image of main at dc1c25f, worker at f5bf784 = rossoctl#562, step-5 harness at 6aedba0 = rossoctl#564), point every Verified-live row at section 4, state that the pending OpenCode half is an accepted known gap that does not block the cohort, and rename the Live (Task 10) legend entry to Verified live now that no row is pending. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Paolo Dettori <[email protected]>
The 1c gate's live rows, verified 2026-10-20 against the deployed namespace (spec 10.6): the smoke's steps 1-4 and 6 green in the full run, step 5's Claude Code half green in a re-run of that step after the smoke harness fix (PR #564; the change touches only the claude invocation), and the hand checks - the deployed sandbox profile from outside, a tokenless Exec and a tokenless attach, and the guest's absent network - done at the same run. Step 5's OpenCode half is still pending (not installed on the acceptance machine). The run caught one real defect, fixed and merged in PR #562 with a new regression test. Assisted-By: Claude (Anthropic AI) <[email protected]> Signed-off-by: Paolo Dettori <[email protected]>
What
One-character-class fix in the Firecracker microVM launcher: the wrapped command now runs in a subshell instead of a brace group, so a command that ends in its own
exitcan no longer skip the wrapper'ssync.Root cause
wrapCommand(remote-worker/internal/vmpool/launcher_firecracker.go) appendssyncafter the command because the guest page cache dies with the VM — the VM is SIGKILLed as soon as the command's shell is gone. But the command ran in a{ … }brace group, so a command ending inexitterminated the whole wrapper shell before the sync ran. The SIGKILL then discarded the still-dirty page cache, and with it every write the command had made.Every
writeFileCommand-based put —packages/sandbox-client/src/tools.ts, the canonical client write path behindmocactl sbx put/editand the MCP write tools — ends its script withexit 0/exit 1. So every put silently vanished, while plain writes whose commands contained noexitsurvived. That asymmetry is what made it look like a routing/storage bug rather than a shell-semantics bug.The guest agent's parked-shell path (
runOnParkedShell) sources the same wrapped script inside its own( . file )subshell, so the brace group was the only unprotected nesting on either exec path; the subshell fix covers both.Evidence (live, 2026-10-20)
Minimal pair against a deployed worker, same sandbox, scripts on stdin:
exit 0→ file written by it absent in the next Exec (and absent fromworkspace.imgitself, via debugfs)bash -xon the pre-fix wrapper shows the trace ending at the command'sexitwith nosyncline — reproduced exactly by the new unit test.The KVM write-durability gate (
TestGateWriteDurability, spec §8) never caught this because its own command contained noexit; it is strengthened here to end inexit 0so the gate covers the real client write shape end-to-end.Fix
exitnow ends the subshell only; thesyncafter it always runs;(exit $__fc_rc)still reports the command's own status. The Cloud Hypervisor arm is deliberately untouched:chvWrapCommandhas no sync by design (virtio-fs writes land on the host filesystem directly), and nothing after its brace group is load-bearing.Host-binary-only change — no guest agent or golden-snapshot rebuild needed.
Tests
TestWrapCommandSyncSurvivesCommandExit: runs the generated wrapper underbash -xand asserts the xtrace shows thesyncafter a command that callsexit(bothexit 0andexit 7), and that the command's own exit code is preserved. Failed against the brace-group wrapper; passes with the subshell. Runs in CI without KVM.TestGateWriteDurability(KVM-gated) now ends Exec N inexit 0.go test ./internal/vmpool/green; also verified on the rig.Live verification
On the SBX0 acceptance rig: worker rebuilt from this commit, minimal pair above now persists both writes, and a real
mocactl sbx putround-trips. FullSBX_LIVE_SMOKE=1 deploy/sbx/smoke.shre-run against the fixed worker: steps 1–4 and 6 green (step 5's failure was an unrelated smoke-harness bug — headless claude ignored the workspace allow in an untrusted repo — fixed separately in the smoke, green on re-run; the harness fix does not touch any other step).🤖 Generated with Claude Code