gh-87135: threading.Lock: Raise rather than hang on Python finalization (GH-135991)

After Python finalization gets to the point where no other thread
can attach thread state, attempting to acquire a Python lock must hang.
Raise PythonFinalizationError instead of hanging.
This commit is contained in:
Petr Viktorin 2025-07-01 10:57:42 +02:00 committed by GitHub
parent 845263adc6
commit fe119a0817
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 97 additions and 5 deletions

View file

@ -429,7 +429,9 @@ The following exceptions are the exceptions that are usually raised.
* Creating a new Python thread.
* :meth:`Joining <threading.Thread.join>` a running daemon thread.
* :func:`os.fork`.
* :func:`os.fork`,
* acquiring a lock such as :class:`threading.Lock`, when it is known that
the operation would otherwise deadlock.
See also the :func:`sys.is_finalizing` function.
@ -440,6 +442,11 @@ The following exceptions are the exceptions that are usually raised.
:meth:`threading.Thread.join` can now raise this exception.
.. versionchanged:: next
This exception may be raised when acquiring :meth:`threading.Lock`
or :meth:`threading.RLock`.
.. exception:: RecursionError
This exception is derived from :exc:`RuntimeError`. It is raised when the