python-greenlet / greenlet

Lightweight in-process concurrent programming
Other
1.63k stars 247 forks source link

Check if interpreter is finalizing in ~ThreadStateCreator #414

Open samschlegel opened 2 months ago

samschlegel commented 2 months ago

Calling PyEval_AddPendingCall while the interpreter is finalizing can lead to a segfault due to the implementation in CPython of PyEval_AddPendingCall. This is a somewhat hacky fix to just check if it's finalizing before we try and schedule the cleanup.

I say somewhat hacky, cause it's still possible to race between checking and actually calling PyEval_AddPendingCall and end up segfaulting, but in practice so far this fix has completely solved our segfaulting issues. Arguably the most ideal fix here would be to fix this call in CPython to not segfault when the interpreter is shutting down, and just terminate the thread like it now does with the locking API

Fixes #411