diff --git a/cmd/dockerd/daemon.go b/cmd/dockerd/daemon.go index b385b671ab283fc967d73ed34eab0adf77d91a7b..476ebdd6aa44edd9e76644e0ecb296e434ce7454 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{}) + 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) + } + go func() { + defer cancel() d.Shutdown(ctx) - close(ch) }() - if shutdownTimeout < 0 { - <-ch - logrus.Debug("Clean shutdown succeeded") - return - } - timeout := time.NewTimer(time.Duration(shutdownTimeout) * time.Second) - defer timeout.Stop() - - 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") } }