diff --git a/containerd-shim-v2/delete.go b/containerd-shim-v2/delete.go index ddf44a819..0e4377124 100644 --- a/containerd-shim-v2/delete.go +++ b/containerd-shim-v2/delete.go @@ -17,11 +17,11 @@ import ( ) func deleteContainer(ctx context.Context, s *service, c *container) error { - if !c.cType.IsSandbox() { - status, err := s.sandbox.StatusContainer(c.id) - if err != nil { - return err - } + status, err := s.sandbox.StatusContainer(c.id) + if err != nil && !isNotFound(err) { + return err + } + if !c.cType.IsSandbox() && err == nil { if status.State.State != types.StateStopped { _, err = s.sandbox.StopContainer(c.id, false) if err != nil { diff --git a/virtcontainers/container.go b/virtcontainers/container.go index 9d2278f90..66f71fcb1 100644 --- a/virtcontainers/container.go +++ b/virtcontainers/container.go @@ -1096,13 +1096,6 @@ func (c *Container) stop(force bool) error { // get failed if the process hasn't exited. c.sandbox.agent.waitProcess(c, c.id) - // container was killed by force, container MUST change its state - // as soon as possible just in case one of below operations fail leaving - // the containers in a bad state. - if err := c.setContainerState(types.StateStopped); err != nil { - return err - } - defer func() { // Save device and drive data. // TODO: can we merge this saving with setContainerState()? @@ -1133,6 +1126,13 @@ func (c *Container) stop(force bool) error { return err } + // container was killed by force, container MUST change its state + // as soon as possible just in case one of below operations fail leaving + // the containers in a bad state. + if err := c.setContainerState(types.StateStopped); err != nil { + return err + } + return nil }