@@ -430,7 +430,11 @@ Initializing and finalizing the interpreter
430
430
Some memory allocated by extension modules may not be freed. Some extensions may not
431
431
work properly if their initialization routine is called more than once; this can
432
432
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.
434
438
435
439
.. audit-event :: cpython._PySys_ClearAuditHooks "" c.Py_FinalizeEx
436
440
@@ -960,6 +964,37 @@ thread, where the CPython global runtime was originally initialized.
960
964
The only exception is if :c:func:`exec` will be called immediately
961
965
after.
962
966
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
+
963
998
964
999
High-level API
965
1000
--------------
@@ -1033,11 +1068,14 @@ code, or when embedding the Python interpreter:
1033
1068
ensues.
1034
1069
1035
1070
.. 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.
1041
1079
1042
1080
.. c:function:: PyThreadState* PyThreadState_Get()
1043
1081
@@ -1092,11 +1130,14 @@ with sub-interpreters:
1092
1130
to call arbitrary Python code. Failure is a fatal error.
1093
1131
1094
1132
.. 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.
1100
1141
1101
1142
.. c:function:: void PyGILState_Release(PyGILState_STATE)
1102
1143
@@ -1224,7 +1265,7 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
1224
1265
.. c:function:: void PyThreadState_DeleteCurrent(void)
1225
1266
1226
1267
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
1228
1269
be held. The thread state must have been reset with a previous call
1229
1270
to :c:func:`PyThreadState_Clear`.
1230
1271
@@ -1374,17 +1415,20 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
1374
1415
If this thread already has the lock, deadlock ensues.
1375
1416
1376
1417
.. 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.
1382
1422
1383
1423
.. versionchanged:: 3.8
1384
1424
Updated to be consistent with :c:func:`PyEval_RestoreThread`,
1385
1425
:c:func:`Py_END_ALLOW_THREADS`, and :c:func:`PyGILState_Ensure`,
1386
1426
and terminate the current thread if called while the interpreter is finalizing.
1387
1427
1428
+ .. versionchanged:: next
1429
+ Hangs the current thread, rather than terminating it, if called while the
1430
+ interpreter is finalizing.
1431
+
1388
1432
:c:func:`PyEval_RestoreThread` is a higher-level function which is always
1389
1433
available (even when threads have not been initialized).
1390
1434
0 commit comments