Subject: [bug#75829] [PATCH 1/2] services: user-processes: Simplify and streamline ‘stop’ action.

From: Ludovic Courtès <[email-masked]>
Date: Sat, 25 Jan 2025 15:53:36 +0100

* gnu/services/shepherd.scm (user-processes-shepherd-service): In ‘stop’
action, remove ‘sleep*’, which is unnecessary when using Fibers, and
remove the ‘reap-children’ loop and its ‘waitpid’ call, which is
redundant with ‘waitpid’ calls made by shepherd itself and could cause
confusion.

Change-Id: I0df1733f0cbe781a0ad5fef4830d903483e0da27
---
 gnu/services/shepherd.scm | 31 +++----------------------------
 1 file changed, 3 insertions(+), 28 deletions(-)

diff --git a/gnu/services/shepherd.scm b/gnu/services/shepherd.scm
index 0de3c9c55c..d4406e9ba9 100644
--- a/gnu/services/shepherd.scm
+++ b/gnu/services/shepherd.scm
@@ -592,18 +592,6 @@ (define (user-processes-shepherd-service requirements)
                                          (@ (ice-9 rdelim) read-string))))
                          '()))
 
-                   (define (now)
-                     (car (gettimeofday)))
-
-                   (define (sleep* n)
-                     ;; Really sleep N seconds.
-                     ;; Work around <http://bugs.gnu.org/19581>.
-                     (define start (now))
-                     (let loop ((elapsed 0))
-                       (when (> n elapsed)
-                         (sleep (- n elapsed))
-                         (loop (- (now) start)))))
-
                    (define lset= (@ (srfi srfi-1) lset=))
 
                    (display "sending all processes the TERM signal\n")
@@ -612,7 +600,7 @@ (define (user-processes-shepherd-service requirements)
                        (begin
                          ;; Easy: terminate all of them.
                          (kill -1 SIGTERM)
-                         (sleep* #$grace-delay)
+                         (sleep #$grace-delay)
                          (kill -1 SIGKILL))
                        (begin
                          ;; Kill them all except OMITTED-PIDS.  XXX: We would
@@ -620,30 +608,17 @@ (define (user-processes-shepherd-service requirements)
                          ;; processes, like 'killall5' does, but that seems
                          ;; unreliable.
                          (kill-except omitted-pids SIGTERM)
-                         (sleep* #$grace-delay)
+                         (sleep #$grace-delay)
                          (kill-except omitted-pids SIGKILL)
                          (delete-file #$%do-not-kill-file)))
 
                    (let wait ()
-                     ;; Reap children, if any, so that we don't end up with
-                     ;; zombies and enter an infinite loop.
-                     (let reap-children ()
-                       (define result
-                         (false-if-exception
-                          (waitpid WAIT_ANY (if (null? omitted-pids)
-                                                0
-                                                WNOHANG))))
-
-                       (when (and (pair? result)
-                                  (not (zero? (car result))))
-                         (reap-children)))
-
                      (let ((pids (processes)))
                        (unless (lset= = pids (cons 1 omitted-pids))
                          (format #t "waiting for process termination\
  (processes left: ~s)~%"
                                  pids)
-                         (sleep* 2)
+                         (sleep 1)
                          (wait))))
 
                    (display "all processes have been terminated\n")
-- 
2.47.1