Just add defer timer.Stop() after NewTimer... instead of explicit time.Stop() in the done case.

On 04/05/2017 17:50, yuri.shakhma...@gmail.com wrote:
Hello, all!

There is a part of code:... // RunTimeout runs the given command with the given timeout. // If the command times out, it attempts to kill the process. func RunTimeout(c *exec.Cmd, timeout time.Duration) error {
        if err := c.Start(); err != nil {
               return err
        }
        return WaitTimeout(c, timeout)
}

// WaitTimeout waits for the given command to finish with a timeout. // It assumes the command has already been started. // If the command times out, it attempts to kill the process. func WaitTimeout(c *exec.Cmd, timeout time.Duration) error {
        timer := time.NewTimer(timeout)
        done := make(chan error)
        go func() { done <- c.Wait() }()
        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)
                      return err
               }
               // wait for the command to return after killing it <-done
               return errors.New("Command timed out.")
        }
}
...
I think that there will be a goroutine leak in WaitTimeout function when
c.Process.Kill() return error. Is it true?

Is there any idea for reimplementing this code?

--
Best regards, Yuri
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com <mailto:golang-nuts+unsubscr...@googlegroups.com>.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to