Skip to content

Commit bf4017b

Browse files
overlordevstinner
andauthored
gh-125053: Document that ob_mutex must only be used via critical section API (#144599)
Add a warning in the free-threading extensions howto explaining that PyObject.ob_mutex is reserved for the critical section API and must not be locked directly with PyMutex_Lock, as this can cause deadlocks. Extension authors who need their own lock should add a separate PyMutex field to their object struct. Also add an ob_mutex member entry under PyObject in the C API reference (Doc/c-api/structures.rst) with a cross-reference to the howto. Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 2756d56 commit bf4017b

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

Doc/c-api/structures.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,19 @@ under :ref:`reference counting <countingrefs>`.
4848
Do not use this field directly; use :c:macro:`Py_TYPE` and
4949
:c:func:`Py_SET_TYPE` instead.
5050

51+
.. c:member:: PyMutex ob_mutex
52+
53+
A :ref:`per-object lock <per-object-locks>`, present only in the :term:`free-threaded <free threading>`
54+
build (when :c:macro:`Py_GIL_DISABLED` is defined).
55+
56+
This field is **reserved for use by the critical section API**
57+
(:c:macro:`Py_BEGIN_CRITICAL_SECTION` / :c:macro:`Py_END_CRITICAL_SECTION`).
58+
Do **not** lock it directly with ``PyMutex_Lock``; doing so can cause
59+
deadlocks. If you need your own lock, add a separate :c:type:`PyMutex`
60+
field to your object struct.
61+
62+
.. versionadded:: 3.13
63+
5164

5265
.. c:type:: PyVarObject
5366

Doc/howto/free-threading-extensions.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,30 @@ Important Considerations
384384
internal extension state, standard mutexes or other synchronization
385385
primitives might be more appropriate.
386386

387+
.. _per-object-locks:
388+
389+
Per-Object Locks (``ob_mutex``)
390+
...............................
391+
392+
In the free-threaded build, each Python object contains a :c:member:`~PyObject.ob_mutex`
393+
field of type :c:type:`PyMutex`. This mutex is **reserved for use by the
394+
critical section API** (:c:macro:`Py_BEGIN_CRITICAL_SECTION` /
395+
:c:macro:`Py_END_CRITICAL_SECTION`).
396+
397+
.. warning::
398+
399+
Do **not** lock ``ob_mutex`` directly with ``PyMutex_Lock(&obj->ob_mutex)``.
400+
Mixing direct ``PyMutex_Lock`` calls with the critical section API on the
401+
same mutex can cause deadlocks.
402+
403+
Even if your own code never uses critical sections on a particular object type,
404+
**CPython internals may use the critical section API on any Python object**.
405+
406+
If your extension type needs its own lock, add a separate :c:type:`PyMutex`
407+
field (or another synchronization primitive) to your object struct.
408+
:c:type:`PyMutex` is very lightweight, so there is negligible cost to having
409+
an additional one.
410+
387411

388412
Building Extensions for the Free-Threaded Build
389413
===============================================

0 commit comments

Comments
 (0)