From 3f2b2bb8ca289355013bc548fd502408c9cb9962 Mon Sep 17 00:00:00 2001 From: Emerson Beckwith Date: Sat, 18 Jul 2026 16:39:05 +0200 Subject: [PATCH 1/3] 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 c59ea15111bef2b..13ab04ac4e8488f 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 320a933e144813d..ee81cf06cfabfee 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 71fb331969c4d6a414c8ebaa1ba995c069f53afa Mon Sep 17 00:00:00 2001 From: Emerson Beckwith Date: Sat, 18 Jul 2026 16:49:50 +0200 Subject: [PATCH 2/3] 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 ee81cf06cfabfee..1b2701307d94d19 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 From 0e38f989412cf8a510033eaeeccbe0211cf9b11e Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 18 Jul 2026 15:06:05 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2026-07-18-15-06-03.gh-issue-138121.A5NY2o.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2026-07-18-15-06-03.gh-issue-138121.A5NY2o.rst 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 000000000000000..cc3c19595ae7b4e --- /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.