waitPidStop: simplify code

The code can be simplified by using a timer directly.

[NO NEW TESTS NEEDED] - should not change behavior.

Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
This commit is contained in:
Valentin Rothberg 2023-01-18 13:36:25 +01:00
parent 17f89c97bd
commit e8b35a8c20
1 changed files with 11 additions and 22 deletions

View File

@ -949,31 +949,20 @@ func waitContainerStop(ctr *Container, timeout time.Duration) error {
// Wait for a given PID to stop // Wait for a given PID to stop
func waitPidStop(pid int, timeout time.Duration) error { func waitPidStop(pid int, timeout time.Duration) error {
done := make(chan struct{}) timer := time.NewTimer(timeout)
chControl := make(chan struct{}) for {
go func() { select {
for { case <-timer.C:
select { return fmt.Errorf("given PID did not die within timeout")
case <-chControl: default:
return if err := unix.Kill(pid, 0); err != nil {
default: if err == unix.ESRCH {
if err := unix.Kill(pid, 0); err != nil { return nil
if err == unix.ESRCH {
close(done)
return
}
logrus.Errorf("Pinging PID %d with signal 0: %v", pid, err)
} }
time.Sleep(100 * time.Millisecond) logrus.Errorf("Pinging PID %d with signal 0: %v", pid, err)
} }
time.Sleep(100 * time.Millisecond)
} }
}()
select {
case <-done:
return nil
case <-time.After(timeout):
close(chControl)
return fmt.Errorf("given PIDs did not die within timeout")
} }
} }