Skip to content

Commit 534b6b0

Browse files
authored
Add comments about _Py_LOCK_DONT_DETACH usage. (#153817)
1 parent 86dab7c commit 534b6b0

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

Include/internal/pycore_lock.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ _PyMutex_at_fork_reinit(PyMutex *m)
3434

3535
typedef enum _PyLockFlags {
3636
// Do not detach/release the GIL when waiting on the lock.
37+
//
38+
// Note that code executed while holding a mutex with this flag must
39+
// not detach, reach a safepoint or initiate a stop-the-world pause.
40+
// Otherwise, a non-detaching waiter may remain waiting for this mutex and
41+
// prevent the pause from completing.
3742
_Py_LOCK_DONT_DETACH = 0,
3843

3944
// Detach/release the GIL while waiting on the lock.

Objects/unicodeobject.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14726,6 +14726,15 @@ intern_common(PyInterpreterState *interp, PyObject *s /* stolen */,
1472614726
}
1472714727
#endif
1472814728

14729+
// Why _Py_LOCK_DONT_DETACH is used here: waiting for the interned mutex
14730+
// must not detach the thread state. Extension code is expected to
14731+
// detach before blocking on opaque external synchronization. However,
14732+
// the lock used for C++ static initialization is hidden, making
14733+
// that difficult, and it is common for C++ extensions to call
14734+
// PyUnicode_InternFromString() from static initializers. Detaching here
14735+
// can therefore deadlock: a stop-the-world pause may prevent the lock
14736+
// owner from reattaching while the pause waits for another attached
14737+
// thread blocked on the hidden lock.
1472914738
FT_MUTEX_LOCK_FLAGS(INTERN_MUTEX, _Py_LOCK_DONT_DETACH);
1473014739
PyObject *t;
1473114740
{

0 commit comments

Comments
 (0)