瀏覽代碼

Fix race in TestRun

Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
Michael Crosby 11 年之前
父節點
當前提交
ad9710685c
共有 3 個文件被更改,包括 11 次插入5 次删除
  1. 1 1
      container.go
  2. 3 1
      execdriver/driver.go
  3. 7 3
      execdriver/lxc/driver.go

+ 1 - 1
container.go

@@ -1171,7 +1171,6 @@ func (container *Container) monitor() {
 	exitCode := container.process.GetExitCode()
 	container.State.SetStopped(exitCode)
 
-	close(container.waitLock)
 	if err := container.ToDisk(); err != nil {
 		// FIXME: there is a race condition here which causes this to fail during the unit tests.
 		// If another goroutine was waiting for Wait() to return before removing the container's root
@@ -1181,6 +1180,7 @@ func (container *Container) monitor() {
 		// FIXME: why are we serializing running state to disk in the first place?
 		//log.Printf("%s: Failed to dump configuration to the disk: %s", container.ID, err)
 	}
+	close(container.waitLock)
 }
 
 func (container *Container) cleanup() {

+ 3 - 1
execdriver/driver.go

@@ -44,6 +44,8 @@ func (c *Process) Pid() int {
 }
 
 func (c *Process) GetExitCode() int {
+	if c.ProcessState == nil {
+		return -1
+	}
 	return c.ProcessState.Sys().(syscall.WaitStatus).ExitStatus()
-	return -1
 }

+ 7 - 3
execdriver/lxc/driver.go

@@ -139,9 +139,13 @@ func (d *driver) waitForStart(c *execdriver.Process) error {
 	// Note: The container can run and finish correctly before
 	// the end of this loop
 	for now := time.Now(); time.Since(now) < 5*time.Second; {
-		// If the process dies while waiting for it, just return
-		if c.ProcessState != nil && c.ProcessState.Exited() {
-			return nil
+		select {
+		case <-c.WaitLock:
+			// If the process dies while waiting for it, just return
+			if c.ProcessState != nil && c.ProcessState.Exited() {
+				return nil
+			}
+		default:
 		}
 
 		output, err = d.getInfo(c)