gh-87135: Hang non-main threads that attempt to acquire the GIL during finalization by jbms · Pull Request #105805 · python/cpython (original) (raw)
I would prefer to keep the current behavior by default, but give the ability to hang for people impacted by pthread_exit() issues.
My problem with that and reason why we need this PR is that the answer to "who is going to use that ability" is unclear. End users of Python applications don't have a way to debug random thread crashes and know that they need it. Extension module authors do not know that they need it. A few big application owners will simply blindly turn it on for everything because they happen to have people aware of the consequences of not going it. But there is no good point to turn such a conditional of "hang threads rather than allow them to randomly crash or refuse to run C++ finalizers despite releasing the associated memory meaning other threads can crash because multiple other things are using the previously allocated and then freed memory". Those concurrency scribbling messes are far worse things to attempt to debug than a simple hung thread, hung within a clear C symbol saying "hi there, i'm going to hang this thread".
So I'm all for undoing our mistaken thread exiting API and use of it in favor of this least bad option of hanging threads that would otherwise have failed to cleanup and left ambiguious process state behind as we have had in the past.
I do still think we want APIs to allow error handling upon re-entering an interpreter or acquiring the GIL and actually failing instead of this... but we don't have those. and most of the world's Python C API using code won't be using such new APIs for a looong time while the random crashing problem would persist. So this remains the least bad option that makes a viable improvement.