You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Make asyncio.Lock.locked() return True when there are waiters #138121
stdlibStandard Library Python modules in the Lib/ directorytopic-asynciotriagedThe issue has been accepted as valid by a triager.type-featureA feature request or enhancement
In issue #97028 everyone seemed to agree that asyncio.{Lock,Semaphore}.locked() should return True if there are waiters, but this change was implemented only for Semaphore.
changed the title [-]Should `asyncio.Lock.locked()` return `True` when there are waiters?[/-][+]Make `asyncio.Lock.locked()` return `True` when there are waiters[/+]on Mar 9, 2026
def locked(self):
"""Returns True if semaphore cannot be acquired immediately."""
# Due to state, or FIFO rules (must allow others to run first).
return self._value == 0 or (
any(not w.cancelled() for w in (self._waiters or ())))
For Lock we have this driven by the state of the Lock
def locked(self):
"""Return True if lock is acquired."""
return self._locked
This gets unset once releasing the lock and I don't see any logic for checking for waiters first.
def release(self):
if self._locked:
self._locked = False
self._wake_up_first()
else:
raise RuntimeError('Lock is not acquired.')
stdlibStandard Library Python modules in the Lib/ directorytopic-asynciotriagedThe issue has been accepted as valid by a triager.type-featureA feature request or enhancement
Activity
ashm-dev commentedon Sep 8, 2025
Can I try to fix this issue?
Just to clarify: am I correct that the main point of this issue is to backport the existing fix (already applied in 3.12+) into the <3.12 branches?
picnixz commentedon Sep 8, 2025
Since we don't know the answer to the question I wouldn't say there is anything to fix. Let's ask @kumaraditya303
olutra commentedon Mar 7, 2026
@kumaraditya303, could you please unblock the issue?
kumaraditya303 commentedon Mar 9, 2026
I don't quite remember the details, maybe @gvanrossum remembers? if this should be case for Lock as well.
gvanrossum commentedon Mar 9, 2026
Yeah, Lock.locked() should definitely return true if there are waiters.
[-]Should `asyncio.Lock.locked()` return `True` when there are waiters?[/-][+]Make `asyncio.Lock.locked()` return `True` when there are waiters[/+]gvanrossum commentedon Mar 10, 2026
Small caveat: Maybe you can prove (or disprove!) that when there are waiters, the locked flag is always set?
johng commentedon Jul 17, 2026
We can see in Semaphore we have
For Lock we have this driven by the state of the Lock
This gets unset once releasing the lock and I don't see any logic for checking for waiters first.
johng commentedon Jul 17, 2026
Was able to reproduce this quite easily with some AI help
EmBeckwith commentedon Jul 20, 2026
Heya new to this but I've made a PR with a fairly naive solution here if someone wants to take a look