Skip to content

P4 microVM tier: per-Exec VM lifetime is finer-grained than the design needs — worth a separate architecture pass #274

Closed as not planned

Description

@pdettori

Summary

vmpool.Exec destroys the VM after every single Exec and never reuses one (P4 §4.1: "runs exactly one command, and destroys the VM before returning. A VM is NEVER reused" — docs/specs/2026-09-09-p4-microvm-sandbox-design.md:304). Since one turn issues many Execs — P4 §3.2 cites operations.ts:78-79, where one file edit alone is two back-to-back Execs with no model round-trip between them — the isolation unit here is the individual command, not the run and not the turn. That's a materially stronger (and more expensive) guarantee than "isolate one run from another," and it's worth asking whether it's the guarantee we actually need.

Why this is deliberate, not an oversight

The design states the rationale plainly, so this isn't a bug report:

Citation What it says
§3.2 "This preserves the per-call property exactly — each VM still serves one Exec and dies — while moving creation off the hot path"
§4.1 / §6 "each VM carries its workspace_key and Exec asserts the popped VM matches. Cheap assertion, catastrophic bug [if it ever fails]"
§8 "No VM reuse: The workspace_key assertion holds under concurrency" is a blocking correctness gate

Per-call destruction bounds the blast radius of any one command's compromise to that command alone, and makes "did this Exec land in the right workspace" a trivial per-VM assertion instead of a stateful one. That's a real security property, not a cost accident.

Why it's worth revisiting anyway

The cost side is exactly what E10/E11 measure: replenishment rate tracks Exec rate 1:1, and that's the dominant term in §7.3's density numbers. A coarser VM lifetime — one VM per turn, or one VM per run — would cut replenishment volume by the average Execs-per-turn (or Execs-per-run) factor, at the cost of a weaker isolation claim ("every turn/run runs in a disposable VM" instead of "every command does"). Whether that trade is worth taking is a product/security call, not an implementation detail, and it should be made explicitly rather than inherited from §3.2's per-call default.

What closing it would involve (not decided here)

  1. Keep per-Exec VMs. Status quo, already measured, tightest blast-radius/assertion story.
  2. Per-turn VM lifetime. One VM serves every Exec a single session.prompt() call issues. Needs the workspace_key assertion (§3.4/§6) redefined against a longer-lived VM, and needs E10/E11 re-measured — the D-standby replenishment model (§3.2, §4.2) is sized around "requests separated by hundreds of ms," which doesn't apply once a VM stops popping mid-turn.
  3. Per-run VM lifetime. Coarser still; changes the isolation claim from per-command to per-run and needs its own threat-model writeup.

Relationship to #267

Separable but adjacent: #267 is about how long the workspace (filesystem) persists; this is about how long the VM process persists. A session-scoped workspace (#267) combined with per-run VM reuse would look different from a session-scoped workspace with per-Exec VMs — worth keeping in mind when either gets designed, but they're independent decisions.

Activity

  1. pdettori commented on Sep 24, 2026

    @pdettori
    MemberAuthor

    Design constraint to settle first: which lifetime unit, and the three timers it sits between

    Adding this before anyone starts, because the campaign has now established both the price of this change and a constraint on its shape that is easy to discover too late.

    This is now the only remaining lever, not the best one

    Every change that reschedules restores has been measured and rejected: more slots (peak is 64, 128 and 256 are worse), StandbyDepth=8 (−20–30%), ReplenishDelay (inside session drift), scale-out to N workers (1.00x), jail pooling (−9.24%), and deferring the teardown (#336 — works, 1.70x on the gate, but only until supply binds). The structural reason is one sentence:

    Every Exec needs exactly one restore, so throughput <= restore rate. A warm standby changes when the restore is paid, never whether.

    So a VM serving n Execs is the only thing left that changes the ratio. Priced from #307 and #337: ~19 ms of unavoidable RCU grace-period latency in kvm_vm_release per VM destroyed, plus ~14 ms of restore work, plus ~35 ms of Resume — of which 78–81% is a guest ext4 mount that #337 showed is CPU-bound in a vcpu_count: 1 guest and not tunable. Against 4.5 ms for the command itself. A session-scoped VM divides the first three by n and pays the mount once.

    The constraint: do not introduce a third lifetime

    Three lifetimes already exist, and the session owns two of them:

    unit lifetime governed by
    Exec one command a VM created for it and destroyed after it
    session standby pool StandbyIdle = 90 s — drop standbys, keep the workspace
    session workspace WorkspaceIdle = 30 min — delete the workspace too

    workspace_key is the session id (select-sandbox.ts), so the session — not the run, not the Exec — is already the isolation unit that owns both the workspace and the standby pool. A longer-lived VM should be scoped to the session rather than to some new interval, or the tier acquires a fourth lifetime to reason about and reclaim.

    Two consequences worth deciding rather than discovering

    1. It sharpens the execGate constraint #336 just relaxed. execGate is capacity 1 per key, i.e. per session, because the workspace ext4 is not a shared-disk filesystem and two live guests mounting it rw would corrupt it. #336 relaxed when the gate opens — at an /proc/<pid>/fd-empty barrier rather than after the full reap — on the argument that a process with no descriptors cannot write to workspace.img. A VM that survives its Exec holds that image across more of the turn, so the barrier argument has to be re-made for the new shape. It does not obviously carry over, and it is the correctness property of the tier.

    2. It interacts with #338. The workspace TTL is already too short for HITL sessions, and a session-scoped VM makes that question sharper: if the VM outlives an Exec, what happens to it across a multi-hour human pause? Holding a VM there is expensive (it is memory, and memory is admission-controlled); dropping it and keeping only the workspace is what the current two-stage policy already does. Whatever #274 decides, it should say which of the three timers governs a surviving VM.

    What the design note should answer

    1. What is the lifetime unit — the session, and if not, why a fourth one is justified.
    2. How is the execGate invariant preserved when a VM outlives its Exec. This is the isolation claim; everything else is performance.
    3. What bounds it — Execs per VM, wall-clock, or an idle timer — and how that composes with StandbyIdle and WorkspaceIdle.
    4. What happens on a long idle gap, per P4 microVM tier: WorkspaceIdle=30min deletes a HITL session's workspace mid-session, silently #338.
    5. What resets between Execs on one VM. Today the guarantee is total: fresh VM, tmpfs /tmp and /var discarded. A reused VM keeps process state, /tmp, environment and installed packages — which is a behaviour change visible to users, not only a performance change, and arguably the biggest thing in this issue.
    6. Expected value, measured as throughput. feat(vmpool): defer the VM reap off the execGate path — 1.70x gate-held, 1.12x end to end (#307) #336 predicted 1.4–1.5x and delivered 1.12x because the estimate divided by a pipeline ceiling production cannot draw on. Expect the same trap here: the gain is bounded by whatever binds after restores stop dominating.

    Method note

    Measure throughput, not latency, and report p95 alongside. #332's jail pool improved latency 1.6 ms and lost 9.24%; #337's clean-unmount arm improved mount_us 2.86% and lost 4.38% throughput. Two component wins in this campaign have reversed under load, so a latency win with no throughput win is a rejection to record.

    Refs #307, #336, #337, #338, #332

  2. pdettori commented on Nov 16, 2026

    @pdettori
    MemberAuthor

    Closing as decided. The ADR-0035 revision of 2026-10-08 settles this issue against reusing a VM across client Execs: one fresh VM per Exec is the tier's only mode, and read coalescing is the one gated exception. The 2026-11-08 note confirms the decision still stands for per-Exec mode.

    The coarser lifetime this issue asked about now exists as an opt-in: SBX lease mode (ADR-0048; #802, #808, #814).

  3. added this to the v0.11.0 milestone on Nov 17, 2026
  4. added a commit that references this issue on Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions