From a46105b03ddf9acebb6b70a2d98543c7f49f61ce Mon Sep 17 00:00:00 2001
From: Dani Pinyol <dani@avatarcognition.com>
Date: Mon, 1 Jul 2024 18:22:31 +0200
Subject: [PATCH] call jl_atexit_hook when atexit

---
 pysrc/juliacall/__init__.py | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/pysrc/juliacall/__init__.py b/pysrc/juliacall/__init__.py
index 66bc34e5..6694298e 100644
--- a/pysrc/juliacall/__init__.py
+++ b/pysrc/juliacall/__init__.py
@@ -3,6 +3,8 @@
 
 __version__ = '0.9.20'
 
+import atexit
+
 _newmodule = None
 
 def newmodule(name):
@@ -199,6 +201,13 @@ def args_from_config():
         None if sysimg is None else sysimg.encode('utf8'),
     )
 
+    @atexit.register
+    def at_jl_exit():
+        jl_atexit_hook = lib.jl_atexit_hook
+        jl_atexit_hook.argtypes = [c.c_int]
+        jl_atexit_hook.restype = None
+        jl_atexit_hook(0)
+
     # initialise PythonCall
     jl_eval = lib.jl_eval_string
     jl_eval.argtypes = [c.c_char_p]