~ chicken-core (chicken-5) faeeb21091bc43f4e02670828080d6ff117915bf


commit faeeb21091bc43f4e02670828080d6ff117915bf
Author:     Peter Bex <peter@more-magic.net>
AuthorDate: Thu Feb 6 15:19:16 2025 +0100
Commit:     Peter Bex <peter@more-magic.net>
CommitDate: Thu Feb 6 15:19:16 2025 +0100

    Clean up process-fork return value handling and fix ##sys#call-with-cthulhu call
    
    Similar to 8de3923, more explicitly handle the three "pid" return
    value cases: error, child or parent.
    
    Also, wrap the call to the thunk in a lambda which is passed to
    the ##sys#call-with-cthulhu procedure instead of calling it and
    passing the result to ##sys#call-with-cthulhu (which somehow did not
    cause an error... probably because the exit call happened inside that
    thunk already) so that the continuation for the child is reset correctly.

diff --git a/posixunix.scm b/posixunix.scm
index a3fe86b9..8b8757be 100644
--- a/posixunix.scm
+++ b/posixunix.scm
@@ -1110,21 +1110,23 @@ static int set_file_mtime(C_word filename, C_word atime, C_word mtime)
                                  (if killothers
                                      (##sys#kill-other-threads thunk)
                                      (thunk)))))
-        (when (fx= -1 pid)
-          (posix-error #:process-error 'process-fork "cannot create child process"))
-        (cond ((zero? pid)
-               ;; child
+        (cond ((fx= -1 pid)             ; error
+               (posix-error #:process-error 'process-fork "cannot create child process"))
+              ((fx= 0 pid)              ; child process
+               (set! children '())
                (cond (thunk
-                      (##sys#call-with-cthulhu
-                       (maybe-kill-others (lambda ()
-                                            (set! children '())
-                                            (thunk)
-                                            (exit 0)))))
+                      (maybe-kill-others (lambda ()
+                                           (##sys#call-with-cthulhu
+                                            (lambda ()
+                                              (thunk)
+                                              ;; Make sure to run clean up tasks.
+                                              ;; NOTE: ##sys#call-with-cthulhu will invoke
+                                              ;; a more low-level runtime C_exit_runtime(0)
+                                              (exit 0))))))
                      (else
-        	      (maybe-kill-others (lambda ()
-                                           (set! children '())
-                                           #f)))))
-              (else (register-pid pid)))))))
+        	      (maybe-kill-others (lambda () #f)))))
+              (else                     ; parent process
+               (register-pid pid)))))))
 
 (set! chicken.process#process-execute
   (lambda (filename #!optional (arglist '()) envlist _)
Trap