From 2006d9f7d1070fb2d4f785a02dea1eb0481748ea Mon Sep 17 00:00:00 2001 From: Cory Snider Date: Fri, 28 Oct 2022 15:20:03 +0200 Subject: [PATCH] cmd/dockerd: Rewrite shutdownDaemon to use context timeout MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Paweł Gronowski --- cmd/dockerd/daemon.go | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/cmd/dockerd/daemon.go b/cmd/dockerd/daemon.go index b385b671ab..476ebdd6aa 100644 --- a/cmd/dockerd/daemon.go +++ b/cmd/dockerd/daemon.go @@ -360,26 +360,23 @@ func (cli *DaemonCli) stop() { // d.Shutdown() is waiting too long to kill container or worst it's // blocked there func shutdownDaemon(ctx context.Context, d *daemon.Daemon) { - shutdownTimeout := d.ShutdownTimeout() - ch := make(chan struct{}) - go func() { - d.Shutdown(ctx) - close(ch) - }() - if shutdownTimeout < 0 { - <-ch - logrus.Debug("Clean shutdown succeeded") - return + var cancel context.CancelFunc + if timeout := d.ShutdownTimeout(); timeout >= 0 { + ctx, cancel = context.WithTimeout(ctx, time.Duration(timeout)*time.Second) + } else { + ctx, cancel = context.WithCancel(ctx) } - timeout := time.NewTimer(time.Duration(shutdownTimeout) * time.Second) - defer timeout.Stop() + go func() { + defer cancel() + d.Shutdown(ctx) + }() - select { - case <-ch: - logrus.Debug("Clean shutdown succeeded") - case <-timeout.C: + <-ctx.Done() + if errors.Is(ctx.Err(), context.DeadlineExceeded) { logrus.Error("Force shutdown daemon") + } else { + logrus.Debug("Clean shutdown succeeded") } }