From 329ad73922940a5e5e4ef84e5cd166f886b4ced5 Mon Sep 17 00:00:00 2001 From: Laura Brehm Date: Tue, 10 Sep 2024 13:30:14 +0100 Subject: [PATCH] attach: close streams when done When Compose is watching a project/reattaching streams on container start, it will make new API `ContainerAttach()` calls every time a container it's watching is started. However, it only closes the stream when the context used to start the attach is canceled. This means that if a user has a project with multiple containers where containers keep restarting, Compose will attach to the new containers but never close the previous streams, causing fds to pile up and goroutines on the engine to get stuck. Signed-off-by: Laura Brehm --- pkg/compose/attach.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/pkg/compose/attach.go b/pkg/compose/attach.go index 499f002fd..d6d8ce3eb 100644 --- a/pkg/compose/attach.go +++ b/pkg/compose/attach.go @@ -102,9 +102,7 @@ func (s *composeService) attachContainer(ctx context.Context, container moby.Con func (s *composeService) attachContainerStreams(ctx context.Context, container string, tty bool, stdin io.ReadCloser, stdout, stderr io.WriteCloser) (func(), chan bool, error) { detached := make(chan bool) - var ( - restore = func() { /* noop */ } - ) + restore := func() { /* noop */ } if stdin != nil { in := streams.NewIn(stdin) if in.IsTerminal() { @@ -128,7 +126,6 @@ func (s *composeService) attachContainerStreams(ctx context.Context, container s if stdin != nil { stdin.Close() //nolint:errcheck } - streamOut.Close() //nolint:errcheck }() if streamIn != nil && stdin != nil { @@ -143,8 +140,9 @@ func (s *composeService) attachContainerStreams(ctx context.Context, container s if stdout != nil { go func() { - defer stdout.Close() //nolint:errcheck - defer stderr.Close() //nolint:errcheck + defer stdout.Close() //nolint:errcheck + defer stderr.Close() //nolint:errcheck + defer streamOut.Close() //nolint:errcheck if tty { io.Copy(stdout, streamOut) //nolint:errcheck } else {