Skip to content

Commit

Permalink
pythongh-114570: Add PythonFinalizationError exception
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vstinner committed Feb 14, 2024
1 parent 326119d commit 2f33cd8
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 6 deletions.
18 changes: 18 additions & 0 deletions Doc/library/exceptions.rst
Expand Up @@ -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 operations is blocked during the :term:`Python finalization <interpreter
shutdown>`.

Examples of operations which can be blocked with a
:exc:`PythonFinalizationError` during the Python finalization:

* create 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
Expand Down
2 changes: 2 additions & 0 deletions Doc/library/sys.rst
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions Doc/whatsnew/3.13.rst
Expand Up @@ -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
===========

Expand Down
2 changes: 2 additions & 0 deletions Include/cpython/pyerrors.h
Expand Up @@ -122,4 +122,6 @@ PyAPI_FUNC(void) _Py_NO_RETURN _Py_FatalErrorFunc(

PyAPI_FUNC(void) PyErr_FormatUnraisable(const char *, ...);

PyAPI_DATA(PyObject *) PyExc_PythonFinalizationError;

#define Py_FatalError(message) _Py_FatalErrorFunc(__func__, (message))
1 change: 1 addition & 0 deletions Lib/test/exception_hierarchy.txt
Expand Up @@ -40,6 +40,7 @@ BaseException
├── ReferenceError
├── RuntimeError
│ ├── NotImplementedError
│ ├── PythonFinalizationError
│ └── RecursionError
├── StopAsyncIteration
├── StopIteration
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_pickle.py
Expand Up @@ -564,6 +564,7 @@ def test_exceptions(self):
if exc in (BlockingIOError,
ResourceWarning,
StopAsyncIteration,
PythonFinalizationError,
RecursionError,
EncodingWarning,
BaseExceptionGroup,
Expand Down
@@ -0,0 +1,3 @@
Add :exc:`PythonFinalizationError` exception. This exception derived from
:exc:`RuntimeError` is raised when an operation is blocked during the
:term:`Python finalization <interpreter shutdown>`. Patch by Victor Stinner.
2 changes: 1 addition & 1 deletion Modules/_posixsubprocess.c
Expand Up @@ -1032,7 +1032,7 @@ subprocess_fork_exec_impl(PyObject *module, PyObject *process_args,

PyInterpreterState *interp = _PyInterpreterState_GET();
if ((preexec_fn != Py_None) && interp->finalizing) {
PyErr_SetString(PyExc_RuntimeError,
PyErr_SetString(PyExc_PythonFinalizationError,
"preexec_fn not supported at interpreter shutdown");
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/_threadmodule.c
Expand Up @@ -1304,7 +1304,7 @@ do_start_new_thread(thread_module_state* state,
return -1;
}
if (interp->finalizing) {
PyErr_SetString(PyExc_RuntimeError,
PyErr_SetString(PyExc_PythonFinalizationError,
"can't create new thread at interpreter shutdown");
return -1;
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/_winapi.c
Expand Up @@ -139,7 +139,7 @@ overlapped_dealloc(OverlappedObject *self)
{
/* The operation is still pending -- give a warning. This
will probably only happen on Windows XP. */
PyErr_SetString(PyExc_RuntimeError,
PyErr_SetString(PyExc_PythonFinalizationError,
"I/O operations still in flight while destroying "
"Overlapped object, the process may crash");
PyErr_WriteUnraisable(NULL);
Expand Down
6 changes: 3 additions & 3 deletions Modules/posixmodule.c
Expand Up @@ -7841,7 +7841,7 @@ os_fork1_impl(PyObject *module)

PyInterpreterState *interp = _PyInterpreterState_GET();
if (interp->finalizing) {
PyErr_SetString(PyExc_RuntimeError,
PyErr_SetString(PyExc_PythonFinalizationError,
"can't fork at interpreter shutdown");
return NULL;
}
Expand Down Expand Up @@ -7885,7 +7885,7 @@ os_fork_impl(PyObject *module)
pid_t pid;
PyInterpreterState *interp = _PyInterpreterState_GET();
if (interp->finalizing) {
PyErr_SetString(PyExc_RuntimeError,
PyErr_SetString(PyExc_PythonFinalizationError,
"can't fork at interpreter shutdown");
return NULL;
}
Expand Down Expand Up @@ -8718,7 +8718,7 @@ os_forkpty_impl(PyObject *module)

PyInterpreterState *interp = _PyInterpreterState_GET();
if (interp->finalizing) {
PyErr_SetString(PyExc_RuntimeError,
PyErr_SetString(PyExc_PythonFinalizationError,
"can't fork at interpreter shutdown");
return NULL;
}
Expand Down
5 changes: 5 additions & 0 deletions Objects/exceptions.c
Expand Up @@ -2177,6 +2177,10 @@ SimpleExtendsException(PyExc_Exception, RuntimeError,
SimpleExtendsException(PyExc_RuntimeError, RecursionError,
"Recursion limit exceeded.");

// PythonFinalizationError extends RuntimeError
SimpleExtendsException(PyExc_RuntimeError, PythonFinalizationError,
"Operation blocked during Python finalization.");

/*
* NotImplementedError extends RuntimeError
*/
Expand Down Expand Up @@ -3641,6 +3645,7 @@ static struct static_exception static_exceptions[] = {
ITEM(KeyError), // base: LookupError(Exception)
ITEM(ModuleNotFoundError), // base: ImportError(Exception)
ITEM(NotImplementedError), // base: RuntimeError(Exception)
ITEM(PythonFinalizationError), // base: RuntimeError(Exception)
ITEM(RecursionError), // base: RuntimeError(Exception)
ITEM(UnboundLocalError), // base: NameError(Exception)
ITEM(UnicodeError), // base: ValueError(Exception)
Expand Down
2 changes: 2 additions & 0 deletions Tools/c-analyzer/cpython/globals-to-fix.tsv
Expand Up @@ -189,6 +189,7 @@ Objects/exceptions.c - _PyExc_ProcessLookupError -
Objects/exceptions.c - _PyExc_TimeoutError -
Objects/exceptions.c - _PyExc_EOFError -
Objects/exceptions.c - _PyExc_RuntimeError -
Objects/exceptions.c - _PyExc_PythonFinalizationError -
Objects/exceptions.c - _PyExc_RecursionError -
Objects/exceptions.c - _PyExc_NotImplementedError -
Objects/exceptions.c - _PyExc_NameError -
Expand Down Expand Up @@ -254,6 +255,7 @@ Objects/exceptions.c - PyExc_ProcessLookupError -
Objects/exceptions.c - PyExc_TimeoutError -
Objects/exceptions.c - PyExc_EOFError -
Objects/exceptions.c - PyExc_RuntimeError -
Objects/exceptions.c - PyExc_PythonFinalizationError -
Objects/exceptions.c - PyExc_RecursionError -
Objects/exceptions.c - PyExc_NotImplementedError -
Objects/exceptions.c - PyExc_NameError -
Expand Down

0 comments on commit 2f33cd8

Please sign in to comment.