Skip to content

Allow Condition.notify() when underlying lock is held directly - #1320

Merged
agronholm merged 3 commits into
agronholm:masterfrom
GruffElixir:fix-condition-notify-shared-lock
Sep 12, 2026
Merged

agronholm merged 3 commits into
agronholm:masterfrom
GruffElixir:fix-condition-notify-shared-lock

Conversation

@GruffElixir

Copy link
Copy Markdown
Contributor

Changes

Fixes #1319.

Update Condition to determine the owner task dynamically from the underlying lock's statistics (self._lock.statistics().owner) instead of storing _owner_task separately only within Condition.acquire() and Condition.acquire_nowait().

This resolves two issues:

  1. Condition.notify() and Condition.notify_all() can now be called when the underlying lock is acquired directly (such as when multiple conditions share the same lock).
  2. Condition.notify() now raises RuntimeError if called after the lock has been released (previously _owner_task was retained on the condition instance after release).

Checklist

  • You've added tests (in tests/) which would fail without your patch
  • You've updated the documentation (in docs/), in case of behavior changes or new features
  • You've added a new changelog entry (in docs/versionhistory.rst).

Copilot AI lite review requested due to automatic review settings September 10, 2026 21:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@agronholm agronholm left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Validated against Trio's native implementation. Just a couple of change requests.

Comment thread src/anyio/_core/_synchronization.py Resolved
Comment thread docs/versionhistory.rst Resolved
@GruffElixir

GruffElixir commented Sep 11, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks, cleaned both up: _check_acquired() now reads the underlying lock directly, and the changelog entry is at the bottom of the unreleased fixes. The focused sync tests are green (12 passed).

@agronholm agronholm left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Looks good now, thanks!

@agronholm
agronholm merged commit c716c78 into agronholm:master Sep 12, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Condition.notify() fails when shared lock is held

3 participants