Skip to content

Commit

Permalink
Merge pull request moby#18412 from aaronlehmann/runcommand-race
Browse files Browse the repository at this point in the history
Fix race in RunCommandWithOutputForDuration
  • Loading branch information
LK4D4 committed Dec 4, 2015
2 parents a65502f + 2704fd9 commit 7c1c965
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions pkg/integration/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,25 @@ func RunCommandWithOutputForDuration(cmd *exec.Cmd, duration time.Duration) (out
}
cmd.Stderr = &outputBuffer

done := make(chan error)

// Start the command in the main thread..
err = cmd.Start()
if err != nil {
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() {
// 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 {
Expand All @@ -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)
}
timedOut = true
break
case err = <-done:
break
case info := <-done:
err = info.exitErr
exitCode = info.exitCode
}
output = outputBuffer.String()
return
Expand Down

0 comments on commit 7c1c965

Please sign in to comment.