mirror of https://github.com/docker/docs.git
Merge pull request #916 from dotcloud/race_attach-fix
- Runtime: Fix race condition within Run command when attaching.
This commit is contained in:
commit
0a9ac63a05
39
commands.go
39
commands.go
|
@ -1061,6 +1061,10 @@ func (cli *DockerCli) CmdAttach(args ...string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !container.State.Running {
|
||||||
|
return fmt.Errorf("Impossible to attach to a stopped container, start it first")
|
||||||
|
}
|
||||||
|
|
||||||
splitStderr := container.Config.Tty
|
splitStderr := container.Config.Tty
|
||||||
|
|
||||||
connections := 1
|
connections := 1
|
||||||
|
@ -1260,16 +1264,6 @@ func (cli *DockerCli) CmdRun(args ...string) error {
|
||||||
fmt.Fprintln(os.Stderr, "WARNING: ", warning)
|
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
|
//start the container
|
||||||
_, _, err = cli.call("POST", "/containers/"+out.ID+"/start", nil)
|
_, _, err = cli.call("POST", "/containers/"+out.ID+"/start", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1278,19 +1272,11 @@ func (cli *DockerCli) CmdRun(args ...string) error {
|
||||||
|
|
||||||
if !config.AttachStdout && !config.AttachStderr {
|
if !config.AttachStdout && !config.AttachStderr {
|
||||||
fmt.Println(out.ID)
|
fmt.Println(out.ID)
|
||||||
}
|
} else {
|
||||||
if connections > 0 {
|
|
||||||
chErrors := make(chan error, connections)
|
|
||||||
if config.Tty {
|
if config.Tty {
|
||||||
cli.monitorTtySize(out.ID)
|
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 := url.Values{}
|
||||||
v.Set("logs", "1")
|
v.Set("logs", "1")
|
||||||
v.Set("stream", "1")
|
v.Set("stream", "1")
|
||||||
|
@ -1301,19 +1287,12 @@ func (cli *DockerCli) CmdRun(args ...string) error {
|
||||||
if config.AttachStdout {
|
if config.AttachStdout {
|
||||||
v.Set("stdout", "1")
|
v.Set("stdout", "1")
|
||||||
}
|
}
|
||||||
if !splitStderr && config.AttachStderr {
|
if config.AttachStderr {
|
||||||
v.Set("stderr", "1")
|
v.Set("stderr", "1")
|
||||||
}
|
}
|
||||||
go func() {
|
if err := cli.hijack("POST", "/containers/"+out.ID+"/attach?"+v.Encode(), config.Tty, os.Stdin, os.Stdout); err != nil {
|
||||||
chErrors <- cli.hijack("POST", "/containers/"+out.ID+"/attach?"+v.Encode(), config.Tty, os.Stdin, os.Stdout)
|
utils.Debugf("Error hijack: %s", err)
|
||||||
}()
|
return err
|
||||||
for connections > 0 {
|
|
||||||
err := <-chErrors
|
|
||||||
if err != nil {
|
|
||||||
utils.Debugf("Error hijack: %s", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
connections -= 1
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -942,9 +942,6 @@ func (srv *Server) ContainerAttach(name string, logs, stream, stdin, stdout, std
|
||||||
if container.State.Ghost {
|
if container.State.Ghost {
|
||||||
return fmt.Errorf("Impossible to attach to a ghost container")
|
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 (
|
var (
|
||||||
cStdin io.ReadCloser
|
cStdin io.ReadCloser
|
||||||
|
|
Loading…
Reference in New Issue