浏览代码

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 <[email protected]>
Laura Brehm 1 年之前
父节点
当前提交
329ad73922
共有 1 个文件被更改,包括 4 次插入6 次删除
  1. 4 6
      pkg/compose/attach.go

+ 4 - 6
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 {