Skip to content

Commit

Permalink
DISPATCH-2144 Fatal Python error: _PyMem_DebugMalloc: Python memory a…
Browse files Browse the repository at this point in the history
…llocator called without holding the GIL (#1495)
  • Loading branch information
jiridanek committed Jan 27, 2022
1 parent 8147b62 commit dd17623
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/python_embedded.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -1442,7 +1442,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);
}

Expand Down

0 comments on commit dd17623

Please sign in to comment.