This is an automated email from the ASF dual-hosted git repository.

jdanek pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/qpid-dispatch.git


The following commit(s) were added to refs/heads/main by this push:
     new b131cdc  DISPATCH-2144 Fatal Python error: _PyMem_DebugMalloc: Python 
memory allocator called without holding the GIL (#1495)
b131cdc is described below

commit b131cdc35bd1e0365bb025c4544b89a8d2412a39
Author: Jiri DanÄ›k <[email protected]>
AuthorDate: Sat Jan 22 19:32:51 2022 +0100

    DISPATCH-2144 Fatal Python error: _PyMem_DebugMalloc: Python memory 
allocator called without holding the GIL (#1495)
---
 src/python_embedded.c | 14 +++++++++++---
 src/server.c          |  2 ++
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/src/python_embedded.c b/src/python_embedded.c
index 1b58c80..8468406 100644
--- a/src/python_embedded.c
+++ b/src/python_embedded.c
@@ -57,15 +57,22 @@ void qd_python_initialize(qd_dispatch_t *qd, const char 
*python_pkgdir)
     if (python_pkgdir)
         dispatch_python_pkgdir = PyUnicode_FromString(python_pkgdir);
 
-    qd_python_lock_state_t ls = qd_python_lock();
     Py_Initialize();
+#if PY_VERSION_HEX < 0x03070000
+    PyEval_InitThreads(); // necessary for Python 3.6 and older versions
+#endif
     qd_python_setup();
-    qd_python_unlock(ls);
+    PyEval_SaveThread(); // drop the Python GIL; we will reacquire it in other 
threads as needed
+    qd_python_lock_state_t lk = qd_python_lock();
+    qd_python_setup();
+    qd_python_unlock(lk);
 }
 
 
 void qd_python_finalize(void)
 {
+    (void) PyGILState_Ensure(); // not qd_python_lock(), because that function 
has to be paired with an _unlock
+
     sys_mutex_free(ilock);
     Py_DECREF(dispatch_module);
     dispatch_module = 0;
@@ -900,11 +907,12 @@ qd_python_lock_state_t qd_python_lock(void)
 {
     sys_mutex_lock(ilock);
     lock_held = true;
-    return 0;
+    return PyGILState_Ensure();
 }
 
 void qd_python_unlock(qd_python_lock_state_t lock_state)
 {
+    PyGILState_Release(lock_state);
     lock_held = false;
     sys_mutex_unlock(ilock);
 }
diff --git a/src/server.c b/src/server.c
index 51755e1..7729e29 100644
--- a/src/server.c
+++ b/src/server.c
@@ -1450,7 +1450,9 @@ void qd_server_free(qd_server_t *qd_server)
     sys_mutex_free(qd_server->lock);
     sys_mutex_free(qd_server->conn_activation_lock);
     sys_cond_free(qd_server->cond);
+    qd_python_lock_state_t ls = qd_python_lock();
     Py_XDECREF((PyObject *)qd_server->py_displayname_obj);
+    qd_python_unlock(ls);
     free(qd_server);
 }
 

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to