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 }