# Finalize for modules?

**URL:** <https://discourse.julialang.org/t/finalize-for-modules/86934>\
**Category:** General Usage\
**Tags:** module, mpi\
**Created:** [September 8, 2022, 9:57am UTC](https://discourse.julialang.org/t/finalize-for-modules/86934 "2022-09-08T09:57:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sfuerst](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@sfuerst](https://discourse.julialang.org/u/sfuerst)\
**Post date:** [September 8, 2022, 9:57am UTC](https://discourse.julialang.org/t/finalize-for-modules/86934/1 "2022-09-08T09:57:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [September 8, 2022, 11:33am UTC](https://discourse.julialang.org/t/finalize-for-modules/86934/2 "2022-09-08T11:33:22Z")

</div>

You can add an [`atexit`](https://docs.julialang.org/en/v1/base/base/#Base.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`](https://github.com/JuliaParallel/MPI.jl/blob/f8e4749c1ce8141458d0a88664044ec64da357ba/src/environment.jl#L62-L63).
