diff --git a/core/src/statusbar.sld b/core/src/statusbar.sld index 26b706c..de5f284 100644 --- a/core/src/statusbar.sld +++ b/core/src/statusbar.sld @@ -53,16 +53,19 @@ (define-values (rows cols) (terminal-size err-port)) (when (> cols 0) (set! terminal-width cols)) + (define shutdown #f) + (define (terminal-width-thread-thunk handler) ; Lock the output mutex whilst setting the terminal size. (define-values (rows cols) (terminal-size err-port)) (mutex-lock! out-mutex) (when (> cols 0) (set! terminal-width cols)) + (define is-shutdown shutdown) (mutex-unlock! out-mutex) ; Wait until the next SIGWINCH, then loop (handler #t) - (terminal-width-thread-thunk handler)) + (unless is-shutdown (terminal-width-thread-thunk handler))) (define terminal-width-thread (make-thread (lambda () (terminal-width-thread-thunk (make-signal-handler signal/winch))))) @@ -83,9 +86,11 @@ (rerender-status-bar) (mutex-lock! out-mutex) (draw-status-bar) + (define is-shutdown shutdown) (mutex-unlock! out-mutex) - (thread-sleep! 0.1) - (redraw-thread-thunk)) + (unless is-shutdown + (thread-sleep! 0.1) + (redraw-thread-thunk))) (define redraw-thread (make-thread redraw-thread-thunk "redraw thread")) (define last-builds-activity-id #f) @@ -123,12 +128,19 @@ (define (close-this-port) (mutex-lock! out-mutex) - (thread-terminate! redraw-thread) - (thread-terminate! terminal-width-thread) + (when (eq? new-err-port (current-error-port)) + (current-error-port err-port)) + (when (eq? new-out-port (current-output-port)) + (current-output-port out-port)) + (define was-shutdown shutdown) + (set! shutdown #t) (mutex-unlock! out-mutex) - (fprintf err-port "\r\x1B[2K\n") - (close-output-port err-port) - (close-output-port out-port)) + (unless was-shutdown + (thread-join! redraw-thread) + (mutex-lock! out-mutex) + (thread-terminate! terminal-width-thread) + (mutex-unlock! out-mutex) + (fprintf err-port "\r\x1B[2K\n"))) (define new-err-port (buffered-port out-mutex write-err-line draw-status-bar close-this-port)) (define new-out-port (buffered-port out-mutex write-out-line draw-status-bar close-this-port))