fix(sandbox/vm): shut down vm.Instance when localsandbox.Start fails#258
Merged
dmcgowan merged 1 commit intoJul 24, 2026
Conversation
localsandbox.Start calls vmm.NewInstance — which allocates resources on behalf of the caller — but only assigns s.instance on success. Any failure after NewInstance (AddDisk, AddFS, AddNIC, SetCPUAndMemory, or vmi.Start) returns without calling Shutdown, leaving an orphaned vm.Instance. localsandbox.Stop returns ErrFailedPrecondition when s.instance is nil, so there is no subsequent path that reaches Shutdown to release those resources. Add a deferred Shutdown after a successful NewInstance call, guarded by a vmiStarted flag that is only set true once vmi.Start succeeds and s.instance is assigned. This covers all failure paths without changing the success path. The Shutdown error is discarded on the cleanup path; the original Start error is what callers need. Signed-off-by: Austin Vazquez <austin.vazquez@docker.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes a resource-leak scenario in the VM-backed sandbox startup path by ensuring partially-initialized vm.Instance objects are shut down when localsandbox.Start fails after allocating the instance.
Changes:
- Add a deferred cleanup in
localsandbox.Startto callvm.Instance.Shutdownon any error path afterNewInstancesucceeds. - Gate the deferred cleanup behind a
vmiStartedflag so the success path remains unchanged.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
austinvazquez
marked this pull request as ready for review
July 23, 2026 21:22
dmcgowan
approved these changes
Jul 24, 2026
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.
localsandbox.Start calls vmm.NewInstance — which allocates resources on behalf of the caller — but only assigns s.instance on success. Any failure after NewInstance (AddDisk, AddFS, AddNIC, SetCPUAndMemory, or vmi.Start) returns without calling Shutdown, leaving an orphaned vm.Instance. localsandbox.Stop returns ErrFailedPrecondition when s.instance is nil, so there is no subsequent path that reaches Shutdown to release those resources.
Add a deferred Shutdown after a successful NewInstance call, guarded by a vmiStarted flag that is only set true once vmi.Start succeeds and s.instance is assigned. This covers all failure paths without changing the success path. The Shutdown error is discarded on the cleanup path; the original Start error is what callers need.