Skip to content

Fix Semaphore fast_acquire outside event loops - #1348

Merged
agronholm merged 3 commits into
agronholm:masterfrom
feiiiiii5:fix/semaphore-fast-acquire
Sep 26, 2026
Merged

agronholm merged 3 commits into
agronholm:masterfrom
feiiiiii5:fix/semaphore-fast-acquire

Conversation

@feiiiiii5

@feiiiiii5 feiiiiii5 commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Changes

No linked issue.

SemaphoreAdapter now forwards its stored fast_acquire option when it lazily creates the backend semaphore. This preserves the documented no-checkpoint behavior for semaphores constructed before an event loop exists.

A backend-parameterized regression test constructs the semaphore synchronously, starts another task, and verifies that an uncontended fast_acquire=True acquisition does not yield to that task. This change is separate from #1331's boolean-argument validation.

Checklist

  • You've added tests (in tests/) which would fail without the 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).

Updating the changelog

The changelog entry links to #1348.

Validation

  • Reproduced the failure on the base commit: an adapter constructed outside the loop yielded to a concurrently started task despite fast_acquire=True.
  • PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 PYTHONPATH=src pytest -q tests/test_synchronization.py — 207 passed.
  • pre-commit run --files src/anyio/_core/_synchronization.py tests/test_synchronization.py docs/versionhistory.rst — all applicable hooks passed.

The full cross-platform CI matrix was not run locally.

@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.

Thanks!

@agronholm
agronholm merged commit 219bb58 into agronholm:master Sep 26, 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.

2 participants