Skip to content

Commit

Permalink
attach: close streams when done
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
laurazard authored and glours committed Sep 10, 2024
1 parent b633c5c commit 329ad73
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions pkg/compose/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 329ad73

Please sign in to comment.