Skip to content

Commit

Permalink
Merge pull request #23 from mpokryva/fix-signal-race
Browse files Browse the repository at this point in the history
Fix race condition in signal handling, and error handling
  • Loading branch information
josegonzalez authored Dec 5, 2020
2 parents 7aca703 + f5af090 commit 357fd3d
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions basher.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ func exitStatus(err error) (int, error) {
// There is no platform independent way to retrieve
// the exit code, but the following will work on Unix
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
return int(status.ExitStatus()), nil
if status.ExitStatus() != -1 {
return status.ExitStatus(), err
} else {
// The process hasn't exited or was terminated by a signal.
return int(status), err
}
}
}
return 0, err
Expand Down Expand Up @@ -260,17 +265,23 @@ func (c *Context) Run(command string, args []string) (int, error) {
cmd.Stdin = c.Stdin
cmd.Stdout = c.Stdout
cmd.Stderr = c.Stderr
if err2 := cmd.Start(); err2 != nil {
return 0, err2
if err := cmd.Start(); err != nil {
return 0, err
}

errChan := make(chan error, 1)
go func() {
for sig := range signals {
cmd.Process.Signal(sig)
if cmd.ProcessState != nil && !cmd.ProcessState.Exited() {
cmd.Process.Signal(sig)
if sig != syscall.SIGCHLD {
err = cmd.Process.Signal(sig)
if err != nil {
errChan <- err
}
}
}
}()
return exitStatus(cmd.Wait())
go func() {
errChan <- cmd.Wait()
}()
err = <-errChan
return exitStatus(err)
}

0 comments on commit 357fd3d

Please sign in to comment.