Skip to content

Commit

Permalink
Fix data race
Browse files Browse the repository at this point in the history
We try to read cmd.Process in the thread that handles incoming
signals, but there's no synchronization between that goroutine and the
one that sets cmd.Process to a non-nil value.

This might be overkill, e.g. there may be a way to write this with
less code, but I confirmed that this patch addresses the
race when shutting down the program.

Fixes #158.
  • Loading branch information
kevinburke committed Oct 12, 2017
1 parent 1a2907d commit 2ffc675
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions cli/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,22 +161,33 @@ func ExecCommand(app *kingpin.Application, input ExecCommandInput) {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

if err := cmd.Start(); err != nil {
app.Errorf("%v", err)
return
}
// wait for the command to finish
waitCh := make(chan error)
go func() {
sig := <-input.Signals
if cmd.Process != nil {
cmd.Process.Signal(sig)
}
waitCh <- cmd.Wait()
}()

var waitStatus syscall.WaitStatus
if err := cmd.Run(); err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
waitStatus = exitError.Sys().(syscall.WaitStatus)
os.Exit(waitStatus.ExitStatus())
}
if err != nil {
app.Errorf("%v", err)
return
for {
select {
case sig := <-input.Signals:
if err = cmd.Process.Signal(sig); err != nil {
app.Errorf("%v", err)
return
}
case err := <-waitCh:
var waitStatus syscall.WaitStatus
if exitError, ok := err.(*exec.ExitError); ok {
waitStatus = exitError.Sys().(syscall.WaitStatus)
os.Exit(waitStatus.ExitStatus())
}
if err != nil {
app.Errorf("%v", err)
return
}
}
}
}
Expand Down

0 comments on commit 2ffc675

Please sign in to comment.