Python: extra check on BPY_thread_save() to ensure proper GIL handling

Use `_PyThreadState_UncheckedGet()` to check that the current thread is
tracked by Python before checking whether it has the GIL. The latter
will abort when the former is false.
This commit is contained in:
2021-09-10 14:57:56 +02:00
parent ca39aff59d
commit 0467ff4053

View File

@@ -29,9 +29,12 @@
/* analogue of PyEval_SaveThread() */
BPy_ThreadStatePtr BPY_thread_save(void)
{
/* Don't use `PyThreadState_Get()`, to avoid a fatal error issued when a thread state is NULL
* (the thread state can be NULL when quitting Blender). */
if (PyGILState_Check()) {
/* Use `_PyThreadState_UncheckedGet()` instead of `PyThreadState_Get()`, to avoid a fatal error
* issued when a thread state is NULL (the thread state can be NULL when quitting Blender).
*
* `PyEval_SaveThread()` will release the GIL, so this thread has to have the GIL to begin with
* or badness will ensue. */
if (_PyThreadState_UncheckedGet() && PyGILState_Check()) {
return (BPy_ThreadStatePtr)PyEval_SaveThread();
}
return NULL;