From 6aa96ad3f7c83be4c402473f7e3802cc9e0b08e1 Mon Sep 17 00:00:00 2001 From: Matthew Flatt Date: Mon, 14 Oct 2019 18:38:19 -0600 Subject: [PATCH] csm thread, io: avoid termination problem Especially avoid problems with `custodian-shutdown-all` on the main custodian in the main thread. --- racket/src/io/port/fd-port.rkt | 8 +++++--- racket/src/thread/schedule.rkt | 2 ++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/racket/src/io/port/fd-port.rkt b/racket/src/io/port/fd-port.rkt index fddd5171fb..51aa595607 100644 --- a/racket/src/io/port/fd-port.rkt +++ b/racket/src/io/port/fd-port.rkt @@ -32,12 +32,14 @@ prop:fd-place-message-opener) ;; in atomic mode -(define (fd-close fd fd-refcount) +(define (fd-close fd fd-refcount + #:discard-errors? [discard-errors? #f]) (set-box! fd-refcount (sub1 (unbox fd-refcount))) (when (zero? (unbox fd-refcount)) (fd-semaphore-update! fd 'remove) (define v (rktio_close rktio fd)) - (when (rktio-error? v) + (when (and (rktio-error? v) + (not discard-errors?)) (end-atomic) (raise-rktio-error #f v "error closing stream port")))) @@ -507,7 +509,7 @@ (if (input-port? port) (send fd-input-port port on-close) (send fd-output-port port on-close)) - (fd-close fd fd-refcount) + (fd-close fd fd-refcount #:discard-errors? #t) (set-closed-state! port)) #f #f)) diff --git a/racket/src/thread/schedule.rkt b/racket/src/thread/schedule.rkt index 70980348a9..39744ffa10 100644 --- a/racket/src/thread/schedule.rkt +++ b/racket/src/thread/schedule.rkt @@ -149,6 +149,8 @@ ;; Thread continues (cond [(zero? (current-atomic)) + (when (thread-dead? root-thread) + (force-exit 0)) (define new-leftover-ticks (- leftover-ticks (- TICKS remaining-ticks))) (accum-cpu-time! t (new-leftover-ticks . <= . 0)) (set-thread-future! t (current-future))