From f05bded56fbf01c20e429cd054a06f13ca12a431 Mon Sep 17 00:00:00 2001 From: Sam Gross Date: Mon, 5 Jan 2026 14:06:30 -0500 Subject: [PATCH] gh-143424: Fix assertion in _PyMutex_LockTimed The assertion doesn't necessarily hold for `threading.Lock`, so allow the lock to be unlocked if `_PY_LOCK_PYTHONLOCK` is set on the flags. --- Python/lock.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Python/lock.c b/Python/lock.c index 789065d8162792..12b5ebc89aeec7 100644 --- a/Python/lock.c +++ b/Python/lock.c @@ -124,8 +124,11 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags) &entry, (flags & _PY_LOCK_DETACH) != 0); if (ret == Py_PARK_OK) { if (entry.handed_off) { - // We own the lock now. - assert(_Py_atomic_load_uint8_relaxed(&m->_bits) & _Py_LOCKED); + // We own the lock now. thread.Lock allows other threads + // to concurrently release the lock so we cannot assert that + // it is locked if _PY_LOCK_PYTHONLOCK is set. + assert(_Py_atomic_load_uint8_relaxed(&m->_bits) & _Py_LOCKED || + (flags & _PY_LOCK_PYTHONLOCK) != 0); return PY_LOCK_ACQUIRED; } }