Will executor not being executed on program exit unless `(collect-garbage`) is called

Adapting from the docs of wills and executors, this works as I expected:

#lang racket
(define an-executor (make-will-executor))
(void (thread (λ () (sync an-executor) (will-execute an-executor))))
(define (executor-proc v) (printf "a-box is now garbage\n"))
(define a-box-to-track (box #f))
(will-register an-executor a-box-to-track executor-proc)
(collect-garbage)
(set! a-box-to-track #f)
(collect-garbage)

but apparently I can't rely on program termination to execute the will, that is, if (collect-garbage) isn't called explicitly, the will is not executed:

#lang racket
(define an-executor (make-will-executor))
(void (thread (λ () (sync an-executor) (will-execute an-executor))))
(define (executor-proc v) (printf "a-box is now garbage\n"))
(define a-box-to-track (box #f))
(will-register an-executor a-box-to-track executor-proc)

(I would have expected all programs to finish with some form of (collect-garbage).)

What's the proper way to ensure that the will is executed?
Specifically, the objective is to delete a temporary file upon program exit (or upon unreachability of a value).

If you want to ensure that something is executed when the process ends, then the best thing to do is probably to use plumbers. However, note that nothing can be guaranteed to run at process exit.

1 Like

Yes, that's exactly what I need, thanks!

So, to ensure the cleanup is run as soon as posible and ensure it's run, I should use a will-executor and a (weak) plumber?