Avoids potential deadlocks with C++ static local initialization in
extensions that call PyUnicode_InternFromString during one-time
initialization.
(cherry picked from commit
f62050d65743f0c895f7e6d665936c4e86aa39d5)
Co-authored-by: Sam Gross <colesbury@gmail.com>
--- /dev/null
+Fix a potential deadlock in :c:func:`PyUnicode_InternFromString` and other
+interning functions in the :term:`free-threaded build` when called from C++
+static local initializers.
#endif
#ifdef Py_GIL_DISABLED
-# define LOCK_INTERNED(interp) PyMutex_Lock(&_Py_INTERP_CACHED_OBJECT(interp, interned_mutex))
+# define LOCK_INTERNED(interp) \
+ PyMutex_LockFlags(&_Py_INTERP_CACHED_OBJECT(interp, interned_mutex), \
+ _Py_LOCK_DONT_DETACH)
# define UNLOCK_INTERNED(interp) PyMutex_Unlock(&_Py_INTERP_CACHED_OBJECT(interp, interned_mutex))
#else
# define LOCK_INTERNED(interp)