From e8b35a8c20a68c852a5819dde39dc077acc11159 Mon Sep 17 00:00:00 2001 From: Valentin Rothberg Date: Wed, 18 Jan 2023 13:36:25 +0100 Subject: [PATCH] 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 --- libpod/oci_conmon_common.go | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/libpod/oci_conmon_common.go b/libpod/oci_conmon_common.go index dc370928fa..5ab1a335d8 100644 --- a/libpod/oci_conmon_common.go +++ b/libpod/oci_conmon_common.go @@ -949,31 +949,20 @@ func waitContainerStop(ctr *Container, timeout time.Duration) error { // Wait for a given PID to stop func waitPidStop(pid int, timeout time.Duration) error { - done := make(chan struct{}) - chControl := make(chan struct{}) - go func() { - for { - select { - case <-chControl: - return - default: - if err := unix.Kill(pid, 0); err != nil { - if err == unix.ESRCH { - close(done) - return - } - logrus.Errorf("Pinging PID %d with signal 0: %v", pid, err) + timer := time.NewTimer(timeout) + for { + select { + case <-timer.C: + return fmt.Errorf("given PID did not die within timeout") + default: + if err := unix.Kill(pid, 0); err != nil { + if err == unix.ESRCH { + return nil } - 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") } }