Skip to content

Commit

Permalink
Prevent 'no child process' err from breaking heartbeats (#222)
Browse files Browse the repository at this point in the history
The golang stdlib (and our RunAndWait implementation) has a race where if
we call `Wait` after the child process has already exited and been cleaned
up then an error bubbles up through the stack. We don't need these to break
heartbeat TTLs.
  • Loading branch information
tgross authored Sep 26, 2016
1 parent bad44bd commit 07f9913
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/joyent/containerpilot/utils"
)

const errNoChild = "wait: no child processes"

// Command wraps an os/exec.Cmd with a timeout, logging, and arg parsing.
type Command struct {
Name string // this gets used only in logs, defaults to Exec
Expand Down Expand Up @@ -72,6 +74,10 @@ func RunAndWait(c *Command, fields log.Fields) (int, error) {
}
log.Debugf("%s.Cmd.Run", c.Name)
if err := c.Cmd.Run(); err != nil {
if err.Error() == errNoChild {
log.Debugf(err.Error())
return 0, nil // process exited cleanly before we hit wait4
}
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
return status.ExitStatus(), err
Expand Down Expand Up @@ -190,6 +196,10 @@ func (c *Command) waitForTimeout() error {
}
log.Debugf("%s.run waiting for PID %d: ", c.Name, cmd.Process.Pid)
if _, err := cmd.Process.Wait(); err != nil {
if err.Error() == errNoChild {
log.Debugf(err.Error())
return nil // process exited cleanly before we hit wait4
}
log.Errorf("%s exited with error: %v", c.Name, err)
return err
}
Expand Down

0 comments on commit 07f9913

Please sign in to comment.