Merge pull request #37184 from cpuguy83/attach_leak

Fix fd leak on attach
This commit is contained in:
Sebastiaan van Stijn 2018-06-08 13:08:06 -07:00 committed by GitHub
commit 5cb95f693d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 88 additions and 97 deletions

View File

@ -3,11 +3,12 @@ package stream // import "github.com/docker/docker/container/stream"
import ( import (
"context" "context"
"io" "io"
"sync"
"github.com/docker/docker/pkg/pools" "github.com/docker/docker/pkg/pools"
"github.com/docker/docker/pkg/term" "github.com/docker/docker/pkg/term"
"github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
) )
var defaultEscapeSequence = []byte{16, 17} // ctrl-p, ctrl-q var defaultEscapeSequence = []byte{16, 17} // ctrl-p, ctrl-q
@ -57,43 +58,15 @@ func (c *Config) AttachStreams(cfg *AttachConfig) {
// CopyStreams starts goroutines to copy data in and out to/from the container // CopyStreams starts goroutines to copy data in and out to/from the container
func (c *Config) CopyStreams(ctx context.Context, cfg *AttachConfig) <-chan error { func (c *Config) CopyStreams(ctx context.Context, cfg *AttachConfig) <-chan error {
var ( var group errgroup.Group
wg sync.WaitGroup
errors = make(chan error, 3)
)
if cfg.Stdin != nil {
wg.Add(1)
}
if cfg.Stdout != nil {
wg.Add(1)
}
if cfg.Stderr != nil {
wg.Add(1)
}
// Connect stdin of container to the attach stdin stream. // Connect stdin of container to the attach stdin stream.
go func() { if cfg.Stdin != nil {
if cfg.Stdin == nil { group.Go(func() error {
return
}
logrus.Debug("attach: stdin: begin") logrus.Debug("attach: stdin: begin")
defer logrus.Debug("attach: stdin: end")
var err error defer func() {
if cfg.TTY {
_, err = copyEscapable(cfg.CStdin, cfg.Stdin, cfg.DetachKeys)
} else {
_, err = pools.Copy(cfg.CStdin, cfg.Stdin)
}
if err == io.ErrClosedPipe {
err = nil
}
if err != nil {
logrus.Errorf("attach: stdin: %s", err)
errors <- err
}
if cfg.CloseStdin && !cfg.TTY { if cfg.CloseStdin && !cfg.TTY {
cfg.CStdin.Close() cfg.CStdin.Close()
} else { } else {
@ -105,48 +78,66 @@ func (c *Config) CopyStreams(ctx context.Context, cfg *AttachConfig) <-chan erro
cfg.CStderr.Close() cfg.CStderr.Close()
} }
} }
logrus.Debug("attach: stdin: end")
wg.Done()
}() }()
attachStream := func(name string, stream io.Writer, streamPipe io.ReadCloser) { var err error
if stream == nil { if cfg.TTY {
return _, err = copyEscapable(cfg.CStdin, cfg.Stdin, cfg.DetachKeys)
} else {
_, err = pools.Copy(cfg.CStdin, cfg.Stdin)
} }
logrus.Debugf("attach: %s: begin", name)
_, err := pools.Copy(stream, streamPipe)
if err == io.ErrClosedPipe { if err == io.ErrClosedPipe {
err = nil err = nil
} }
if err != nil { if err != nil {
logrus.Errorf("attach: %s: %v", name, err) logrus.WithError(err).Debug("error on attach stdin")
errors <- err return errors.Wrap(err, "error on attach stdin")
} }
return nil
})
}
attachStream := func(name string, stream io.Writer, streamPipe io.ReadCloser) error {
logrus.Debugf("attach: %s: begin", name)
defer logrus.Debugf("attach: %s: end", name)
defer func() {
// Make sure stdin gets closed // Make sure stdin gets closed
if cfg.Stdin != nil { if cfg.Stdin != nil {
cfg.Stdin.Close() cfg.Stdin.Close()
} }
streamPipe.Close() streamPipe.Close()
logrus.Debugf("attach: %s: end", name) }()
wg.Done()
_, err := pools.Copy(stream, streamPipe)
if err == io.ErrClosedPipe {
err = nil
}
if err != nil {
logrus.WithError(err).Debugf("attach: %s", name)
return errors.Wrapf(err, "error attaching %s stream", name)
}
return nil
} }
go attachStream("stdout", cfg.Stdout, cfg.CStdout) if cfg.Stdout != nil {
go attachStream("stderr", cfg.Stderr, cfg.CStderr) group.Go(func() error {
return attachStream("stdout", cfg.Stdout, cfg.CStdout)
})
}
if cfg.Stderr != nil {
group.Go(func() error {
return attachStream("stderr", cfg.Stderr, cfg.CStderr)
})
}
errs := make(chan error, 1) errs := make(chan error, 1)
go func() { go func() {
defer close(errs) defer logrus.Debug("attach done")
errs <- func() error { groupErr := make(chan error, 1)
done := make(chan struct{})
go func() { go func() {
wg.Wait() groupErr <- group.Wait()
close(done)
}() }()
select { select {
case <-done:
case <-ctx.Done(): case <-ctx.Done():
// close all pipes // close all pipes
if cfg.CStdin != nil { if cfg.CStdin != nil {
@ -158,16 +149,16 @@ func (c *Config) CopyStreams(ctx context.Context, cfg *AttachConfig) <-chan erro
if cfg.CStderr != nil { if cfg.CStderr != nil {
cfg.CStderr.Close() cfg.CStderr.Close()
} }
<-done
// Now with these closed, wait should return.
if err := group.Wait(); err != nil {
errs <- err
return
} }
close(errors) errs <- ctx.Err()
for err := range errors { case err := <-groupErr:
if err != nil { errs <- err
return err
} }
}
return nil
}()
}() }()
return errs return errs

View File

@ -176,7 +176,7 @@ func (daemon *Daemon) containerAttach(c *container.Container, cfg *stream.Attach
ctx := c.InitAttachContext() ctx := c.InitAttachContext()
err := <-c.StreamConfig.CopyStreams(ctx, cfg) err := <-c.StreamConfig.CopyStreams(ctx, cfg)
if err != nil { if err != nil {
if _, ok := err.(term.EscapeError); ok { if _, ok := errors.Cause(err).(term.EscapeError); ok || err == context.Canceled {
daemon.LogContainerEvent(c, "detach") daemon.LogContainerEvent(c, "detach")
} else { } else {
logrus.Errorf("attach failed with error: %v", err) logrus.Errorf("attach failed with error: %v", err)