Skip to content

Commit 5869873

Browse files
committed
gh-149162: Use _Py_LOCK_DONT_DETACH in intern_common
Avoids potential deadlocks with C++ static local initialization in extensions that call PyUnicode_InternFromString during one-time initialization.
1 parent 8851a06 commit 5869873

3 files changed

Lines changed: 6 additions & 1 deletion

File tree

Include/internal/pycore_pyatomic_ft_wrappers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ extern "C" {
138138
#define FT_ATOMIC_ADD_SSIZE(value, new_value) \
139139
(void)_Py_atomic_add_ssize(&value, new_value)
140140
#define FT_MUTEX_LOCK(lock) PyMutex_Lock(lock)
141+
#define FT_MUTEX_LOCK_FLAGS(lock, flags) PyMutex_LockFlags(lock, flags)
141142
#define FT_MUTEX_UNLOCK(lock) PyMutex_Unlock(lock)
142143

143144
#else
@@ -201,6 +202,7 @@ extern "C" {
201202
#define FT_ATOMIC_STORE_ULLONG_RELAXED(value, new_value) value = new_value
202203
#define FT_ATOMIC_ADD_SSIZE(value, new_value) (void)(value += new_value)
203204
#define FT_MUTEX_LOCK(lock) do {} while (0)
205+
#define FT_MUTEX_LOCK_FLAGS(lock, flags) do {} while (0)
204206
#define FT_MUTEX_UNLOCK(lock) do {} while (0)
205207

206208
#endif
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a potential deadlock in :c:func:`PyUnicode_InternFromString` and other
2+
interning functions in the :term:`free-threaded build` when called from C++
3+
static local initializers.

Objects/unicodeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14347,7 +14347,7 @@ intern_common(PyInterpreterState *interp, PyObject *s /* stolen */,
1434714347
}
1434814348
#endif
1434914349

14350-
FT_MUTEX_LOCK(INTERN_MUTEX);
14350+
FT_MUTEX_LOCK_FLAGS(INTERN_MUTEX, _Py_LOCK_DONT_DETACH);
1435114351
PyObject *t;
1435214352
{
1435314353
int res = PyDict_SetDefaultRef(interned, s, s, &t);

0 commit comments

Comments
 (0)