Skip to content

Commit 1b10658

Browse files
committed
Document some examples and notes about non-Python threads with subinterpreters.
1 parent 44c55c2 commit 1b10658

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed

Doc/c-api/init.rst

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,8 +1064,37 @@ Note that the ``PyGILState_*`` functions assume there is only one global
10641064
interpreter (created automatically by :c:func:`Py_Initialize`). Python
10651065
supports the creation of additional interpreters (using
10661066
:c:func:`Py_NewInterpreter`), but mixing multiple interpreters and the
1067-
``PyGILState_*`` API is unsupported.
1067+
``PyGILState_*`` API is unsupported. This is because :c:func:`PyGILState_Ensure`
1068+
and similar functions almost always acquire the :term:`GIL` of the main interpreter
1069+
in threads created by subinterpreters, which can lead to data races or deadlocks.
1070+
1071+
Supporting subinterpreters in non-Python threads
1072+
------------------------------------------------
1073+
1074+
If you would like to support subinterpreters with non-Python created threads, you
1075+
must use the ``PyThreadState_*`` API instead of the traditional ``PyGILState_*``
1076+
API.
1077+
1078+
In particular, you must store the exact interpreter state from the calling
1079+
function and pass it to :c:func:`PyThreadState_New` to ensure that the thread
1080+
acquires the :term:`GIL` of the subinterpreter instead of the main interpreter.
1081+
In turn, this means that the return value of :c:func:`PyInterpreterState_Get`
1082+
should be stored alongside any information passed to the thread.::
1083+
1084+
/* The return value of PyInterpreterState_Get() from the
1085+
function that created this thread. */
1086+
PyInterpreterState *interp = ThreadData->interp;
1087+
PyThreadState *tstate = PyThreadState_New(interp);
1088+
PyThreadState_Swap(tstate);
1089+
1090+
/* GIL of the subinterpreter is now held.
1091+
Perform Python actions here. */
1092+
result = CallSomeFunction();
1093+
/* evaluate result or handle exception */
10681094
1095+
/* Destroy the thread state. No Python API allowed beyond this point. */
1096+
PyThreadState_Clear(tstate);
1097+
PyThreadState_DeleteCurrent();
10691098
10701099
.. _fork-and-threads:
10711100
@@ -1243,6 +1272,10 @@ code, or when embedding the Python interpreter:
12431272
*tstate*, which may be ``NULL``. The global interpreter lock must be held
12441273
and is not released.
12451274
1275+
.. note::
1276+
Similar to :c:func:`PyGILState_Ensure`, this function will hang the
1277+
thread if the runtime is finalizing.
1278+
12461279
12471280
The following functions use thread-local storage, and are not compatible
12481281
with sub-interpreters:
@@ -1269,10 +1302,10 @@ with sub-interpreters:
12691302
When the function returns, the current thread will hold the GIL and be able
12701303
to call arbitrary Python code. Failure is a fatal error.
12711304
1272-
.. note::
1273-
Calling this function from a thread when the runtime is finalizing will
1274-
hang the thread until the program exits, even if the thread was not
1275-
created by Python. Refer to
1305+
.. warning::
1306+
Calling this function when the runtime is finalizing is unsafe. Doing
1307+
so will either hang the thread until the program ends, or fully crash
1308+
the interpreter in rare cases. Refer to
12761309
:ref:`cautions-regarding-runtime-finalization` for more details.
12771310
12781311
.. versionchanged:: 3.14
@@ -1289,14 +1322,18 @@ with sub-interpreters:
12891322
Every call to :c:func:`PyGILState_Ensure` must be matched by a call to
12901323
:c:func:`PyGILState_Release` on the same thread.
12911324
1292-
12931325
.. c:function:: PyThreadState* PyGILState_GetThisThreadState()
12941326
12951327
Get the current thread state for this thread. May return ``NULL`` if no
12961328
GILState API has been used on the current thread. Note that the main thread
12971329
always has such a thread-state, even if no auto-thread-state call has been
12981330
made on the main thread. This is mainly a helper/diagnostic function.
12991331
1332+
.. note::
1333+
This function does not account for thread states created by something
1334+
other than :c:func:`PyGILState_Ensure` (such as :c:func:`PyThreadState_New`).
1335+
Prefer :c:func:`PyThreadState_Get` or :c:func:`PyThreadState_GetUnchecked`
1336+
for most cases.
13001337
13011338
.. c:function:: int PyGILState_Check()
13021339
@@ -1309,6 +1346,11 @@ with sub-interpreters:
13091346
knowing that the GIL is locked can allow the caller to perform sensitive
13101347
actions or otherwise behave differently.
13111348
1349+
.. note::
1350+
If the current Python process has ever created a subinterpreter, this
1351+
function will *always* return ``1``. Prefer :c:func:`PyThreadState_GetUnchecked`
1352+
for most cases.
1353+
13121354
.. versionadded:: 3.4
13131355
13141356

0 commit comments

Comments
 (0)