Skip to content

Commit 675f3d4

Browse files
Deploy preview for PR 1133 🛫
1 parent 689d06e commit 675f3d4

File tree

558 files changed

+4757
-4646
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

558 files changed

+4757
-4646
lines changed

pr-preview/pr-1133/_sources/c-api/init.rst.txt

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,11 @@ Initializing and finalizing the interpreter
447447
freed. Some memory allocated by extension modules may not be freed. Some
448448
extensions may not work properly if their initialization routine is called more
449449
than once; this can happen if an application calls :c:func:`Py_Initialize` and
450-
:c:func:`Py_FinalizeEx` more than once.
450+
:c:func:`Py_FinalizeEx` more than once. :c:func:`Py_FinalizeEx` must not be
451+
called recursively from within itself. Therefore, it must not be called by
452+
any code that may be run as part of the interpreter shutdown process, such
453+
as :py:mod:`atexit` handlers, object finalizers, or any code that may be run
454+
while flushing the stdout and stderr files.
451455
452456
.. audit-event:: cpython._PySys_ClearAuditHooks "" c.Py_FinalizeEx
453457
@@ -1074,6 +1078,37 @@ thread, where the CPython global runtime was originally initialized.
10741078
The only exception is if :c:func:`exec` will be called immediately
10751079
after.
10761080
1081+
.. _cautions-regarding-runtime-finalization:
1082+
1083+
Cautions regarding runtime finalization
1084+
---------------------------------------
1085+
1086+
In the late stage of :term:`interpreter shutdown`, after attempting to wait for
1087+
non-daemon threads to exit (though this can be interrupted by
1088+
:class:`KeyboardInterrupt`) and running the :mod:`atexit` functions, the runtime
1089+
is marked as *finalizing*: :c:func:`Py_IsFinalizing` and
1090+
:func:`sys.is_finalizing` return true. At this point, only the *finalization
1091+
thread* that initiated finalization (typically the main thread) is allowed to
1092+
acquire the :term:`GIL`.
1093+
1094+
If any thread, other than the finalization thread, attempts to acquire the GIL
1095+
during finalization, either explicitly via :c:func:`PyGILState_Ensure`,
1096+
:c:macro:`Py_END_ALLOW_THREADS`, :c:func:`PyEval_AcquireThread`, or
1097+
:c:func:`PyEval_AcquireLock`, or implicitly when the interpreter attempts to
1098+
reacquire it after having yielded it, the thread enters **a permanently blocked
1099+
state** where it remains until the program exits. In most cases this is
1100+
harmless, but this can result in deadlock if a later stage of finalization
1101+
attempts to acquire a lock owned by the blocked thread, or otherwise waits on
1102+
the blocked thread.
1103+
1104+
Gross? Yes. This prevents random crashes and/or unexpectedly skipped C++
1105+
finalizations further up the call stack when such threads were forcibly exited
1106+
here in CPython 3.13.7 and earlier. The CPython runtime GIL acquiring C APIs
1107+
have never had any error reporting or handling expectations at GIL acquisition
1108+
time that would've allowed for graceful exit from this situation. Changing that
1109+
would require new stable C APIs and rewriting the majority of C code in the
1110+
CPython ecosystem to use those with error handling.
1111+
10771112
10781113
High-level API
10791114
--------------
@@ -1147,11 +1182,14 @@ code, or when embedding the Python interpreter:
11471182
ensues.
11481183
11491184
.. note::
1150-
Calling this function from a thread when the runtime is finalizing
1151-
will terminate the thread, even if the thread was not created by Python.
1152-
You can use :c:func:`Py_IsFinalizing` or :func:`sys.is_finalizing` to
1153-
check if the interpreter is in process of being finalized before calling
1154-
this function to avoid unwanted termination.
1185+
Calling this function from a thread when the runtime is finalizing will
1186+
hang the thread until the program exits, even if the thread was not
1187+
created by Python. Refer to
1188+
:ref:`cautions-regarding-runtime-finalization` for more details.
1189+
1190+
.. versionchanged:: next
1191+
Hangs the current thread, rather than terminating it, if called while the
1192+
interpreter is finalizing.
11551193
11561194
.. c:function:: PyThreadState* PyThreadState_Get()
11571195
@@ -1207,11 +1245,14 @@ with sub-interpreters:
12071245
to call arbitrary Python code. Failure is a fatal error.
12081246
12091247
.. note::
1210-
Calling this function from a thread when the runtime is finalizing
1211-
will terminate the thread, even if the thread was not created by Python.
1212-
You can use :c:func:`Py_IsFinalizing` or :func:`sys.is_finalizing` to
1213-
check if the interpreter is in process of being finalized before calling
1214-
this function to avoid unwanted termination.
1248+
Calling this function from a thread when the runtime is finalizing will
1249+
hang the thread until the program exits, even if the thread was not
1250+
created by Python. Refer to
1251+
:ref:`cautions-regarding-runtime-finalization` for more details.
1252+
1253+
.. versionchanged:: next
1254+
Hangs the current thread, rather than terminating it, if called while the
1255+
interpreter is finalizing.
12151256
12161257
.. c:function:: void PyGILState_Release(PyGILState_STATE)
12171258
@@ -1503,17 +1544,20 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
15031544
If this thread already has the lock, deadlock ensues.
15041545
15051546
.. note::
1506-
Calling this function from a thread when the runtime is finalizing
1507-
will terminate the thread, even if the thread was not created by Python.
1508-
You can use :c:func:`Py_IsFinalizing` or :func:`sys.is_finalizing` to
1509-
check if the interpreter is in process of being finalized before calling
1510-
this function to avoid unwanted termination.
1547+
Calling this function from a thread when the runtime is finalizing will
1548+
hang the thread until the program exits, even if the thread was not
1549+
created by Python. Refer to
1550+
:ref:`cautions-regarding-runtime-finalization` for more details.
15111551
15121552
.. versionchanged:: 3.8
15131553
Updated to be consistent with :c:func:`PyEval_RestoreThread`,
15141554
:c:func:`Py_END_ALLOW_THREADS`, and :c:func:`PyGILState_Ensure`,
15151555
and terminate the current thread if called while the interpreter is finalizing.
15161556
1557+
.. versionchanged:: next
1558+
Hangs the current thread, rather than terminating it, if called while the
1559+
interpreter is finalizing.
1560+
15171561
:c:func:`PyEval_RestoreThread` is a higher-level function which is always
15181562
available (even when threads have not been initialized).
15191563

pr-preview/pr-1133/about.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ <h3>瀏覽</h3>
320320
<a href="https://www.python.org/psf/donations/">Please donate.</a>
321321
<br>
322322
<br>
323-
最後更新於 8月 17, 2025 (00:23 UTC)。
323+
最後更新於 8月 18, 2025 (00:23 UTC)。
324324

325325
<a href="/bugs.html">Found a bug</a>?
326326

pr-preview/pr-1133/bugs.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ <h3>瀏覽</h3>
359359
<a href="https://www.python.org/psf/donations/">Please donate.</a>
360360
<br>
361361
<br>
362-
最後更新於 8月 17, 2025 (00:23 UTC)。
362+
最後更新於 8月 18, 2025 (00:23 UTC)。
363363

364364
<a href="/bugs.html">Found a bug</a>?
365365

pr-preview/pr-1133/c-api/abstract.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ <h3>瀏覽</h3>
329329
<a href="https://www.python.org/psf/donations/">Please donate.</a>
330330
<br>
331331
<br>
332-
最後更新於 8月 17, 2025 (00:23 UTC)。
332+
最後更新於 8月 18, 2025 (00:23 UTC)。
333333

334334
<a href="/bugs.html">Found a bug</a>?
335335

pr-preview/pr-1133/c-api/allocation.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ <h3>瀏覽</h3>
346346
<a href="https://www.python.org/psf/donations/">Please donate.</a>
347347
<br>
348348
<br>
349-
最後更新於 8月 17, 2025 (00:23 UTC)。
349+
最後更新於 8月 18, 2025 (00:23 UTC)。
350350

351351
<a href="/bugs.html">Found a bug</a>?
352352

pr-preview/pr-1133/c-api/apiabiversion.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ <h3>瀏覽</h3>
376376
<a href="https://www.python.org/psf/donations/">Please donate.</a>
377377
<br>
378378
<br>
379-
最後更新於 8月 17, 2025 (00:23 UTC)。
379+
最後更新於 8月 18, 2025 (00:23 UTC)。
380380

381381
<a href="/bugs.html">Found a bug</a>?
382382

pr-preview/pr-1133/c-api/arg.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ <h3>瀏覽</h3>
931931
<a href="https://www.python.org/psf/donations/">Please donate.</a>
932932
<br>
933933
<br>
934-
最後更新於 8月 17, 2025 (00:23 UTC)。
934+
最後更新於 8月 18, 2025 (00:23 UTC)。
935935

936936
<a href="/bugs.html">Found a bug</a>?
937937

pr-preview/pr-1133/c-api/bool.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ <h3>瀏覽</h3>
340340
<a href="https://www.python.org/psf/donations/">Please donate.</a>
341341
<br>
342342
<br>
343-
最後更新於 8月 17, 2025 (00:23 UTC)。
343+
最後更新於 8月 18, 2025 (00:23 UTC)。
344344

345345
<a href="/bugs.html">Found a bug</a>?
346346

pr-preview/pr-1133/c-api/buffer.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ <h3>瀏覽</h3>
10221022
<a href="https://www.python.org/psf/donations/">Please donate.</a>
10231023
<br>
10241024
<br>
1025-
最後更新於 8月 17, 2025 (00:23 UTC)。
1025+
最後更新於 8月 18, 2025 (00:23 UTC)。
10261026

10271027
<a href="/bugs.html">Found a bug</a>?
10281028

pr-preview/pr-1133/c-api/bytearray.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ <h3>瀏覽</h3>
400400
<a href="https://www.python.org/psf/donations/">Please donate.</a>
401401
<br>
402402
<br>
403-
最後更新於 8月 17, 2025 (00:23 UTC)。
403+
最後更新於 8月 18, 2025 (00:23 UTC)。
404404

405405
<a href="/bugs.html">Found a bug</a>?
406406

0 commit comments

Comments
 (0)