From a444b5f60c75bdac82c9a7126738c210e2ccf941 Mon Sep 17 00:00:00 2001 From: Qiang Huang Date: Tue, 8 Mar 2016 10:45:17 +0800 Subject: [PATCH 1/2] Fix race condition when exec with tty I can reproduce this easily on one of my servers, `docker exec -ti my_cont ls` will not print anything, without `-t` it acts normally. Signed-off-by: Qiang Huang --- daemon/execdriver/native/driver.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/daemon/execdriver/native/driver.go b/daemon/execdriver/native/driver.go index 98d64a32cf..a765305013 100644 --- a/daemon/execdriver/native/driver.go +++ b/daemon/execdriver/native/driver.go @@ -441,12 +441,12 @@ type TtyConsole struct { } // NewTtyConsole returns a new TtyConsole struct. -func NewTtyConsole(console libcontainer.Console, pipes *execdriver.Pipes) (*TtyConsole, error) { +func NewTtyConsole(console libcontainer.Console, pipes *execdriver.Pipes, wg *sync.WaitGroup) (*TtyConsole, error) { tty := &TtyConsole{ console: console, } - if err := tty.AttachPipes(pipes); err != nil { + if err := tty.AttachPipes(pipes, wg); err != nil { tty.Close() return nil, err } @@ -460,8 +460,10 @@ func (t *TtyConsole) Resize(h, w int) error { } // AttachPipes attaches given pipes to TtyConsole -func (t *TtyConsole) AttachPipes(pipes *execdriver.Pipes) error { +func (t *TtyConsole) AttachPipes(pipes *execdriver.Pipes, wg *sync.WaitGroup) error { + wg.Add(1) go func() { + defer wg.Done() if wb, ok := pipes.Stdout.(interface { CloseWriters() error }); ok { @@ -501,7 +503,7 @@ func setupPipes(container *configs.Config, processConfig *execdriver.ProcessConf if err != nil { return writers, err } - term, err := NewTtyConsole(cons, pipes) + term, err := NewTtyConsole(cons, pipes, wg) if err != nil { return writers, err } From 021a12ef1b96ebee852173212d9de6b0a6285f92 Mon Sep 17 00:00:00 2001 From: Qiang Huang Date: Tue, 8 Mar 2016 10:54:18 +0800 Subject: [PATCH 2/2] Revert hack in TestExecTTY It'll weaken this test case, and the flaky test is resolved by another commit in this PR. Signed-off-by: Qiang Huang --- integration-cli/docker_cli_exec_unix_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration-cli/docker_cli_exec_unix_test.go b/integration-cli/docker_cli_exec_unix_test.go index 9363092652..42db4091dd 100644 --- a/integration-cli/docker_cli_exec_unix_test.go +++ b/integration-cli/docker_cli_exec_unix_test.go @@ -49,7 +49,7 @@ func (s *DockerSuite) TestExecTTY(c *check.C) { c.Assert(err, checker.IsNil) defer p.Close() - _, err = p.Write([]byte("cat /foo && sleep 2 && exit\n")) + _, err = p.Write([]byte("cat /foo && exit\n")) c.Assert(err, checker.IsNil) chErr := make(chan error)