diff --git a/panda/src/event/pythonTask.cxx b/panda/src/event/pythonTask.cxx index 7eba5c482a..a8f888289f 100644 --- a/panda/src/event/pythonTask.cxx +++ b/panda/src/event/pythonTask.cxx @@ -37,6 +37,7 @@ PythonTask(PyObject *function, const string &name) : _args = NULL; _upon_death = NULL; _owner = NULL; + _registered_to_owner = false; _generator = NULL; set_function(function); @@ -528,13 +529,14 @@ upon_death(AsyncTaskManager *manager, bool clean_exit) { //////////////////////////////////////////////////////////////////// void PythonTask:: register_to_owner() { - if (_owner != Py_None) { + if (_owner != Py_None && !_registered_to_owner) { #if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS) // Use PyGILState to protect this asynchronous call. PyGILState_STATE gstate; gstate = PyGILState_Ensure(); #endif + _registered_to_owner = true; call_owner_method("_addTask"); #if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS) @@ -550,13 +552,15 @@ register_to_owner() { //////////////////////////////////////////////////////////////////// void PythonTask:: unregister_from_owner() { - if (_owner != Py_None) { + // make sure every call to _clearTask corresponds to a call to _addTask + if (_owner != Py_None && _registered_to_owner) { #if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS) // Use PyGILState to protect this asynchronous call. PyGILState_STATE gstate; gstate = PyGILState_Ensure(); #endif + _registered_to_owner = false; call_owner_method("_clearTask"); #if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS) diff --git a/panda/src/event/pythonTask.h b/panda/src/event/pythonTask.h index 6f087e7d59..13874bc317 100644 --- a/panda/src/event/pythonTask.h +++ b/panda/src/event/pythonTask.h @@ -66,6 +66,7 @@ private: bool _append_task; PyObject *_upon_death; PyObject *_owner; + bool _registered_to_owner; PyObject *_dict; PyObject *_generator;