Browse Source

Merge pull request #18412 from aaronlehmann/runcommand-race

Fix race in RunCommandWithOutputForDuration
Alexander Morozov 9 years ago
parent
commit
7c1c96551d
1 changed files with 14 additions and 8 deletions
  1. 14 8
      pkg/integration/utils.go

+ 14 - 8
pkg/integration/utils.go

@@ -104,19 +104,25 @@ func RunCommandWithOutputForDuration(cmd *exec.Cmd, duration time.Duration) (out
 	}
 	}
 	cmd.Stderr = &outputBuffer
 	cmd.Stderr = &outputBuffer
 
 
-	done := make(chan error)
-
 	// Start the command in the main thread..
 	// Start the command in the main thread..
 	err = cmd.Start()
 	err = cmd.Start()
 	if err != nil {
 	if err != nil {
 		err = fmt.Errorf("Fail to start command %v : %v", cmd, err)
 		err = fmt.Errorf("Fail to start command %v : %v", cmd, err)
 	}
 	}
 
 
+	type exitInfo struct {
+		exitErr  error
+		exitCode int
+	}
+
+	done := make(chan exitInfo, 1)
+
 	go func() {
 	go func() {
 		// And wait for it to exit in the goroutine :)
 		// And wait for it to exit in the goroutine :)
-		exitErr := cmd.Wait()
-		exitCode = ProcessExitCode(exitErr)
-		done <- exitErr
+		info := exitInfo{}
+		info.exitErr = cmd.Wait()
+		info.exitCode = ProcessExitCode(info.exitErr)
+		done <- info
 	}()
 	}()
 
 
 	select {
 	select {
@@ -126,9 +132,9 @@ func RunCommandWithOutputForDuration(cmd *exec.Cmd, duration time.Duration) (out
 			fmt.Printf("failed to kill (pid=%d): %v\n", cmd.Process.Pid, killErr)
 			fmt.Printf("failed to kill (pid=%d): %v\n", cmd.Process.Pid, killErr)
 		}
 		}
 		timedOut = true
 		timedOut = true
-		break
-	case err = <-done:
-		break
+	case info := <-done:
+		err = info.exitErr
+		exitCode = info.exitCode
 	}
 	}
 	output = outputBuffer.String()
 	output = outputBuffer.String()
 	return
 	return