@@ -430,7 +430,11 @@ Initializing and finalizing the interpreter
430430 Some memory allocated by extension modules may not be freed. Some extensions may not
431431 work properly if their initialization routine is called more than once; this can
432432 happen if an application calls :c:func: `Py_Initialize ` and :c:func: `Py_FinalizeEx `
433- more than once.
433+ more than once. :c:func: `Py_FinalizeEx ` must not be called recursively from
434+ within itself. Therefore, it must not be called by any code that may be run
435+ as part of the interpreter shutdown process, such as :py:mod: `atexit `
436+ handlers, object finalizers, or any code that may be run while flushing the
437+ stdout and stderr files.
434438
435439 .. audit-event :: cpython._PySys_ClearAuditHooks "" c.Py_FinalizeEx
436440
@@ -960,6 +964,37 @@ thread, where the CPython global runtime was originally initialized.
960964The only exception is if :c:func:`exec` will be called immediately
961965after.
962966
967+ .. _cautions-regarding-runtime-finalization:
968+
969+ Cautions regarding runtime finalization
970+ ---------------------------------------
971+
972+ In the late stage of :term:`interpreter shutdown`, after attempting to wait for
973+ non-daemon threads to exit (though this can be interrupted by
974+ :class:`KeyboardInterrupt`) and running the :mod:`atexit` functions, the runtime
975+ is marked as *finalizing*: :c:func:`_Py_IsFinalizing` and
976+ :func:`sys.is_finalizing` return true. At this point, only the *finalization
977+ thread* that initiated finalization (typically the main thread) is allowed to
978+ acquire the :term:`GIL`.
979+
980+ If any thread, other than the finalization thread, attempts to acquire the GIL
981+ during finalization, either explicitly via :c:func:`PyGILState_Ensure`,
982+ :c:macro:`Py_END_ALLOW_THREADS`, :c:func:`PyEval_AcquireThread`, or
983+ :c:func:`PyEval_AcquireLock`, or implicitly when the interpreter attempts to
984+ reacquire it after having yielded it, the thread enters **a permanently blocked
985+ state** where it remains until the program exits. In most cases this is
986+ harmless, but this can result in deadlock if a later stage of finalization
987+ attempts to acquire a lock owned by the blocked thread, or otherwise waits on
988+ the blocked thread.
989+
990+ Gross? Yes. This prevents random crashes and/or unexpectedly skipped C++
991+ finalizations further up the call stack when such threads were forcibly exited
992+ here in CPython 3.13 and earlier. The CPython runtime GIL acquiring C APIs
993+ have never had any error reporting or handling expectations at GIL acquisition
994+ time that would've allowed for graceful exit from this situation. Changing that
995+ would require new stable C APIs and rewriting the majority of C code in the
996+ CPython ecosystem to use those with error handling.
997+
963998
964999High-level API
9651000--------------
@@ -1033,11 +1068,14 @@ code, or when embedding the Python interpreter:
10331068 ensues.
10341069
10351070 .. note::
1036- Calling this function from a thread when the runtime is finalizing
1037- will terminate the thread, even if the thread was not created by Python.
1038- You can use :c:func:`Py_IsFinalizing` or :func:`sys.is_finalizing` to
1039- check if the interpreter is in process of being finalized before calling
1040- this function to avoid unwanted termination.
1071+ Calling this function from a thread when the runtime is finalizing will
1072+ hang the thread until the program exits, even if the thread was not
1073+ created by Python. Refer to
1074+ :ref:`cautions-regarding-runtime-finalization` for more details.
1075+
1076+ .. versionchanged:: next
1077+ Hangs the current thread, rather than terminating it, if called while the
1078+ interpreter is finalizing.
10411079
10421080.. c:function:: PyThreadState* PyThreadState_Get()
10431081
@@ -1092,11 +1130,14 @@ with sub-interpreters:
10921130 to call arbitrary Python code. Failure is a fatal error.
10931131
10941132 .. note::
1095- Calling this function from a thread when the runtime is finalizing
1096- will terminate the thread, even if the thread was not created by Python.
1097- You can use :c:func:`Py_IsFinalizing` or :func:`sys.is_finalizing` to
1098- check if the interpreter is in process of being finalized before calling
1099- this function to avoid unwanted termination.
1133+ Calling this function from a thread when the runtime is finalizing will
1134+ hang the thread until the program exits, even if the thread was not
1135+ created by Python. Refer to
1136+ :ref:`cautions-regarding-runtime-finalization` for more details.
1137+
1138+ .. versionchanged:: next
1139+ Hangs the current thread, rather than terminating it, if called while the
1140+ interpreter is finalizing.
11001141
11011142.. c:function:: void PyGILState_Release(PyGILState_STATE)
11021143
@@ -1224,7 +1265,7 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
12241265.. c:function:: void PyThreadState_DeleteCurrent(void)
12251266
12261267 Destroy the current thread state and release the global interpreter lock.
1227- Like :c:func:`PyThreadState_Delete`, the global interpreter lock need not
1268+ Like :c:func:`PyThreadState_Delete`, the global interpreter lock must
12281269 be held. The thread state must have been reset with a previous call
12291270 to :c:func:`PyThreadState_Clear`.
12301271
@@ -1374,17 +1415,20 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
13741415 If this thread already has the lock, deadlock ensues.
13751416
13761417 .. note::
1377- Calling this function from a thread when the runtime is finalizing
1378- will terminate the thread, even if the thread was not created by Python.
1379- You can use :c:func:`Py_IsFinalizing` or :func:`sys.is_finalizing` to
1380- check if the interpreter is in process of being finalized before calling
1381- this function to avoid unwanted termination.
1418+ Calling this function from a thread when the runtime is finalizing will
1419+ hang the thread until the program exits, even if the thread was not
1420+ created by Python. Refer to
1421+ :ref:`cautions-regarding-runtime-finalization` for more details.
13821422
13831423 .. versionchanged:: 3.8
13841424 Updated to be consistent with :c:func:`PyEval_RestoreThread`,
13851425 :c:func:`Py_END_ALLOW_THREADS`, and :c:func:`PyGILState_Ensure`,
13861426 and terminate the current thread if called while the interpreter is finalizing.
13871427
1428+ .. versionchanged:: next
1429+ Hangs the current thread, rather than terminating it, if called while the
1430+ interpreter is finalizing.
1431+
13881432 :c:func:`PyEval_RestoreThread` is a higher-level function which is always
13891433 available (even when threads have not been initialized).
13901434
0 commit comments