Skip to content

fix(sandbox/vm): shut down vm.Instance when localsandbox.Start fails#258

Merged
dmcgowan merged 1 commit into
containerd:mainfrom
austinvazquez:fix/vm-instance-cleanup-on-start-failure
Jul 24, 2026
Merged

fix(sandbox/vm): shut down vm.Instance when localsandbox.Start fails#258
dmcgowan merged 1 commit into
containerd:mainfrom
austinvazquez:fix/vm-instance-cleanup-on-start-failure

Conversation

@austinvazquez

Copy link
Copy Markdown
Member

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.

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>
Copilot AI review requested due to automatic review settings July 23, 2026 21:00

Copilot AI 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.

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.Start to call vm.Instance.Shutdown on any error path after NewInstance succeeds.
  • Gate the deferred cleanup behind a vmiStarted flag so the success path remains unchanged.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread internal/shim/sandbox/vm/vm.go
@austinvazquez
austinvazquez marked this pull request as ready for review July 23, 2026 21:22
@dmcgowan
dmcgowan merged commit f683c1c into containerd:main Jul 24, 2026
16 checks passed
@austinvazquez
austinvazquez deleted the fix/vm-instance-cleanup-on-start-failure branch July 24, 2026 20:12
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.

3 participants