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

**URL:** <https://racket.discourse.group/t/will-executor-not-being-executed-on-program-exit-unless-collect-garbage-is-called/3316>\
**Category:** General\
**Created:** [November 14, 2024, 3:09pm UTC](https://racket.discourse.group/t/will-executor-not-being-executed-on-program-exit-unless-collect-garbage-is-called/3316 "2024-11-14T15:09:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Laurent.O](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/laurent.o/32/18_2.png) [@Laurent.O](https://racket.discourse.group/u/Laurent.O)\
**Post date:** [November 14, 2024, 3:09pm UTC](https://racket.discourse.group/t/will-executor-not-being-executed-on-program-exit-unless-collect-garbage-is-called/3316/1 "2024-11-14T15:09:25Z")

</div>

Adapting from the docs of [wills and executors](https://docs.racket-lang.org/reference/willexecutor.html), this works as I expected:

```scheme
#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:

```scheme
#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).

---

<div class="post-metadata">

**Author:** ![samth](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/samth/32/3_2.png) [@samth](https://racket.discourse.group/u/samth)\
**Post date:** [November 14, 2024, 3:37pm UTC](https://racket.discourse.group/t/will-executor-not-being-executed-on-program-exit-unless-collect-garbage-is-called/3316/2 "2024-11-14T15:37:52Z")

</div>

If you want to ensure that something is executed when the process ends, then the best thing to do is probably to use [_plumbers_](https://docs.racket-lang.org/reference/plumbers.html#%28tech._plumber%29). However, note that nothing can be _guaranteed_ to run at process exit.

---

<div class="post-metadata">

**Author:** ![Laurent.O](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/laurent.o/32/18_2.png) [@Laurent.O](https://racket.discourse.group/u/Laurent.O)\
**Post date:** [November 14, 2024, 3:45pm UTC](https://racket.discourse.group/t/will-executor-not-being-executed-on-program-exit-unless-collect-garbage-is-called/3316/3 "2024-11-14T15:45:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gus-massa](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/gus-massa/32/507_2.png) [@gus-massa](https://racket.discourse.group/u/gus-massa)\
**Post date:** [November 15, 2024, 6:31pm UTC](https://racket.discourse.group/t/will-executor-not-being-executed-on-program-exit-unless-collect-garbage-is-called/3316/4 "2024-11-15T18:31:06Z")

</div>

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`?
