diff --git a/virtcontainers/api.go b/virtcontainers/api.go index 6405a34d28..5466d5f6d4 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 84d7aeb853..0092737400 100644 --- a/virtcontainers/sandbox.go +++ b/virtcontainers/sandbox.go @@ -913,7 +913,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() @@ -944,6 +944,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 {