Description
🐛 Describe the bug
Quote:
Attempted to use _Py_IsFinalizing() to guard against finalize, but it always returns false (even though the coredump indicates the interpreter thread is within Py_FinalizeEx() and should have set the finalize attribute on the runtime...but the whole thing is racy): D52866385
Also tried just catching the exception with try {...} catch (...) {} but the program still aborts with FATAL: exception not rethrown.
The exact line that fails is we create a new PyThreadState and then try to swap it and lock the GIL w/ PyEval_AcquireThread in the pybind11 code.
The docs for PyEval_AcquireThread suggest that the calling thread will be terminate if the interpreter is finalizing, which matches the behavior we get. https://docs.python.org/3/c-api/init.html...
The problem is I don't know how to avoid that. I tried _Py_IsFinalizing(), it returns false everywhere. I attempted to use Py_AtExit to schedule a callback where I could implement my own synchronization mechanism, but it only gets scheduled half the time.
Versions
main
cc @albanD