summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Bex <peter@more-magic.net>2017-11-08 20:28:13 +0100
committerPeter Bex <peter@more-magic.net>2017-11-08 20:29:38 +0100
commit736da4689151366a707669bff7569c3123ed608b (patch)
treeac51646af9f31fc53759d6fc6cc7c4587f375582
parent464cb22f4b73c6e90dcde03ad78085b7ed85f925 (diff)
downloadscsh-process-736da4689151366a707669bff7569c3123ed608b.tar.gz
Fix process-wait on #f or a plain pid
Thanks to Jörg F. Wittenberger for the patch and test
-rw-r--r--scsh-process.scm30
1 files changed, 20 insertions, 10 deletions
diff --git a/scsh-process.scm b/scsh-process.scm
index e2be39c..4153c00 100644
--- a/scsh-process.scm
+++ b/scsh-process.scm
@@ -115,7 +115,12 @@
(error 'process-wait
"Not a scsh-type process object or pid"
pid-or-process))
- (let ((p (if (and pid-or-process (number? pid-or-process))
+ ;; We need to make a copy because when waiting for #f, we
+ ;; can't predict which pid we'll receive, and the SIGCHLD
+ ;; handler will drop the pid from the pending list.
+ (let ((pending-before
+ (hash-table-copy *scsh-pending-processes*))
+ (p (if (and pid-or-process (number? pid-or-process))
(hash-table-ref/default *scsh-pending-processes*
pid-or-process #f)
pid-or-process)))
@@ -138,16 +143,21 @@
(scsh-process-pid p))
(abort exn))
(receive (pid ok? status)
- (posix-process-wait (and p (scsh-process-pid p)) nohang)
+ (posix-process-wait (if p
+ (scsh-process-pid p)
+ pid-or-process) nohang)
(cond
((zero? pid) (values #f #f #f))
- (else (when p
- (scsh-process-exit-status-set! p status)
- (scsh-process-ok?-set! p ok?)
- (condition-variable-broadcast!
- (scsh-process-child-condition p)))
- (remove-scsh-pending-process! pid)
- (values status ok? pid))))))))))
+ (else
+ (and-let* ((p (or p (hash-table-ref/default
+ pending-before pid #f))))
+ (scsh-process-exit-status-set! p status)
+ (scsh-process-ok?-set! p ok?)
+ (condition-variable-broadcast!
+ (scsh-process-child-condition p)))
+
+ (remove-scsh-pending-process! pid)
+ (values status ok? pid))))))))))
(set-signal-handler!
signal/chld
@@ -163,7 +173,7 @@
(for-each (lambda (pid)
(handle-exceptions exn
;; User might have waited manually
- (begin (remove-scsh-pending-process! pid) (void))
+ (begin (remove-scsh-pending-process! pid) (void))
(receive (pid ok? status)
(posix-process-wait pid #t)
(unless (zero? pid)