fix(vmm): keep stopped VMs' CIDs reserved across a reload - #996
Merged
Conversation
reload_vms_sync() clears the CID pool and re-occupies it from the supervisor's running processes only. A stopped VM is absent from that list but still records its CID in vm.config.cid and keeps it on restart, and load_or_update_vm() takes the Some(existing_vm) branch for it, which preserves the CID without occupying it. The CID is therefore reserved by nobody once the reload finishes. allocate() then hands that CID to the next VM created, and the two collide on the same vsock CID as soon as the stopped one starts: 1. deploy VM A -> CID 1000, stop it (A stays in memory and in the UI) 2. call the ReloadVms RPC -> 1000 is not re-occupied 3. deploy VM B -> allocate() returns 1000 4. start A -> A and B both claim CID 1000 Reserve the union of running CIDs and in-memory CIDs, keyed by CID so a VM in both sets is reserved once and the running process wins as the recorded owner. The existing orphan sweep at the end of the function still frees CIDs whose VM disappeared from the filesystem, so this does not leak reservations for removed VMs.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a vsock CID collision hazard in dstack-vmm when reload_vms_sync() rebuilds the CID pool: stopped VMs that still retain their CID in memory are now kept reserved across reloads, preventing allocate() from reusing a CID that a stopped VM will later reclaim on restart.
Changes:
- Rebuild the CID pool from the union of (a) CIDs held by currently running supervisor processes and (b) CIDs recorded in in-memory VM state.
- Add a small pure helper (
cids_to_reserve) to compute the reservation set with “running wins” semantics for ownership. - Add unit tests covering stopped-VM reservation, de-duplication, and precedence rules.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
reload_vms_sync()(theReloadVmsRPC) clears the CID pool and rebuilds it from the supervisor's list of running processes. A stopped VM does not appear in that list, but it still records its CID invm.config.cidand keeps it when restarted.load_or_update_vm()takes theSome(existing_vm)branch for such a VM, which preserves the CID but never occupies it.Once the reload finishes, that CID is reserved by nobody.
allocate()hands it to the next VM created, and the two collide on the same vsock CID the moment the stopped one starts:ReloadVmsRPC → 1000 is not re-occupiedallocate()returns 1000Root cause and fix
The rebuild treated "running according to the supervisor" as equivalent to "holds a CID", but VM state in memory outlives the process.
Reserve the union of the running CIDs and the in-memory CIDs. The set is keyed by CID, so a VM present in both is reserved once and the running process wins as the recorded owner — the supervisor is authoritative about who actually holds a CID right now.
The orphan sweep at the end of
reload_vms_sync()is unchanged and still frees CIDs whose VM disappeared from the filesystem, so this does not leak reservations for removed VMs.Implementation
fix(vmm): keep stopped VMs' CIDs reserved across a reloadThe reservation set is computed by a small pure helper,
cids_to_reserve(), so the rule is testable without standing up a supervisor and a VM directory.Changed paths:
dstack/vmm/src/app.rsScope
One logical
vmmfinding. Found while reviewing #907; it is independent of that change and of theIdPoolbounds work — this is about which CIDs get put back into the pool, not about the pool's own semantics.Verification
cargo test -p dstack-vmm --bins: 83 passed.The new
stopped_vms_keep_their_cid_reserved_across_a_reloadtest was confirmed to fail against the previous logic before the fix was applied:cargo fmt --check --all: passed.cargo clippy -- -D warnings -D clippy::expect_used -D clippy::unwrap_used --allow unused_variables: passed.Not covered: no end-to-end run against a real supervisor with a stopped CVM. The failure mode in step 4 is reasoned from the code path, not observed on hardware.