From ce8523394ea49b91228e6f05a738d72d14b2e5c1 Mon Sep 17 00:00:00 2001
From: Cyrille Bagard <nocbos@gmail.com>
Date: Sat, 6 Oct 2018 13:13:18 +0200
Subject: Dealt with the Python global lock at exit.

---
 plugins/pychrysalide/plugin.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/plugins/pychrysalide/plugin.c b/plugins/pychrysalide/plugin.c
index 321fda9..c42235a 100644
--- a/plugins/pychrysalide/plugin.c
+++ b/plugins/pychrysalide/plugin.c
@@ -164,14 +164,30 @@ static void g_python_plugin_init(GPythonPlugin *plugin)
 
 static void g_python_plugin_dispose(GPythonPlugin *plugin)
 {
-    PyGILState_STATE gstate;                /* Sauvegarde d'environnement  */
-
-    gstate = PyGILState_Ensure();
+    PyThreadState *tstate;                  /* Contexte d'environnement    */
+
+    /**
+     * Cf. https://docs.python.org/3/c-api/init.html#thread-state-and-the-global-interpreter-lock
+     *
+     * Cependant, comme on se trouve à priori dans le thread principal de l'interpréteur,
+     * PyGILState_Ensure() ne pose aucun verrou. Ce qui aboutit à la situation suivante :
+     *
+     *    Fatal Python error: drop_gil: GIL is not locked
+     *
+     * On peut forcer les choses avec PyEval_AcquireLock(), mais cette fonction est marquée
+     * comme dépréciée depuis Python 3.2.
+     *
+     * Donc on choisit les alternatives officielles.
+     */
+
+    tstate = PyThreadState_Get();
+
+    PyEval_RestoreThread(tstate);
 
     Py_XDECREF(plugin->instance);
     plugin->instance = NULL;
 
-    PyGILState_Release(gstate);
+    PyEval_SaveThread();
 
     G_OBJECT_CLASS(g_python_plugin_parent_class)->dispose(G_OBJECT(plugin));
 
-- 
cgit v0.11.2-87-g4458