Merge pull request #6189 from fuweid/followup-6166
This commit is contained in:
commit
7020719646
@ -119,23 +119,33 @@ func (m *Monitor) Wait(c *exec.Cmd, ec chan runc.Exit) (int, error) {
|
|||||||
|
|
||||||
// WaitTimeout is used to skip the blocked command and kill the left process.
|
// WaitTimeout is used to skip the blocked command and kill the left process.
|
||||||
func (m *Monitor) WaitTimeout(c *exec.Cmd, ec chan runc.Exit, timeout time.Duration) (int, error) {
|
func (m *Monitor) WaitTimeout(c *exec.Cmd, ec chan runc.Exit, timeout time.Duration) (int, error) {
|
||||||
sch := make(chan int)
|
type exitStatusWrapper struct {
|
||||||
ech := make(chan error)
|
status int
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
// capacity can make sure that the following goroutine will not be
|
||||||
|
// blocked if there is no receiver when timeout.
|
||||||
|
waitCh := make(chan *exitStatusWrapper, 1)
|
||||||
go func() {
|
go func() {
|
||||||
|
defer close(waitCh)
|
||||||
|
|
||||||
status, err := m.Wait(c, ec)
|
status, err := m.Wait(c, ec)
|
||||||
sch <- status
|
waitCh <- &exitStatusWrapper{
|
||||||
if err != nil {
|
status: status,
|
||||||
ech <- err
|
err: err,
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
timer := time.NewTimer(timeout)
|
||||||
|
defer timer.Stop()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-time.After(timeout):
|
case <-timer.C:
|
||||||
syscall.Kill(c.Process.Pid, syscall.SIGKILL)
|
syscall.Kill(c.Process.Pid, syscall.SIGKILL)
|
||||||
return 0, errors.Errorf("timeout %ds for cmd(pid=%d): %s, %s", timeout/time.Second, c.Process.Pid, c.Path, c.Args)
|
return 0, errors.Errorf("timeout %v for cmd(pid=%d): %s, %s", timeout, c.Process.Pid, c.Path, c.Args)
|
||||||
case status := <-sch:
|
case res := <-waitCh:
|
||||||
return status, nil
|
return res.status, res.err
|
||||||
case err := <-ech:
|
|
||||||
return -1, err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user