瀏覽代碼

Fix race condition when exec with tty

I can reproduce this easily on one of my servers,
`docker exec -ti my_cont ls` will not print anything,
without `-t` it acts normally.

Signed-off-by: Qiang Huang <h.huangqiang@huawei.com>
Qiang Huang 9 年之前
父節點
當前提交
a444b5f60c
共有 1 個文件被更改,包括 6 次插入4 次删除
  1. 6 4
      daemon/execdriver/native/driver.go

+ 6 - 4
daemon/execdriver/native/driver.go

@@ -441,12 +441,12 @@ type TtyConsole struct {
 }
 }
 
 
 // NewTtyConsole returns a new TtyConsole struct.
 // NewTtyConsole returns a new TtyConsole struct.
-func NewTtyConsole(console libcontainer.Console, pipes *execdriver.Pipes) (*TtyConsole, error) {
+func NewTtyConsole(console libcontainer.Console, pipes *execdriver.Pipes, wg *sync.WaitGroup) (*TtyConsole, error) {
 	tty := &TtyConsole{
 	tty := &TtyConsole{
 		console: console,
 		console: console,
 	}
 	}
 
 
-	if err := tty.AttachPipes(pipes); err != nil {
+	if err := tty.AttachPipes(pipes, wg); err != nil {
 		tty.Close()
 		tty.Close()
 		return nil, err
 		return nil, err
 	}
 	}
@@ -460,8 +460,10 @@ func (t *TtyConsole) Resize(h, w int) error {
 }
 }
 
 
 // AttachPipes attaches given pipes to TtyConsole
 // AttachPipes attaches given pipes to TtyConsole
-func (t *TtyConsole) AttachPipes(pipes *execdriver.Pipes) error {
+func (t *TtyConsole) AttachPipes(pipes *execdriver.Pipes, wg *sync.WaitGroup) error {
+	wg.Add(1)
 	go func() {
 	go func() {
+		defer wg.Done()
 		if wb, ok := pipes.Stdout.(interface {
 		if wb, ok := pipes.Stdout.(interface {
 			CloseWriters() error
 			CloseWriters() error
 		}); ok {
 		}); ok {
@@ -501,7 +503,7 @@ func setupPipes(container *configs.Config, processConfig *execdriver.ProcessConf
 		if err != nil {
 		if err != nil {
 			return writers, err
 			return writers, err
 		}
 		}
-		term, err := NewTtyConsole(cons, pipes)
+		term, err := NewTtyConsole(cons, pipes, wg)
 		if err != nil {
 		if err != nil {
 			return writers, err
 			return writers, err
 		}
 		}