|
| 1 | +From b9beeef78a6fd90ece5801780c45f550caf71b3d Mon Sep 17 00:00:00 2001 |
| 2 | +From: wheat2018 < [email protected]> |
| 3 | +Date: Tue, 13 Aug 2024 15:56:31 +0800 |
| 4 | +Subject: [PATCH] fix goroutine leak of container Attach |
| 5 | + |
| 6 | +The monitor goroutine (runs (*ContainerIO).Attach.func1) of Attach will |
| 7 | +never finish if it attaches to a container without any stdout or stderr |
| 8 | +output. Wait for http context cancel and break the pipe actively to |
| 9 | +address the issue. |
| 10 | + |
| 11 | +Signed-off-by: wheat2018 < [email protected]> |
| 12 | +Signed-off-by: Akihiro Suda < [email protected]> |
| 13 | +Signed-off-by: Azure Linux Security Servicing Account < [email protected]> |
| 14 | +Upstream-reference: https://github.com/containerd/containerd/commit/083b53cd6f19b5de7717b0ce92c11bdf95e612df.patch |
| 15 | +--- |
| 16 | + internal/cri/io/container_io.go | 14 +++++++++++--- |
| 17 | + internal/cri/server/container_attach.go | 2 +- |
| 18 | + 2 files changed, 12 insertions(+), 4 deletions(-) |
| 19 | + |
| 20 | +diff --git a/internal/cri/io/container_io.go b/internal/cri/io/container_io.go |
| 21 | +index 9fc5545..194634e 100644 |
| 22 | +--- a/internal/cri/io/container_io.go |
| 23 | ++++ b/internal/cri/io/container_io.go |
| 24 | +@@ -17,6 +17,7 @@ |
| 25 | + package io |
| 26 | + |
| 27 | + import ( |
| 28 | ++ "context" |
| 29 | + "errors" |
| 30 | + "fmt" |
| 31 | + "io" |
| 32 | +@@ -160,7 +161,7 @@ func (c *ContainerIO) Pipe() { |
| 33 | + |
| 34 | + // Attach attaches container stdio. |
| 35 | + // TODO(random-liu): Use pools.Copy in docker to reduce memory usage? |
| 36 | +-func (c *ContainerIO) Attach(opts AttachOptions) { |
| 37 | ++func (c *ContainerIO) Attach(ctx context.Context, opts AttachOptions) { |
| 38 | + var wg sync.WaitGroup |
| 39 | + key := util.GenerateID() |
| 40 | + stdinKey := streamKey(c.id, "attach-"+key, Stdin) |
| 41 | +@@ -201,8 +202,15 @@ func (c *ContainerIO) Attach(opts AttachOptions) { |
| 42 | + } |
| 43 | + |
| 44 | + attachStream := func(key string, close <-chan struct{}) { |
| 45 | +- <-close |
| 46 | +- log.L.Infof("Attach stream %q closed", key) |
| 47 | ++ select { |
| 48 | ++ case <-close: |
| 49 | ++ log.L.Infof("Attach stream %q closed", key) |
| 50 | ++ case <-ctx.Done(): |
| 51 | ++ log.L.Infof("Attach client of %q cancelled", key) |
| 52 | ++ // Avoid writeGroup heap up |
| 53 | ++ c.stdoutGroup.Remove(key) |
| 54 | ++ c.stderrGroup.Remove(key) |
| 55 | ++ } |
| 56 | + // Make sure stdin gets closed. |
| 57 | + if stdinStreamRC != nil { |
| 58 | + stdinStreamRC.Close() |
| 59 | +diff --git a/internal/cri/server/container_attach.go b/internal/cri/server/container_attach.go |
| 60 | +index 0147859..f4c3322 100644 |
| 61 | +--- a/internal/cri/server/container_attach.go |
| 62 | ++++ b/internal/cri/server/container_attach.go |
| 63 | +@@ -82,6 +82,6 @@ func (c *criService) attachContainer(ctx context.Context, id string, stdin io.Re |
| 64 | + }, |
| 65 | + } |
| 66 | + // TODO(random-liu): Figure out whether we need to support historical output. |
| 67 | +- cntr.IO.Attach(opts) |
| 68 | ++ cntr.IO.Attach(ctx, opts) |
| 69 | + return nil |
| 70 | + } |
| 71 | +-- |
| 72 | +2.45.4 |
| 73 | + |
0 commit comments