Merge pull request #602 from runcom/busy-loop

oci: remove busy loop
This commit is contained in:
Mrunal Patel 2017-06-15 10:29:34 -07:00 committed by GitHub
commit 2b8e3a0d0f

View file

@ -540,13 +540,26 @@ func (r *Runtime) StopContainer(c *Container, timeout int64) error {
timeout = 10 timeout = 10
} }
done := make(chan struct{}) done := make(chan struct{})
// we could potentially re-use "done" channel to exit the loop on timeout
// but we use another channel "chControl" so that we won't never incur in the
// case the "done" channel is closed in the "default" select case and we also
// reach the timeout in the select below. If that happens we could raise
// a panic closing a closed channel so better be safe and use another new
// channel just to control the loop.
chControl := make(chan struct{})
go func() { go func() {
for { for {
// Check if the process is still around select {
err := unix.Kill(c.state.Pid, 0) case <-chControl:
if err == syscall.ESRCH { return
close(done) default:
break // Check if the process is still around
err := unix.Kill(c.state.Pid, 0)
if err == syscall.ESRCH {
close(done)
return
}
time.Sleep(100 * time.Millisecond)
} }
} }
}() }()
@ -554,6 +567,7 @@ func (r *Runtime) StopContainer(c *Container, timeout int64) error {
case <-done: case <-done:
return nil return nil
case <-time.After(time.Duration(timeout) * time.Second): case <-time.After(time.Duration(timeout) * time.Second):
close(chControl)
err := unix.Kill(c.state.Pid, syscall.SIGKILL) err := unix.Kill(c.state.Pid, syscall.SIGKILL)
if err != nil && err != syscall.ESRCH { if err != nil && err != syscall.ESRCH {
return fmt.Errorf("failed to kill process: %v", err) return fmt.Errorf("failed to kill process: %v", err)