Browse Source

collide: Fix CollisionNode owner crash (grab GIL when destructing)

rdb 8 months ago
parent
commit
eae39b339c
1 changed files with 11 additions and 1 deletions
  1. 11 1
      panda/src/collide/collisionNode_ext.cxx

+ 11 - 1
panda/src/collide/collisionNode_ext.cxx

@@ -53,7 +53,17 @@ void Extension<CollisionNode>::
 set_owner(PyObject *owner) {
 set_owner(PyObject *owner) {
   if (owner != Py_None) {
   if (owner != Py_None) {
     PyObject *ref = PyWeakref_NewRef(owner, nullptr);
     PyObject *ref = PyWeakref_NewRef(owner, nullptr);
-    _this->set_owner(ref, [](void *obj) { Py_DECREF((PyObject *)obj); });
+    _this->set_owner(ref, [](void *obj) {
+#if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
+      // Use PyGILState to protect this asynchronous call.
+      PyGILState_STATE gstate;
+      gstate = PyGILState_Ensure();
+#endif
+      Py_DECREF((PyObject *)obj);
+#if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
+      PyGILState_Release(gstate);
+#endif
+    });
   } else {
   } else {
     _this->clear_owner();
     _this->clear_owner();
   }
   }