1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

daemon: containerStop(): use a regular "defer" to log container event

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2021-08-20 22:57:09 +02:00
parent 5edf9acf9c
commit 952902efbc
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -34,7 +34,7 @@ func (daemon *Daemon) ContainerStop(name string, timeout *int) error {
}
// containerStop sends a stop signal, waits, sends a kill signal.
func (daemon *Daemon) containerStop(ctr *container.Container, seconds *int) error {
func (daemon *Daemon) containerStop(ctr *container.Container, seconds *int) (retErr error) {
// TODO propagate a context down to this function
ctx := context.TODO()
if !ctr.IsRunning() {
@ -53,10 +53,11 @@ func (daemon *Daemon) containerStop(ctr *container.Container, seconds *int) erro
if stopTimeout >= 0 {
wait = time.Duration(stopTimeout) * time.Second
}
success := func() error {
defer func() {
if retErr == nil {
daemon.LogContainerEvent(ctr, "stop")
return nil
}
}()
// 1. Send a stop signal
err := daemon.killPossiblyDeadProcess(ctr, stopSignal)
@ -75,7 +76,7 @@ func (daemon *Daemon) containerStop(ctr *container.Container, seconds *int) erro
if status := <-ctr.Wait(subCtx, container.WaitConditionNotRunning); status.Err() == nil {
// container did exit, so ignore any previous errors and return
return success()
return nil
}
if err != nil {
@ -89,18 +90,19 @@ func (daemon *Daemon) containerStop(ctr *container.Container, seconds *int) erro
}
logrus.WithField("container", ctr.ID).Infof("Container failed to exit within %s of signal %d - using the force", wait, stopSignal)
// Stop either failed or container didnt exit, so fallback to kill.
// Stop either failed or container didn't exit, so fallback to kill.
if err := daemon.Kill(ctr); err != nil {
// got a kill error, but give container 2 more seconds to exit just in case
subCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()
if status := <-ctr.Wait(subCtx, container.WaitConditionNotRunning); status.Err() == nil {
// container did exit, so ignore error and return
return success()
}
logrus.WithError(err).WithField("container", ctr.ID).Error("Error killing the container")
status := <-ctr.Wait(subCtx, container.WaitConditionNotRunning)
if status.Err() != nil {
logrus.WithError(err).WithField("container", ctr.ID).Errorf("error killing container: %v", status.Err())
return err
}
return success()
// container did exit, so ignore previous errors and continue
}
return nil
}