diff --git a/audio-placed-player.rkt b/audio-placed-player.rkt index b0ebd5a..1f3061a 100644 --- a/audio-placed-player.rkt +++ b/audio-placed-player.rkt @@ -97,6 +97,11 @@ (define (get) (uni-channel-get ch-in)) + + (define (close-and-wait ch) + (when ch + (uni-channel-close ch) + (uni-channel-wait ch))) (define (audio-read-worker ao-dec file-id) (set! feeding-audio #t) @@ -481,11 +486,17 @@ ))]) (cond - ((eq? cmd 'quit) (do-rpc - (stop-and-cleanup) - (set! player-state 'quit) - (state "quit" evt 'force) - '(quit))) + ((eq? cmd 'quit) + ;; Quit is the one RPC where the acknowledgement must be written + ;; before the worker starts tearing down its audio state and stdio + ;; transport. For port-based uni-channels, put is asynchronous: + ;; close-and-wait makes sure the queued '(quit) has actually been + ;; written and flushed before the process exits. + (put '(quit)) + (close-and-wait ch-out) + (stop-and-cleanup) + (set! player-state 'quit) + (close-and-wait ch-evt)) ((eq? cmd 'init) (do-rpc (set! ch-out (->uni-channel (cadr data))) (set! ch-evt (->uni-channel (caddr data)))