gh-87135: Raise PythonFinalizationError when joining a blocked daemon thread#130402
gh-87135: Raise PythonFinalizationError when joining a blocked daemon thread#130402encukou merged 9 commits into
Conversation
…daemon thread If `Py_IsFinalizing()` is true, non-daemon threads (other than the current one) are done, and daemon threads are prevented from running, so they cannot finalize themselves and become done. Joining them without timeout would block forever. Raise PythonFinalizationError instead of hanging. See pythongh-123940 for a use case: calling `join()` from `__del__`. This is ill-advised, but an exception should at least make it easier to diagnose.
|
This seems like a good idea to me.
|
Sorry, something went wrong.
I agree with Sam. |
Sorry, something went wrong.
True. Threads that are already done can be joined normally.
In a finalizer, wouldn't it be OK to wait a bit for graceful termination (using Raising an exception would mean you skip that teardown, unless you have a IOW, to me, the reasoning is not as clear-cut here as in the “hang the only Python thread that can run” case.
Well... You can even write an infinite loop without any join at all! :) I wrote the code to hang even with timeout; I'll update the PR if you still think that's the way to go. |
Sorry, something went wrong.
|
Updated to raise even with timeout. |
Sorry, something went wrong.
|
I'll merge on ~Friday if there are no objections. |
Sorry, something went wrong.
|
... And I went offline for a month after writing that. I'm back now; merging. |
Sorry, something went wrong.
If
Py_IsFinalizing()is true, non-daemon threads (other than the current one) are done, and daemon threads are prevented from acquiring GIL (or thread state), so they cannot finalize themselves and become done. Joining them without timeout would block forever.Raise PythonFinalizationError instead of hanging.
See gh-123940 for a real-world use case: calling
join()from__del__.Doing this is still ill-advised, but an exception should at least make the issue easier to diagnose.
📚 Documentation preview 📚: https://cpython-previews--130402.org.readthedocs.build/