浏览代码

Fix start container failed hijack the stdin of terminal

Signed-off-by: Lei Jitang <leijitang@huawei.com>
Lei Jitang 10 年之前
父节点
当前提交
518fdf972d
共有 1 个文件被更改,包括 11 次插入8 次删除
  1. 11 8
      api/client/commands.go

+ 11 - 8
api/client/commands.go

@@ -712,6 +712,16 @@ func (cli *DockerCli) CmdStart(args ...string) error {
 	utils.ParseFlags(cmd, args, true)
 
 	hijacked := make(chan io.Closer)
+	// Block the return until the chan gets closed
+	defer func() {
+		log.Debugf("CmdStart() returned, defer waiting for hijack to finish.")
+		if _, ok := <-hijacked; ok {
+			log.Errorf("Hijack did not finish (chan still open)")
+		}
+		if *openStdin || *attach {
+			cli.in.Close()
+		}
+	}()
 
 	if *attach || *openStdin {
 		if cmd.NArg() > 1 {
@@ -769,26 +779,19 @@ func (cli *DockerCli) CmdStart(args ...string) error {
 		}
 	}
 
-	var encounteredError error
 	for _, name := range cmd.Args() {
 		_, _, err := readBody(cli.call("POST", "/containers/"+name+"/start", nil, false))
 		if err != nil {
 			if !*attach && !*openStdin {
 				fmt.Fprintf(cli.err, "%s\n", err)
 			}
-			encounteredError = fmt.Errorf("Error: failed to start one or more containers")
+			return fmt.Errorf("Error: failed to start one or more containers")
 		} else {
 			if !*attach && !*openStdin {
 				fmt.Fprintf(cli.out, "%s\n", name)
 			}
 		}
 	}
-	if encounteredError != nil {
-		if *openStdin || *attach {
-			cli.in.Close()
-		}
-		return encounteredError
-	}
 
 	if *openStdin || *attach {
 		if tty && cli.isTerminalOut {