From 39033b2cdffa3ca222e80cac8d2ba0068c58527c Mon Sep 17 00:00:00 2001 From: EmBeckwith Date: Thu, 26 Nov 2026 14:39:36 +0000 Subject: [PATCH 1/4] Fixed asyncio lock.locked returning False when waiters exist --- Lib/asyncio/locks.py | 4 ++-- Lib/test/test_asyncio/test_locks.py | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) 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..ee81cf06cfabfe 100644 --- a/Lib/test/test_asyncio/test_locks.py +++ b/Lib/test/test_asyncio/test_locks.py @@ -186,10 +186,11 @@ 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 + self.assertTrue(tc.done()) async def test_cancel_release_race(self): # Issue 32734 From 56a7ad4c1c45e0154feac3c78b87af4504b2b360 Mon Sep 17 00:00:00 2001 From: EmBeckwith Date: Thu, 26 Nov 2026 14:39:36 +0000 Subject: [PATCH 2/4] Added check for any cancelled waiters --- Lib/asyncio/locks.py | 3 ++- Lib/test/test_asyncio/test_locks.py | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Lib/asyncio/locks.py b/Lib/asyncio/locks.py index 13ab04ac4e8488..75712f28d65839 100644 --- a/Lib/asyncio/locks.py +++ b/Lib/asyncio/locks.py @@ -85,7 +85,8 @@ def __repr__(self): def locked(self): """Return True if lock is acquired or if there are active waiters.""" - return self._locked or self._waiters + return not 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 ee81cf06cfabfe..bed34b7fdb5004 100644 --- a/Lib/test/test_asyncio/test_locks.py +++ b/Lib/test/test_asyncio/test_locks.py @@ -273,6 +273,19 @@ 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() + waiter = asyncio.create_task(lock.acquire()) + await asyncio.sleep(0) + + lock.release() + self.assertTrue(lock.locked()) + + # Cleanup + await waiter + async def test_context_manager(self): lock = asyncio.Lock() self.assertFalse(lock.locked()) From d81e90a5a287c016db76b478c7e64e422f872de7 Mon Sep 17 00:00:00 2001 From: EmBeckwith Date: Thu, 26 Nov 2026 14:39:37 +0000 Subject: [PATCH 3/4] Added blurb --- .../next/Library/2026-11-26-14-05-07.gh-issue-138121.-AVsWv.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-11-26-14-05-07.gh-issue-138121.-AVsWv.rst diff --git a/Misc/NEWS.d/next/Library/2026-11-26-14-05-07.gh-issue-138121.-AVsWv.rst b/Misc/NEWS.d/next/Library/2026-11-26-14-05-07.gh-issue-138121.-AVsWv.rst new file mode 100644 index 00000000000000..7348b5cb23a194 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-26-14-05-07.gh-issue-138121.-AVsWv.rst @@ -0,0 +1,2 @@ +:meth:`asyncio.Lock.locked` now returns ``True`` when it has active waiters. +Contributed by Emerson Beckwith. From 10f7a37485f517f59e1fb52fc1a72b9a956e8cfb Mon Sep 17 00:00:00 2001 From: EmBeckwith Date: Thu, 26 Nov 2026 14:39:37 +0000 Subject: [PATCH 4/4] Removed redundant line --- Lib/test/test_asyncio/test_locks.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_asyncio/test_locks.py b/Lib/test/test_asyncio/test_locks.py index bed34b7fdb5004..c5b0ebcefc65b8 100644 --- a/Lib/test/test_asyncio/test_locks.py +++ b/Lib/test/test_asyncio/test_locks.py @@ -190,7 +190,6 @@ async def lockit(name, blocker): self.assertTrue(ta.done()) self.assertTrue(tb.cancelled()) await tc - self.assertTrue(tc.done()) async def test_cancel_release_race(self): # Issue 32734