Skip to content

fix(fork): capture Firecracker memory and volumes in one paused window - #558

Open
nothiny wants to merge 1 commit into
kvcache-ai:mainfrom
nothiny:fix/firecracker-fork-volume-skew
Open

nothiny wants to merge 1 commit into
kvcache-ai:mainfrom
nothiny:fix/firecracker-fork-volume-skew

Conversation

@nothiny

@nothiny nothiny commented Nov 25, 2026

Copy link
Copy Markdown

Fixes #557

Approach

Fork now captures first and describes the children afterwards.

  • SandboxBackend::capture_fork returns a SandboxForkCapture that can spawn any number of children.
    • Firecracker does this with one pause_to_dir plus resume. pause_to_dir already restacks writable persistent volumes into their backings while the VM is paused, so memory, rootfs and volumes are consistent.
    • Firecracker's one-step fork reuses this path.
    • The default implementation returns a recoverable error, so other backends are unaffected.
  • ForkChildren::{Ready, AfterCapture} in the orchestrator.
    • AfterCapture carries a ForkChildPreparer. The order is capture, then the preparer clones volumes from the just-sealed backings, then spawn.
    • The preparer releases volumes of children that fail to start, and of all children if their IDs are rejected.
  • Fork API: sources with volumes go through VolumeForkPreparer. The separate snapshot_volume_mounts call before the fork is removed. Volume errors keep their 404/409/500 mapping.
  • Volume-less forks are unchanged and still use the one-step fork.

Tests

  • New mock-backed orchestrator tests:
    • capture → prepare → release order
    • a failed preparation leaves the source running with nothing reserved
    • invalid child IDs release everything that was prepared
    • a capture failure skips the preparer
  • cargo clippy --all-targets -D warnings and cargo test --lib -p agentenv (1071 passed) pass.

Not covered

No end-to-end run on a real Firecracker VM with volumes yet.

🤖 Generated with Claude Code

A full fork of a Firecracker sandbox with writable managed volumes sealed
the volumes first (snapshot_volume_mounts, then resume), cloned the
children's volumes from that seal, and only then paused again to capture
memory. A child therefore resumed memory from the second pause on top of
volumes from the first: writes in between were missing, and the guest's
page cache and filesystem metadata no longer matched the disk.

Fork now captures first and describes the children afterwards:

- SandboxBackend::capture_fork returns a SandboxForkCapture that can spawn
  any number of children. Firecracker implements it with a single pause:
  pause_to_dir already restacks every writable persistent volume into its
  backing while the VM is paused, so memory, rootfs and volumes come from
  the same instant. Firecracker's one-step fork reuses the same path.
- ForkChildren::AfterCapture hands the orchestrator a ForkChildPreparer.
  The orchestrator captures, lets the preparer clone the children's
  volumes from the backings the capture just sealed, then spawns the
  children. The preparer releases the volumes of children that fail.
- The fork API routes sources with volumes through a VolumeForkPreparer
  and no longer seals volumes ahead of the fork. Volume errors keep their
  status codes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

Full fork with writable volumes resumes memory and volumes from different instants (Firecracker)

1 participant