Merge pull request #20589 from coolljt0725/fix_restore_terminal

cli: move setRawTerminal and restoreTerminal to holdHijackedConnection
Upstream-commit: 641bd7652f0f65712bab60f7a5f8e299dcb5643c
Component: engine
This commit is contained in:
Alexander Morozov
2016-03-24 22:42:24 -07:00
5 changed files with 63 additions and 37 deletions

View File

@ -158,6 +158,8 @@ func (cli *DockerCli) CmdRun(args ...string) error {
var (
waitDisplayID chan struct{}
errCh chan error
cancelFun context.CancelFunc
ctx context.Context
)
if !config.AttachStdout && !config.AttachStderr {
// Make this asynchronous to allow the client to write to stdin before having to read the ID
@ -170,8 +172,8 @@ func (cli *DockerCli) CmdRun(args ...string) error {
if *flAutoRemove && (hostConfig.RestartPolicy.IsAlways() || hostConfig.RestartPolicy.IsOnFailure()) {
return ErrConflictRestartPolicyAndAutoRemove
}
if config.AttachStdin || config.AttachStdout || config.AttachStderr {
attach := config.AttachStdin || config.AttachStdout || config.AttachStderr
if attach {
var (
out, stderr io.Writer
in io.ReadCloser
@ -207,14 +209,9 @@ func (cli *DockerCli) CmdRun(args ...string) error {
if err != nil {
return err
}
if in != nil && config.Tty {
if err := cli.setRawTerminal(); err != nil {
return err
}
defer cli.restoreTerminal(in)
}
ctx, cancelFun = context.WithCancel(context.Background())
errCh = promise.Go(func() error {
return cli.holdHijackedConnection(config.Tty, in, out, stderr, resp)
return cli.holdHijackedConnection(ctx, config.Tty, in, out, stderr, resp)
})
}
@ -228,6 +225,14 @@ func (cli *DockerCli) CmdRun(args ...string) error {
//start the container
if err := cli.client.ContainerStart(context.Background(), createResponse.ID); err != nil {
// If we have holdHijackedConnection, we should notify
// holdHijackedConnection we are going to exit and wait
// to avoid the terminal are not restored.
if attach {
cancelFun()
<-errCh
}
cmd.ReportError(err.Error(), false)
return runStartContainerErr(err)
}