Skip to content

Commit 25e1cf0

Browse files
committed
Add a whatsnew entry.
1 parent 73cccbb commit 25e1cf0

4 files changed

Lines changed: 48 additions & 4 deletions

File tree

Doc/c-api/interp-lifecycle.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,8 @@ which led to other issues (see the warning note at
604604
Gross? Yes. Starting in Python 3.15, there are a number of C APIs that make
605605
it possible to avoid these issues by temporarily preventing finalization:
606606
607+
.. _interpreter-guards:
608+
607609
.. seealso::
608610
609611
:pep:`788`
@@ -703,6 +705,8 @@ it possible to avoid these issues by temporarily preventing finalization:
703705
.. versionadded:: next
704706
705707
708+
.. _interpreter-views:
709+
706710
Interpreter views
707711
-----------------
708712

Doc/c-api/threads.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ as in a :c:macro:`Py_BEGIN_ALLOW_THREADS` block or in a fresh thread, will the
6161
thread not have an attached thread state.
6262
If uncertain, check if :c:func:`PyThreadState_GetUnchecked` returns ``NULL``.
6363

64-
If it turns out that you do need to create a thread state, call :c:func:`PyThreadState_New`
65-
followed by :c:func:`PyThreadState_Swap`, or use the dangerous
66-
:c:func:`PyGILState_Ensure` function.
64+
If it turns out that you do need to create a thread state, it is recommended to
65+
use :c:func:`PyThreadState_Ensure` or :c:func:`PyThreadState_EnsureFromView`,
66+
which will manage the thread state for you.
6767

6868

6969
.. _detaching-thread-state:
@@ -248,6 +248,11 @@ Some notes about this:
248248
.. seealso::
249249
:pep:`788`
250250

251+
.. _c-api-attach-detach:
252+
253+
Attaching/detaching thread states
254+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
255+
251256
.. c:function:: PyThreadState *PyThreadState_Ensure(PyInterpreterGuard *guard)
252257
253258
Ensure that the thread has an attached thread state for the

Doc/howto/free-threading-extensions.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,15 @@ Thread State and GIL APIs
218218
Python provides a set of functions and macros to manage thread state and the
219219
GIL, such as:
220220

221+
* :c:func:`PyThreadState_Ensure`, :c:func:`PyThreadState_EnsureFromView`,
222+
and :c:func:`PyThreadState_Release`
221223
* :c:func:`PyGILState_Ensure` and :c:func:`PyGILState_Release`
222224
* :c:func:`PyEval_SaveThread` and :c:func:`PyEval_RestoreThread`
223225
* :c:macro:`Py_BEGIN_ALLOW_THREADS` and :c:macro:`Py_END_ALLOW_THREADS`
224226

225227
These functions should still be used in the free-threaded build to manage
226228
thread state even when the :term:`GIL` is disabled. For example, if you
227-
create a thread outside of Python, you must call :c:func:`PyGILState_Ensure`
229+
create a thread outside of Python, you must call :c:func:`PyThreadState_Ensure`
228230
before calling into the Python API to ensure that the thread has a valid
229231
Python thread state.
230232

Doc/whatsnew/3.15.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Summary -- Release highlights
8686
* :pep:`782`: :ref:`A new PyBytesWriter C API to create a Python bytes object
8787
<whatsnew315-pybyteswriter>`
8888
* :pep:`803`: :ref:`Stable ABI for Free-Threaded Builds <whatsnew315-abi3t>`
89+
* :pep:`788`: :ref:`Protection against finalization in the C API <whatsnew315-c-api-interpreter-finalization>`
8990
* :ref:`The JIT compiler has been significantly upgraded <whatsnew315-jit>`
9091
* :ref:`Improved error messages <whatsnew315-improved-error-messages>`
9192
* :ref:`The official Windows 64-bit binaries now use the tail-calling interpreter
@@ -446,6 +447,38 @@ If not using a build tool -- or when writing such a tool -- you can select
446447
``abi3t`` by setting the macro :c:macro:`!Py_TARGET_ABI3T` as discussed
447448
in :ref:`abi3-compiling`.
448449

450+
.. _whatsnew315-c-api-interpreter-finalization:
451+
452+
:pep:`788`: Protecting the C API from interpreter finalization
453+
--------------------------------------------------------------
454+
455+
In the C API, :term:`interpreter finalization <interpreter shutdown>` can be
456+
problematic for many extensions, because :term:`attaching <attached thread
457+
state>` a thread state will permanently hang the thread, resulting in deadlocks
458+
and other spurious issues. Additionally, it has historically been impossible
459+
to atomically check whether an interpreter is alive, leading to crashes
460+
when a thread concurrently deletes an interpreter while another thread is
461+
trying to attach to it.
462+
463+
There are now several new suites of APIs to circumvent these problems:
464+
465+
* :ref:`Interpreter guards <interpreter-views>`, which prevent an interpreter
466+
from finalizing.
467+
* :ref:`Interpreter views <interpreter-guards>`, which allow thread-safe access
468+
to an interpreter that may be concurrently finalizing or deleted.
469+
* :ref:`New APIs <c-api-attach-detach>` to automatically attach and detach
470+
thread states that come with built-in protection against finalization.
471+
472+
In addition, APIs in the ``PyGILState`` family (most notably
473+
:c:func:`PyGILState_Ensure` and :c:func:`PyGILState_Release`) have been
474+
:term:`soft deprecated`. There is **no** plan to remove them, and existing
475+
code will continue to work, but there will be no new ``PyGILState`` APIs
476+
in future versions of Python.
477+
478+
.. seealso:: :pep:`788` for further details.
479+
480+
(Contributed by Peter Bierma in :gh:`149101`.)
481+
449482

450483
.. _whatsnew315-improved-error-messages:
451484

0 commit comments

Comments
 (0)