From eae39b339ca3ba5e0271f353927cd02a433fe66b Mon Sep 17 00:00:00 2001 From: rdb Date: Tue, 15 Apr 2025 10:44:33 +0200 Subject: [PATCH] collide: Fix CollisionNode owner crash (grab GIL when destructing) --- panda/src/collide/collisionNode_ext.cxx | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/panda/src/collide/collisionNode_ext.cxx b/panda/src/collide/collisionNode_ext.cxx index 8508cf5206..62e7d5f874 100644 --- a/panda/src/collide/collisionNode_ext.cxx +++ b/panda/src/collide/collisionNode_ext.cxx @@ -53,7 +53,17 @@ void Extension:: set_owner(PyObject *owner) { if (owner != Py_None) { 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 { _this->clear_owner(); }