From 056ea94765aeccd659e3905fa46ccc4556eaa9e8 Mon Sep 17 00:00:00 2001 From: rdb Date: Thu, 22 Dec 2016 21:28:19 +0100 Subject: [PATCH] Fix PythonThread crash (LP bug 1245818) --- panda/src/pipeline/pythonThread.cxx | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/panda/src/pipeline/pythonThread.cxx b/panda/src/pipeline/pythonThread.cxx index c1ef41e344..493b576229 100644 --- a/panda/src/pipeline/pythonThread.cxx +++ b/panda/src/pipeline/pythonThread.cxx @@ -50,6 +50,13 @@ PythonThread(PyObject *function, PyObject *args, nassert_raise("Invalid args passed to PythonThread constructor"); } } + +#ifndef SIMPLE_THREADS + // Ensure that the Python threading system is initialized and ready to go. +#ifdef WITH_THREAD // This symbol defined within Python.h + PyEval_InitThreads(); +#endif +#endif } //////////////////////////////////////////////////////////////////// @@ -59,9 +66,20 @@ PythonThread(PyObject *function, PyObject *args, //////////////////////////////////////////////////////////////////// PythonThread:: ~PythonThread() { + // Unfortunately, we need to grab the GIL to release these things, + // since the destructor could be called from any thread. +#if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS) + PyGILState_STATE gstate; + gstate = PyGILState_Ensure(); +#endif + Py_DECREF(_function); Py_XDECREF(_args); Py_XDECREF(_result); + +#if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS) + PyGILState_Release(gstate); +#endif } ////////////////////////////////////////////////////////////////////