gh-114570: Add PythonFinalizationError exception (#115352)

Add PythonFinalizationError exception. This exception derived from
RuntimeError is raised when an operation is blocked during the Python
finalization.

The following functions now raise PythonFinalizationError, instead of
RuntimeError:

* _thread.start_new_thread()
* subprocess.Popen
* os.fork()
* os.fork1()
* os.forkpty()

Morever, _winapi.Overlapped finalizer now logs an unraisable
PythonFinalizationError, instead of an unraisable RuntimeError.
This commit is contained in:
Victor Stinner 2024-02-14 23:35:06 +01:00 committed by GitHub
parent 326119d373
commit 3e7b7df5cb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 55 additions and 6 deletions

View file

@ -416,6 +416,24 @@ The following exceptions are the exceptions that are usually raised.
handling in C, most floating point operations are not checked.
.. exception:: PythonFinalizationError
This exception is derived from :exc:`RuntimeError`. It is raised when
an operation is blocked during interpreter shutdown also known as
:term:`Python finalization <interpreter shutdown>`.
Examples of operations which can be blocked with a
:exc:`PythonFinalizationError` during the Python finalization:
* Creating a new Python thread.
* :func:`os.fork`.
See also the :func:`sys.is_finalizing` function.
.. versionadded:: 3.13
Previously, a plain :exc:`RuntimeError` was raised.
.. exception:: RecursionError
This exception is derived from :exc:`RuntimeError`. It is raised when the

View file

@ -1202,6 +1202,8 @@ always available.
Return :const:`True` if the main Python interpreter is
:term:`shutting down <interpreter shutdown>`. Return :const:`False` otherwise.
See also the :exc:`PythonFinalizationError` exception.
.. versionadded:: 3.5
.. data:: last_exc

View file

@ -160,6 +160,21 @@ Other Language Changes
(Contributed by Levi Sabah, Zackery Spytz and Hugo van Kemenade in
:gh:`73965`.)
* Add :exc:`PythonFinalizationError` exception. This exception derived from
:exc:`RuntimeError` is raised when an operation is blocked during
the :term:`Python finalization <interpreter shutdown>`.
The following functions now raise PythonFinalizationError, instead of
:exc:`RuntimeError`:
* :func:`_thread.start_new_thread`.
* :class:`subprocess.Popen`.
* :func:`os.fork`.
* :func:`os.forkpty`.
(Contributed by Victor Stinner in :gh:`114570`.)
New Modules
===========