Skip to content

Commit 901d95c

Browse files
authored
Merge pull request #414 from samschlegel/fix-411
Check if interpreter is finalizing in ~ThreadStateCreator
2 parents aa9966b + acdd8f1 commit 901d95c

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/greenlet/TThreadStateDestroy.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,23 @@ struct ThreadState_DestroyNoGIL
9696
// Python < 3.8 or >= 3.9
9797
static int AddPendingCall(int (*func)(void*), void* arg)
9898
{
99+
// If the interpreter is in the middle of finalizing, we can't add a
100+
// pending call. Trying to do so will end up in a SIGSEGV, as
101+
// Py_AddPendingCall will not be able to get the interpreter and will
102+
// try to dereference a NULL pointer. It's possible this can still
103+
// segfault if we happen to get context switched, and maybe we should
104+
// just always implement our own AddPendingCall, but I'd like to see if
105+
// this works first
106+
#if GREENLET_PY313
107+
if (Py_IsFinalizing()) {
108+
#else
109+
if (_Py_IsFinalizing()) {
110+
#endif
111+
fprintf(stderr,
112+
"greenlet: WARNING: Interpreter is finalizing. Ignoring "
113+
"call to Py_AddPendingCall; \n");
114+
return 0;
115+
}
99116
return Py_AddPendingCall(func, arg);
100117
}
101118
#endif

0 commit comments

Comments
 (0)