From 743309cdc944e91c9673aac370933262325591b7 Mon Sep 17 00:00:00 2001 From: Peng Tao Date: Tue, 10 Dec 2019 21:12:09 -0800 Subject: [PATCH] vc: stop container should change container state at last Otherwise if we fail to stop it, container state is set as StateStopped. And future force stop will just be ignored. Then when we force delete the container, we are deleting it without actually cleaning up container resources especially the host shared mounts, which would be removed by agent cleanup code and we endup removing container volume contents unexpectedly. Fixes: #2345 Signed-off-by: Peng Tao --- virtcontainers/container.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) 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 }