Browse Source

putil: grab GIL while decreffing function

rdb 4 months ago
parent
commit
de4865b324
1 changed files with 11 additions and 0 deletions
  1. 11 0
      panda/src/putil/pythonCallbackObject.cxx

+ 11 - 0
panda/src/putil/pythonCallbackObject.cxx

@@ -60,7 +60,18 @@ PythonCallbackObject(PyObject *function) {
  */
  */
 PythonCallbackObject::
 PythonCallbackObject::
 ~PythonCallbackObject() {
 ~PythonCallbackObject() {
+  // This may be called from the cull or draw thread, so we have to be sure we
+  // own the GIL.
+#if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
+  PyGILState_STATE gstate;
+  gstate = PyGILState_Ensure();
+#endif
+
   Py_DECREF(_function);
   Py_DECREF(_function);
+
+#if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
+  PyGILState_Release(gstate);
+#endif
 }
 }
 
 
 /**
 /**