diff options
author | Christopher Baines <mail@cbaines.net> | 2023-08-22 16:19:58 +0100 |
---|---|---|
committer | Christopher Baines <mail@cbaines.net> | 2023-08-22 16:19:58 +0100 |
commit | f9408ba75803baccd0d44de06b5c2b609fd42315 (patch) | |
tree | 98992850bb87ffdc4896e9bbff41bf71e2918888 | |
parent | 71c301c777a6329ab0d51df9f6c6e077a733ac7a (diff) | |
download | build-coordinator-f9408ba75803baccd0d44de06b5c2b609fd42315.tar build-coordinator-f9408ba75803baccd0d44de06b5c2b609fd42315.tar.gz |
Try and ensure that the worker threads don't disappear
Add some top level exception handling to guard against errors anywhere.
-rw-r--r-- | guix-build-coordinator/utils/fibers.scm | 133 |
1 files changed, 72 insertions, 61 deletions
diff --git a/guix-build-coordinator/utils/fibers.scm b/guix-build-coordinator/utils/fibers.scm index 947697d..0ce6da0 100644 --- a/guix-build-coordinator/utils/fibers.scm +++ b/guix-build-coordinator/utils/fibers.scm @@ -85,6 +85,68 @@ arguments of the worker thread procedure." (sleep 5) (destructor/safe args))))) + (define (process channel args) + (let loop ((current-lifetime lifetime)) + (let ((exception? + (match (get-message channel) + (((? channel? reply) sent-time (? procedure? proc)) + (let ((time-delay + (- (get-internal-real-time) + sent-time))) + (delay-logger (/ time-delay + internal-time-units-per-second)) + + (let* ((start-time (get-internal-real-time)) + (response + (with-exception-handler + (lambda (exn) + (list 'worker-thread-error + (/ (- (get-internal-real-time) + start-time) + internal-time-units-per-second) + exn)) + (lambda () + (with-throw-handler #t + (lambda () + (call-with-values + (lambda () + (apply proc args)) + (lambda vals + (cons (/ (- (get-internal-real-time) + start-time) + internal-time-units-per-second) + vals)))) + (lambda args + (when (match args + (('%exception exn) + (log-exception? exn)) + (_ #t)) + (simple-format + (current-error-port) + "worker-thread: exception: ~A\n" args) + (backtrace))))) + #:unwind? #t))) + (put-message reply + response) + + (match response + (('worker-thread-error duration _) + (when duration-logger + (duration-logger duration proc)) + #t) + ((duration . _) + (when duration-logger + (duration-logger duration proc)) + #f)))))))) + (unless (and expire-on-exception? + exception?) + (if (number? current-lifetime) + (unless (< current-lifetime 0) + (loop (if current-lifetime + (- current-lifetime 1) + #f))) + (loop #f)))))) + (let ((channel (make-channel))) (for-each (lambda (thread-index) @@ -99,67 +161,16 @@ arguments of the worker thread procedure." (const #t)) (let init ((args (initializer/safe))) - (parameterize ((%worker-thread-args args)) - (let loop ((current-lifetime lifetime)) - (let ((exception? - (match (get-message channel) - (((? channel? reply) sent-time (? procedure? proc)) - (let ((time-delay - (- (get-internal-real-time) - sent-time))) - (delay-logger (/ time-delay - internal-time-units-per-second)) - - (let* ((start-time (get-internal-real-time)) - (response - (with-exception-handler - (lambda (exn) - (list 'worker-thread-error - (/ (- (get-internal-real-time) - start-time) - internal-time-units-per-second) - exn)) - (lambda () - (with-throw-handler #t - (lambda () - (call-with-values - (lambda () - (apply proc args)) - (lambda vals - (cons (/ (- (get-internal-real-time) - start-time) - internal-time-units-per-second) - vals)))) - (lambda args - (when (match args - (('%exception exn) - (log-exception? exn)) - (_ #t)) - (simple-format - (current-error-port) - "worker-thread: exception: ~A\n" args) - (backtrace))))) - #:unwind? #t))) - (put-message reply - response) - - (match response - (('worker-thread-error duration _) - (when duration-logger - (duration-logger duration proc)) - #t) - ((duration . _) - (when duration-logger - (duration-logger duration proc)) - #f)))))))) - (unless (and expire-on-exception? - exception?) - (if (number? current-lifetime) - (unless (< current-lifetime 0) - (loop (if current-lifetime - (- current-lifetime 1) - #f))) - (loop #f)))))) + (with-exception-handler + (lambda (exn) + (simple-format + (current-error-port) + "worker-thread-channel: exception: ~A\n" exn)) + (lambda () + (parameterize ((%worker-thread-args args)) + (process channel args))) + #:unwind? #t) + (when destructor (destructor/safe args)) |