From 3fab78a174b23d012a71f96fd4cdc7590706323e Mon Sep 17 00:00:00 2001
From: chenjiankun <chenjiankun1@huawei.com>
Date: Mon, 8 Nov 2021 20:23:08 +0800
Subject: [PATCH] docker: Adding logs for debugging in docker stop

do the following logs for debug
1. add container id in logs
2. add logs for each "kill"
3. sync with community
---
 components/engine/daemon/container_operations_unix.go |  2 +-
 components/engine/daemon/stop.go                      | 10 ++++++----
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/components/engine/daemon/container_operations_unix.go b/components/engine/daemon/container_operations_unix.go
index df2f3261f..2ea167ca2 100644
--- a/components/engine/daemon/container_operations_unix.go
+++ b/components/engine/daemon/container_operations_unix.go
@@ -345,7 +345,6 @@ func killProcessDirectly(cntr *container.Container) error {
 	if status.Err() != nil {
 		// Ensure that we don't kill ourselves
 		if pid := cntr.GetPID(); pid != 0 {
-			logrus.Infof("Container %s failed to exit within 10 seconds of kill - trying direct SIGKILL", stringid.TruncateID(cntr.ID))
 			pattern := fmt.Sprintf("/var/run/docker/containerd/exit/moby/%s.%d.*", cntr.ID, pid)
 			efiles, err := filepath.Glob(pattern)
 			if err != nil {
@@ -356,6 +355,7 @@ func killProcessDirectly(cntr *container.Container) error {
 				return errNoSuchProcess{pid, 9}
 			}
 
+			logrus.Infof("Container %s failed to exit within 10 seconds of kill - trying direct SIGKILL", stringid.TruncateID(cntr.ID))
 			if err := unix.Kill(pid, 9); err != nil {
 				if err != unix.ESRCH {
 					return err
diff --git a/components/engine/daemon/stop.go b/components/engine/daemon/stop.go
index 741f5d5dd..633a34aab 100644
--- a/components/engine/daemon/stop.go
+++ b/components/engine/daemon/stop.go
@@ -48,7 +48,7 @@ func (daemon *Daemon) containerStop(container *containerpkg.Container, seconds i
 	stopSignal := container.StopSignal()
 	// 1. Send a stop signal
 	if err := daemon.killPossiblyDeadProcess(container, stopSignal); err != nil {
-		logrus.Infof("docker send %d signal to stop container get error: %v", stopSignal, err)
+		logrus.Infof("docker send %d signal to stop container %v get error: %v", stopSignal, container.ID, err)
 		// While normally we might "return err" here we're not going to
 		// because if we can't stop the container by this point then
 		// it's probably because it's already stopped. Meaning, between
@@ -63,7 +63,7 @@ func (daemon *Daemon) containerStop(container *containerpkg.Container, seconds i
 		defer cancel()
 
 		if status := <-container.Wait(ctx, containerpkg.WaitConditionNotRunning); status.Err() != nil {
-			logrus.Infof("Container failed to stop after sending signal %d to the process, force killing", stopSignal)
+			logrus.Infof("Container %v failed to stop after sending signal %d to the process, force killing", container.ID, stopSignal)
 			if err := daemon.killPossiblyDeadProcess(container, 9); err != nil {
 				return err
 			}
@@ -85,8 +85,10 @@ func (daemon *Daemon) containerStop(container *containerpkg.Container, seconds i
 			ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
 			defer cancel()
 
-			<-container.Wait(ctx, containerpkg.WaitConditionNotRunning)
-			logrus.Warn(err) // Don't return error because we only care that container is stopped, not what function stopped it
+			if status := <-container.Wait(ctx, containerpkg.WaitConditionNotRunning); status.Err() != nil {
+				logrus.WithError(err).WithField("container", container.ID).Error("Error killing the container")
+				return err
+			}
 		}
 	}
 
-- 
2.27.0

