[racket] Callback on shutdown

From: Tony Garnock-Jones (tonyg at ccs.neu.edu)
Date: Wed May 7 14:36:37 EDT 2014

On 05/07/2014 10:19 AM, Matthew Flatt wrote:
> I've added `custodian-tidy-all` and related functions to [un]register a
> "tidy callback". The default exit handler calls `custodian-tidy-all` on
> the root custodian before exiting, which triggers all registered tidy
> callbacks.

Thank you Matthew, that's exactly what I was looking for!

The following program now produces its expected output. I should be able
to use this to shut down background threads cleanly before Racket exits.

Regards,
  Tony

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#lang racket
;; Expected output:
;;   wow
;;   much work
;;   amaze
;;   I WAS CALLED ON EXIT! YAY

(custodian-add-tidy! (current-custodian)
		     (lambda (cb)
		       (printf "I WAS CALLED ON EXIT! YAY\n")
		       (flush-output)))

(printf "wow\n")
(printf "much work\n")
(printf "amaze\n")

Posted on the users mailing list.