From c7b346f1196e7e99be5d0fd3ef82e221681820ee Mon Sep 17 00:00:00 2001
From: zhongjiawei <zhongjiawei1@huawei.com>
Date: Thu, 11 May 2023 09:47:51 +0800
Subject: [PATCH] containerd: fix journald stop container shim log stuck bug

---
 runtime/v1/linux/runtime.go      | 36 +++++++++++++++++++-------------
 runtime/v1/shim/client/client.go | 16 ++++++++------
 2 files changed, 32 insertions(+), 20 deletions(-)

diff --git a/runtime/v1/linux/runtime.go b/runtime/v1/linux/runtime.go
index eb39273..f8ddd7f 100644
--- a/runtime/v1/linux/runtime.go
+++ b/runtime/v1/linux/runtime.go
@@ -428,6 +428,18 @@ func (r *Runtime) loadTasks(ctx context.Context, ns string) ([]*Task, error) {
 
 		logDirPath := filepath.Join(r.root, ns, id)
 
+		copyAndClose := func(dst io.Writer, src io.ReadWriteCloser) {
+			copyDone := make(chan struct{})
+			go func() {
+				io.Copy(dst, src)
+				close(copyDone)
+			}()
+			select {
+			case <-shimExit:
+			case <-copyDone:
+			}
+			src.Close()
+		}
 		shimStdoutLog, err := v1.OpenShimStdoutLog(ctx, logDirPath)
 		if err != nil {
 			log.G(ctx).WithError(err).WithFields(logrus.Fields{
@@ -437,7 +449,11 @@ func (r *Runtime) loadTasks(ctx context.Context, ns string) ([]*Task, error) {
 			}).Error("opening shim stdout log pipe")
 			continue
 		}
-		go io.Copy(os.Stdout, shimStdoutLog)
+		if r.config.ShimDebug {
+			go copyAndClose(os.Stdout, shimStdoutLog)
+		} else {
+			go copyAndClose(io.Discard, shimStdoutLog)
+		}
 
 		shimStderrLog, err := v1.OpenShimStderrLog(ctx, logDirPath)
 		if err != nil {
@@ -448,19 +464,11 @@ func (r *Runtime) loadTasks(ctx context.Context, ns string) ([]*Task, error) {
 			}).Error("opening shim stderr log pipe")
 			continue
 		}
-		go io.Copy(os.Stderr, shimStderrLog)
-
-		go func() {
-			select {
-			case <-shimExit:
-				if shimStdoutLog != nil {
-					shimStdoutLog.Close()
-				}
-				if shimStderrLog != nil {
-					shimStderrLog.Close()
-				}
-			}
-		}()
+		if r.config.ShimDebug {
+			go copyAndClose(os.Stderr, shimStderrLog)
+		} else {
+			go copyAndClose(io.Discard, shimStderrLog)
+		}
 
 		t, err := newTask(id, ns, pid, s, r.events, r.tasks, bundle)
 		if err != nil {
diff --git a/runtime/v1/shim/client/client.go b/runtime/v1/shim/client/client.go
index 6861df0..c5a9bba 100644
--- a/runtime/v1/shim/client/client.go
+++ b/runtime/v1/shim/client/client.go
@@ -75,20 +75,24 @@ func WithStart(binary, address, daemonAddress, cgroup string, debug bool, exitHa
 		}
 		defer f.Close()
 
-		var stdoutLog io.ReadWriteCloser
-		var stderrLog io.ReadWriteCloser
-		stdoutLog, err = v1.OpenShimStdoutLog(ctx, config.WorkDir)
+		stdoutCopy := io.Discard
+		stderrCopy := io.Discard
+		stdoutLog, err := v1.OpenShimStdoutLog(ctx, config.WorkDir)
 		if err != nil {
 			return nil, nil, errors.Wrapf(err, "failed to create stdout log")
 		}
 
-		stderrLog, err = v1.OpenShimStderrLog(ctx, config.WorkDir)
+		stderrLog, err := v1.OpenShimStderrLog(ctx, config.WorkDir)
 		if err != nil {
 			return nil, nil, errors.Wrapf(err, "failed to create stderr log")
 		}
+		if debug {
+			stdoutCopy = os.Stdout
+			stderrCopy = os.Stderr
+		}
 
-		go io.Copy(os.Stdout, stdoutLog)
-		go io.Copy(os.Stderr, stderrLog)
+		go io.Copy(stdoutCopy, stdoutLog)
+		go io.Copy(stderrCopy, stderrLog)
 
 		if err := writeFile(filepath.Join(config.Path, "address"), address); err != nil {
 			return nil, nil, err
-- 
2.33.0

