Skip to content

Commit 056ea94

Browse files
committed
Fix PythonThread crash (LP bug 1245818)
1 parent 62d0d82 commit 056ea94

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

panda/src/pipeline/pythonThread.cxx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ PythonThread(PyObject *function, PyObject *args,
5050
nassert_raise("Invalid args passed to PythonThread constructor");
5151
}
5252
}
53+
54+
#ifndef SIMPLE_THREADS
55+
// Ensure that the Python threading system is initialized and ready to go.
56+
#ifdef WITH_THREAD // This symbol defined within Python.h
57+
PyEval_InitThreads();
58+
#endif
59+
#endif
5360
}
5461

5562
////////////////////////////////////////////////////////////////////
@@ -59,9 +66,20 @@ PythonThread(PyObject *function, PyObject *args,
5966
////////////////////////////////////////////////////////////////////
6067
PythonThread::
6168
~PythonThread() {
69+
// Unfortunately, we need to grab the GIL to release these things,
70+
// since the destructor could be called from any thread.
71+
#if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
72+
PyGILState_STATE gstate;
73+
gstate = PyGILState_Ensure();
74+
#endif
75+
6276
Py_DECREF(_function);
6377
Py_XDECREF(_args);
6478
Py_XDECREF(_result);
79+
80+
#if defined(HAVE_THREADS) && !defined(SIMPLE_THREADS)
81+
PyGILState_Release(gstate);
82+
#endif
6583
}
6684

6785
////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy