Finalize for modules?

I’m working on a package that is using MPI in the background, and are calling MPI.Init() in the modules init function. But I have no idea how I can detect that the module is unloaded (or the Julia session is finished) to call MPI.Finalize() . Exist there any opportunity to get a notification of the teardown process?

You can add an atexit hook which is called when Julia shuts down.

Note that, for this specific example, MPI.Init() adds this hook by default for you, see MPI.jl/src/environment.jl.