|
| 1 | +From 711fc7f7388bdb317a813a791d9e8d38f725a85f Mon Sep 17 00:00:00 2001 |
| 2 | +From: AllSpark < [email protected]> |
| 3 | +Date: Mon, 10 Nov 2025 03:04:25 +0000 |
| 4 | +Subject: [PATCH] fix(cri): prevent goroutine leak in ContainerIO.Attach by |
| 5 | + honoring ctx cancellation and removing writer group entries; plumb context |
| 6 | + through attach call |
| 7 | + |
| 8 | +Signed-off-by: Azure Linux Security Servicing Account < [email protected]> |
| 9 | +Upstream-reference: AI Backport from Build Backport-Build-ID-978512 |
| 10 | +--- |
| 11 | + pkg/cri/io/container_io.go | 14 +++++++++++--- |
| 12 | + pkg/cri/server/container_attach.go | 2 +- |
| 13 | + 2 files changed, 12 insertions(+), 4 deletions(-) |
| 14 | + |
| 15 | +diff --git a/pkg/cri/io/container_io.go b/pkg/cri/io/container_io.go |
| 16 | +index 70bc8b7..e158410 100644 |
| 17 | +--- a/pkg/cri/io/container_io.go |
| 18 | ++++ b/pkg/cri/io/container_io.go |
| 19 | +@@ -17,6 +17,7 @@ |
| 20 | + package io |
| 21 | + |
| 22 | + import ( |
| 23 | ++ "context" |
| 24 | + "errors" |
| 25 | + "io" |
| 26 | + "strings" |
| 27 | +@@ -134,7 +135,7 @@ func (c *ContainerIO) Pipe() { |
| 28 | + |
| 29 | + // Attach attaches container stdio. |
| 30 | + // TODO(random-liu): Use pools.Copy in docker to reduce memory usage? |
| 31 | +-func (c *ContainerIO) Attach(opts AttachOptions) { |
| 32 | ++func (c *ContainerIO) Attach(ctx context.Context, opts AttachOptions) { |
| 33 | + var wg sync.WaitGroup |
| 34 | + key := util.GenerateID() |
| 35 | + stdinKey := streamKey(c.id, "attach-"+key, Stdin) |
| 36 | +@@ -175,8 +176,15 @@ func (c *ContainerIO) Attach(opts AttachOptions) { |
| 37 | + } |
| 38 | + |
| 39 | + attachStream := func(key string, close <-chan struct{}) { |
| 40 | +- <-close |
| 41 | +- logrus.Infof("Attach stream %q closed", key) |
| 42 | ++ select { |
| 43 | ++ case <-close: |
| 44 | ++ logrus.Infof("Attach stream %q closed", key) |
| 45 | ++ case <-ctx.Done(): |
| 46 | ++ logrus.Infof("Attach client of %q cancelled", key) |
| 47 | ++ // Avoid writeGroup heap up |
| 48 | ++ c.stdoutGroup.Remove(key) |
| 49 | ++ c.stderrGroup.Remove(key) |
| 50 | ++ } |
| 51 | + // Make sure stdin gets closed. |
| 52 | + if stdinStreamRC != nil { |
| 53 | + stdinStreamRC.Close() |
| 54 | +diff --git a/pkg/cri/server/container_attach.go b/pkg/cri/server/container_attach.go |
| 55 | +index cd79f3b..aa6519a 100644 |
| 56 | +--- a/pkg/cri/server/container_attach.go |
| 57 | ++++ b/pkg/cri/server/container_attach.go |
| 58 | +@@ -79,6 +79,6 @@ func (c *criService) attachContainer(ctx context.Context, id string, stdin io.Re |
| 59 | + }, |
| 60 | + } |
| 61 | + // TODO(random-liu): Figure out whether we need to support historical output. |
| 62 | +- cntr.IO.Attach(opts) |
| 63 | ++ cntr.IO.Attach(ctx, opts) |
| 64 | + return nil |
| 65 | + } |
| 66 | +-- |
| 67 | +2.45.4 |
| 68 | + |
0 commit comments