diff --git a/reaper/reaper.go b/reaper/reaper.go index 40ddd8e..38894fe 100644 --- a/reaper/reaper.go +++ b/reaper/reaper.go @@ -65,7 +65,6 @@ func (m *Monitor) Start(c *exec.Cmd) error { c: c, exitCh: make(chan int, 1), } - // make sure we register the command first before starting the process m.mu.Lock() // start the process if err := rc.c.Start(); err != nil { @@ -87,7 +86,9 @@ func (m *Monitor) Run(c *exec.Cmd) error { } func (m *Monitor) Wait(c *exec.Cmd) (int, error) { + m.mu.Lock() rc, ok := m.cmds[c.Process.Pid] + m.mu.Unlock() if !ok { return 255, fmt.Errorf("process does not exist") } diff --git a/vendor/github.com/crosbymichael/go-runc/monitor.go b/vendor/github.com/crosbymichael/go-runc/monitor.go new file mode 100644 index 0000000..83eebb0 --- /dev/null +++ b/vendor/github.com/crosbymichael/go-runc/monitor.go @@ -0,0 +1,50 @@ +package runc + +import ( + "os/exec" + "syscall" +) + +var Monitor ProcessMonitor = &defaultMonitor{} + +// ProcessMonitor is an interface for process monitoring +// +// It allows daemons using go-runc to have a SIGCHLD handler +// to handle exits without introducing races between the handler +// and go's exec.Cmd +// These methods should match the methods exposed by exec.Cmd to provide +// a consistent experience for the caller +type ProcessMonitor interface { + Output(*exec.Cmd) ([]byte, error) + CombinedOutput(*exec.Cmd) ([]byte, error) + Run(*exec.Cmd) error + Start(*exec.Cmd) error + Wait(*exec.Cmd) (int, error) +} + +type defaultMonitor struct { +} + +func (m *defaultMonitor) Output(c *exec.Cmd) ([]byte, error) { + return c.Output() +} + +func (m *defaultMonitor) CombinedOutput(c *exec.Cmd) ([]byte, error) { + return c.CombinedOutput() +} + +func (m *defaultMonitor) Run(c *exec.Cmd) error { + return c.Run() +} + +func (m *defaultMonitor) Start(c *exec.Cmd) error { + return c.Start() +} + +func (m *defaultMonitor) Wait(c *exec.Cmd) (int, error) { + status, err := c.Process.Wait() + if err != nil { + return -1, err + } + return status.Sys().(syscall.WaitStatus).ExitStatus(), nil +}