diff --git a/Doc/library/asyncio-sync.rst b/Doc/library/asyncio-sync.rst index f9e98e05cab7ac..42820ab05b18ff 100644 --- a/Doc/library/asyncio-sync.rst +++ b/Doc/library/asyncio-sync.rst @@ -92,7 +92,11 @@ Lock .. method:: locked() - Return ``True`` if the lock is *locked*. + Return ``True`` if the lock can not be acquired immediately. + + .. versionchanged:: next + Also return ``True`` if the lock is *unlocked* but other tasks are + waiting to acquire it. Event diff --git a/Lib/asyncio/locks.py b/Lib/asyncio/locks.py index f2cf7c72ee8ed3..30bf8fe2c9fffd 100644 --- a/Lib/asyncio/locks.py +++ b/Lib/asyncio/locks.py @@ -84,8 +84,10 @@ def __repr__(self): return f'<{res[1:-1]} [{extra}]>' def locked(self): - """Return True if lock is acquired.""" - return self._locked + """Return True if lock cannot be acquired immediately.""" + # Due to state, or FIFO rules (must allow others to run first). + return self._locked or ( + any(not w.cancelled() for w in (self._waiters or ()))) async def acquire(self): """Acquire a lock. diff --git a/Lib/test/test_asyncio/test_locks.py b/Lib/test/test_asyncio/test_locks.py index f0e779c7a80409..960073e0944581 100644 --- a/Lib/test/test_asyncio/test_locks.py +++ b/Lib/test/test_asyncio/test_locks.py @@ -186,10 +186,12 @@ async def lockit(name, blocker): tb.cancel() self.assertTrue(lock._waiters[0].cancelled()) await asyncio.sleep(0) - self.assertFalse(lock.locked()) + # A released the lock, and it is now reserved for C. + self.assertTrue(lock.locked()) self.assertTrue(ta.done()) self.assertTrue(tb.cancelled()) await tc + self.assertFalse(lock.locked()) async def test_cancel_release_race(self): # Issue 32734 @@ -272,6 +274,31 @@ async def test_release_no_waiters(self): lock.release() self.assertFalse(lock.locked()) + async def test_locked_with_waiters(self): + lock = asyncio.Lock() + await lock.acquire() + + t1 = asyncio.create_task(lock.acquire()) + t2 = asyncio.create_task(lock.acquire()) + await asyncio.sleep(0) + self.assertEqual(len(lock._waiters), 2) + + # The lock is released, but it is reserved for the first waiter. + lock.release() + self.assertIs(lock.locked(), True) + + await t1 + self.assertIs(lock.locked(), True) + + # A cancelled waiter does not keep the lock locked. + t2.cancel() + lock.release() + self.assertIs(lock.locked(), False) + + with self.assertRaises(asyncio.CancelledError): + await t2 + self.assertIs(lock.locked(), False) + async def test_context_manager(self): lock = asyncio.Lock() self.assertFalse(lock.locked()) diff --git a/Misc/NEWS.d/next/Library/2026-11-27-17-28-49.gh-issue-138121.Lk3wQa.rst b/Misc/NEWS.d/next/Library/2026-11-27-17-28-49.gh-issue-138121.Lk3wQa.rst new file mode 100644 index 00000000000000..1db7ca32a8e085 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-27-17-28-49.gh-issue-138121.Lk3wQa.rst @@ -0,0 +1,4 @@ +Fix :meth:`asyncio.Lock.locked` returning ``False`` for a lock that has been +released while other tasks are still waiting to acquire it. It now returns +``True`` whenever the lock cannot be acquired immediately, like +:meth:`asyncio.Semaphore.locked`.