diff --git a/Lib/asyncio/locks.py b/Lib/asyncio/locks.py index c59ea15111bef2..13ab04ac4e8488 100644 --- a/Lib/asyncio/locks.py +++ b/Lib/asyncio/locks.py @@ -84,8 +84,8 @@ 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 is acquired or if there are active waiters.""" + return self._locked or self._waiters 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 320a933e144813..1b2701307d94d1 100644 --- a/Lib/test/test_asyncio/test_locks.py +++ b/Lib/test/test_asyncio/test_locks.py @@ -186,7 +186,7 @@ async def lockit(name, blocker): tb.cancel() self.assertTrue(lock._waiters[0].cancelled()) await asyncio.sleep(0) - self.assertFalse(lock.locked()) + self.assertTrue(lock.locked()) self.assertTrue(ta.done()) self.assertTrue(tb.cancelled()) await tc diff --git a/Misc/NEWS.d/next/Library/2026-07-18-15-06-03.gh-issue-138121.A5NY2o.rst b/Misc/NEWS.d/next/Library/2026-07-18-15-06-03.gh-issue-138121.A5NY2o.rst new file mode 100644 index 00000000000000..cc3c19595ae7b4 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-18-15-06-03.gh-issue-138121.A5NY2o.rst @@ -0,0 +1 @@ +:meth:`asyncio.Lock.locked` now returns ``True`` when it has active waiters. Contributed by Emerson Beckwith.