Move context cancel into defer.

Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
Lantao Liu 2019-07-25 15:00:43 -07:00
parent 74d2b6947c
commit d3cacff8e1

View File

@ -135,18 +135,16 @@ func (c *criService) stopContainer(ctx context.Context, container containerstore
} }
sigTermCtx, sigTermCtxCancel := context.WithTimeout(ctx, timeout) sigTermCtx, sigTermCtxCancel := context.WithTimeout(ctx, timeout)
defer sigTermCtxCancel()
err = c.waitContainerStop(sigTermCtx, container) err = c.waitContainerStop(sigTermCtx, container)
if err == nil { if err == nil {
// Container stopped on first signal no need for SIGKILL // Container stopped on first signal no need for SIGKILL
sigTermCtxCancel()
return nil return nil
} }
// If the parent context was cancelled or exceeded return immediately // If the parent context was cancelled or exceeded return immediately
if ctx.Err() != nil { if ctx.Err() != nil {
sigTermCtxCancel()
return ctx.Err() return ctx.Err()
} }
sigTermCtxCancel()
// sigTermCtx was exceeded. Send SIGKILL // sigTermCtx was exceeded. Send SIGKILL
logrus.Debugf("Stop container %q with signal %v timed out", id, sig) logrus.Debugf("Stop container %q with signal %v timed out", id, sig)
} }