Use time.AfterFunc to avoid need for goroutine in WaitTimeout (#4702)
This commit is contained in:
parent
358920e6ba
commit
a086ea6989
|
@ -153,22 +153,24 @@ func RunTimeout(c *exec.Cmd, timeout time.Duration) error {
|
||||||
// It assumes the command has already been started.
|
// It assumes the command has already been started.
|
||||||
// If the command times out, it attempts to kill the process.
|
// If the command times out, it attempts to kill the process.
|
||||||
func WaitTimeout(c *exec.Cmd, timeout time.Duration) error {
|
func WaitTimeout(c *exec.Cmd, timeout time.Duration) error {
|
||||||
timer := time.NewTimer(timeout)
|
timer := time.AfterFunc(timeout, func() {
|
||||||
done := make(chan error)
|
err := c.Process.Kill()
|
||||||
go func() { done <- c.Wait() }()
|
if err != nil {
|
||||||
select {
|
|
||||||
case err := <-done:
|
|
||||||
timer.Stop()
|
|
||||||
return err
|
|
||||||
case <-timer.C:
|
|
||||||
if err := c.Process.Kill(); err != nil {
|
|
||||||
log.Printf("E! FATAL error killing process: %s", err)
|
log.Printf("E! FATAL error killing process: %s", err)
|
||||||
return err
|
return
|
||||||
}
|
}
|
||||||
// wait for the command to return after killing it
|
})
|
||||||
<-done
|
|
||||||
|
err := c.Wait()
|
||||||
|
isTimeout := timer.Stop()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
} else if isTimeout == false {
|
||||||
return TimeoutErr
|
return TimeoutErr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// RandomSleep will sleep for a random amount of time up to max.
|
// RandomSleep will sleep for a random amount of time up to max.
|
||||||
|
|
Loading…
Reference in New Issue