diff --git a/virtcontainers/api.go b/virtcontainers/api.go index 67ce10ca94..172b33efa8 100644 --- a/virtcontainers/api.go +++ b/virtcontainers/api.go @@ -108,7 +108,7 @@ func createSandboxFromConfig(ctx context.Context, sandboxConfig SandboxConfig, f } }() - if err := s.getAndStoreGuestDetails(); err != nil { + if err = s.getAndStoreGuestDetails(); err != nil { return nil, err } diff --git a/virtcontainers/sandbox.go b/virtcontainers/sandbox.go index 3175ec8ae1..fe2b8d0e17 100644 --- a/virtcontainers/sandbox.go +++ b/virtcontainers/sandbox.go @@ -908,7 +908,7 @@ func (s *Sandbox) ListRoutes() ([]*vcTypes.Route, error) { } // startVM starts the VM. -func (s *Sandbox) startVM() error { +func (s *Sandbox) startVM() (err error) { span, ctx := s.trace("startVM") defer span.Finish() @@ -939,6 +939,12 @@ func (s *Sandbox) startVM() error { return err } + defer func() { + if err != nil { + s.hypervisor.stopSandbox() + } + }() + // In case of vm factory, network interfaces are hotplugged // after vm is started. if s.factory != nil {