Skip to content

#gh-138121 Make asyncio.Lock.locked return True when waiters exist - #153989

Open
EmBeckwith wants to merge 4 commits into
python:mainfrom
EmBeckwith:bugfix/asyncio_lock_locked_returns_true
Open

EmBeckwith wants to merge 4 commits into
python:mainfrom
EmBeckwith:bugfix/asyncio_lock_locked_returns_true

Conversation

@EmBeckwith

Copy link
Copy Markdown

gh-138121 Fix Asyncio.Lock.locked returning True when waiters exist

Currently the behaviour described by Asyncio.Lock.locked() is returning True when there are pending waiters in the loop. As described by the issue, this behaviour is different to the Semaphore primitive.

@bedevere-app

bedevere-app Bot commented Jul 18, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@EmBeckwith EmBeckwith closed this Jul 18, 2026
@EmBeckwith EmBeckwith changed the title #138121 Fixed asyncio lock.locked returning False when waiters exist #gh-138121 Fixed asyncio lock.locked returning False when waiters exist Jul 18, 2026
@EmBeckwith EmBeckwith reopened this Jul 18, 2026
@bedevere-app

bedevere-app Bot commented Jul 18, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@python-cla-bot

python-cla-bot Bot commented Jul 18, 2026 •

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

@EmBeckwith EmBeckwith changed the title #gh-138121 Fixed asyncio lock.locked returning False when waiters exist #gh-138121 Make asyncio lock.locked return True when waiters exist Jul 20, 2026
@EmBeckwith EmBeckwith changed the title #gh-138121 Make asyncio lock.locked return True when waiters exist #gh-138121 Make asyncio.Lock.locked return True when waiters exist Jul 22, 2026
@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Oct 28, 2026

@OsmnvAslan OsmnvAslan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking this up! I checked it against current main (it merges cleanly, and test_locks passes). Two things from a small script:

  • locked() no longer returns a bool: it's None for a lock that was never contended, and an empty deque after contention. Wrapping it in bool(...) would keep the return type.
  • If the only waiter is cancelled but still in _waiters, locked() returns True while acquire() takes the lock immediately. Checking any(not w.cancelled() for w in (self._waiters or ())), as Semaphore.locked() does, would keep the two consistent (and also returns a bool).

A test with the scenario from the issue would also help.

@EmBeckwith

Copy link
Copy Markdown
Author

Cool. I'll get those changes added in!

@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Nov 22, 2026
@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Nov 26, 2026
@EmBeckwith
EmBeckwith force-pushed the bugfix/asyncio_lock_locked_returns_true branch 2 times, most recently from 3a55835 to 3f2b2bb Compare November 26, 2026 13:42
@bedevere-app

bedevere-app Bot commented Nov 26, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@bedevere-app

bedevere-app Bot commented Nov 26, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants