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 <bergwolf@hyper.sh>
This commit is contained in:
Peng Tao 2019-12-10 21:12:09 -08:00
parent e3115e344e
commit 743309cdc9

View File

@ -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
}