From c106ed32ea7613573a2081d47ad2498429ac86f2 Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Mon, 17 Jun 2013 15:40:04 -0700 Subject: [PATCH 1/3] Move the attach prevention from server to client --- commands.go | 4 ++++ server.go | 3 --- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/commands.go b/commands.go index ce15fd6cf1..4297ac0c11 100644 --- a/commands.go +++ b/commands.go @@ -1058,6 +1058,10 @@ func (cli *DockerCli) CmdAttach(args ...string) error { return err } + if !container.State.Running { + return fmt.Errorf("Impossible to attach to a stopped container, start it first") + } + splitStderr := container.Config.Tty connections := 1 diff --git a/server.go b/server.go index 30e3ec6b3a..ece6a93ceb 100644 --- a/server.go +++ b/server.go @@ -930,9 +930,6 @@ func (srv *Server) ContainerAttach(name string, logs, stream, stdin, stdout, std if container.State.Ghost { return fmt.Errorf("Impossible to attach to a ghost container") } - if !container.State.Running { - return fmt.Errorf("Impossible to attach to a stopped container, start it first") - } var ( cStdin io.ReadCloser From 2b6ca3872883dcb487d8a39a1a8530be6a62f947 Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Mon, 17 Jun 2013 15:45:08 -0700 Subject: [PATCH 2/3] Remove Run race condition --- commands.go | 33 ++++++--------------------------- 1 file changed, 6 insertions(+), 27 deletions(-) diff --git a/commands.go b/commands.go index 4297ac0c11..fb8bb528bb 100644 --- a/commands.go +++ b/commands.go @@ -1261,16 +1261,6 @@ func (cli *DockerCli) CmdRun(args ...string) error { fmt.Fprintln(os.Stderr, "WARNING: ", warning) } - splitStderr := !config.Tty - - connections := 0 - if config.AttachStdin || config.AttachStdout || (!splitStderr && config.AttachStderr) { - connections += 1 - } - if splitStderr && config.AttachStderr { - connections += 1 - } - //start the container _, _, err = cli.call("POST", "/containers/"+out.ID+"/start", nil) if err != nil { @@ -1279,19 +1269,12 @@ func (cli *DockerCli) CmdRun(args ...string) error { if !config.AttachStdout && !config.AttachStderr { fmt.Println(out.ID) - } - if connections > 0 { - chErrors := make(chan error, connections) + } else { + chErrors := make(chan error) if config.Tty { cli.monitorTtySize(out.ID) } - if splitStderr && config.AttachStderr { - go func() { - chErrors <- cli.hijack("POST", "/containers/"+out.ID+"/attach?logs=1&stream=1&stderr=1", config.Tty, nil, os.Stderr) - }() - } - v := url.Values{} v.Set("logs", "1") v.Set("stream", "1") @@ -1302,19 +1285,15 @@ func (cli *DockerCli) CmdRun(args ...string) error { if config.AttachStdout { v.Set("stdout", "1") } - if !splitStderr && config.AttachStderr { + if config.AttachStderr { v.Set("stderr", "1") } go func() { chErrors <- cli.hijack("POST", "/containers/"+out.ID+"/attach?"+v.Encode(), config.Tty, os.Stdin, os.Stdout) }() - for connections > 0 { - err := <-chErrors - if err != nil { - utils.Debugf("Error hijack: %s", err) - return err - } - connections -= 1 + if err := <-chErrors; err != nil { + utils.Debugf("Error hijack: %s", err) + return err } } return nil From e2d034e48858d0afe9ee0f88f04e40cbf95ab8ba Mon Sep 17 00:00:00 2001 From: "Guillaume J. Charmes" Date: Tue, 18 Jun 2013 10:06:26 -0700 Subject: [PATCH 3/3] Remove useless goroutine --- commands.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/commands.go b/commands.go index 847b5d3b33..0703f4ebe0 100644 --- a/commands.go +++ b/commands.go @@ -1270,7 +1270,6 @@ func (cli *DockerCli) CmdRun(args ...string) error { if !config.AttachStdout && !config.AttachStderr { fmt.Println(out.ID) } else { - chErrors := make(chan error) if config.Tty { cli.monitorTtySize(out.ID) } @@ -1288,10 +1287,7 @@ func (cli *DockerCli) CmdRun(args ...string) error { if config.AttachStderr { v.Set("stderr", "1") } - go func() { - chErrors <- cli.hijack("POST", "/containers/"+out.ID+"/attach?"+v.Encode(), config.Tty, os.Stdin, os.Stdout) - }() - if err := <-chErrors; err != nil { + if err := cli.hijack("POST", "/containers/"+out.ID+"/attach?"+v.Encode(), config.Tty, os.Stdin, os.Stdout); err != nil { utils.Debugf("Error hijack: %s", err) return err }