Skip to content

fix(vmpool): run the wrapped command in a subshell so its exit cannot skip the sync - #562

Merged
pdettori merged 1 commit into
rossoctl:mainfrom
pdettori:fix/sbx-sync-exit
Oct 20, 2026
Merged

pdettori merged 1 commit into
rossoctl:mainfrom
pdettori:fix/sbx-sync-exit

Conversation

@pdettori

@pdettori pdettori commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

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 exit can no longer skip the wrapper's sync.

Root cause

wrapCommand (remote-worker/internal/vmpool/launcher_firecracker.go) appends sync after 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 in exit terminated 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 behind mocactl sbx put/edit and the MCP write tools — ends its script with exit 0/exit 1. So every put silently vanished, while plain writes whose commands contained no exit survived. 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:

  • script ending exit 0 → file written by it absent in the next Exec (and absent from workspace.img itself, via debugfs)
  • same script falling off the end → file present

bash -x on the pre-fix wrapper shows the trace ending at the command's exit with no sync line — reproduced exactly by the new unit test.

The KVM write-durability gate (TestGateWriteDurability, spec §8) never caught this because its own command contained no exit; it is strengthened here to end in exit 0 so the gate covers the real client write shape end-to-end.

Fix

// before
"cd /workspace\n{ " + cmd + "\n}\n__fc_rc=$?\nsync\n(exit $__fc_rc)\n"
// after
"cd /workspace\n( " + cmd + "\n)\n__fc_rc=$?\nsync\n(exit $__fc_rc)\n"

exit now ends the subshell only; the sync after it always runs; (exit $__fc_rc) still reports the command's own status. The Cloud Hypervisor arm is deliberately untouched: chvWrapCommand has 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

  • New non-KVM gate TestWrapCommandSyncSurvivesCommandExit: runs the generated wrapper under bash -x and asserts the xtrace shows the sync after a command that calls exit (both exit 0 and exit 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 in exit 0.
  • Full 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 put round-trips. Full SBX_LIVE_SMOKE=1 deploy/sbx/smoke.sh re-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

… 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 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.

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") {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

@pdettori
pdettori merged commit 920fa88 into rossoctl:main Oct 20, 2026
15 checks passed
@pdettori
pdettori deleted the fix/sbx-sync-exit branch October 20, 2026 21:03
pdettori added a commit to pdettori/moca that referenced this pull request Oct 20, 2026
… 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]>
pdettori added a commit that referenced this pull request Oct 21, 2026
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]>
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.

2 participants