From 79a77a396e003d0f5827c48d1b179f87c1542311 Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Fri, 31 Oct 2014 01:10:35 +0200 Subject: [PATCH] Wait for hijack on docker start command MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With current implementation there was a possibility that /start responds quicker than /attach, meaning that some output would be clipped. Fixed so the implementation matches with `docker run`. This also fixes the flaky test results for TestCreateEchoStdout. Signed-off-by: Tõnis Tiigi (github: tonistiigi) --- api/client/commands.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/api/client/commands.go b/api/client/commands.go index da1eab27c9..7ce208d466 100644 --- a/api/client/commands.go +++ b/api/client/commands.go @@ -622,6 +622,8 @@ func (cli *DockerCli) CmdStart(args ...string) error { return nil } + hijacked := make(chan io.Closer) + if *attach || *openStdin { if cmd.NArg() > 1 { return fmt.Errorf("You cannot start and attach multiple containers at once.") @@ -658,8 +660,24 @@ func (cli *DockerCli) CmdStart(args ...string) error { v.Set("stderr", "1") cErr = promise.Go(func() error { - return cli.hijack("POST", "/containers/"+cmd.Arg(0)+"/attach?"+v.Encode(), tty, in, cli.out, cli.err, nil, nil) + return cli.hijack("POST", "/containers/"+cmd.Arg(0)+"/attach?"+v.Encode(), tty, in, cli.out, cli.err, hijacked, nil) }) + } else { + close(hijacked) + } + + // Acknowledge the hijack before starting + select { + case closer := <-hijacked: + // Make sure that the hijack gets closed when returning (results + // in closing the hijack chan and freeing server's goroutines) + if closer != nil { + defer closer.Close() + } + case err := <-cErr: + if err != nil { + return err + } } var encounteredError error