Add asyncio.CancelScope with level-triggered cancellation#145345
Closed
kovan wants to merge 2 commits intopython:mainfrom
Closed
Add asyncio.CancelScope with level-triggered cancellation#145345kovan wants to merge 2 commits intopython:mainfrom
kovan wants to merge 2 commits intopython:mainfrom
Conversation
Introduce CancelScope, an async context manager that provides level-triggered cancellation semantics for asyncio. Once cancelled (via cancel() or deadline expiry), every subsequent await inside the scope raises CancelledError until the scope exits — the coroutine cannot simply catch-and-ignore. This integrates with Task.__step by checking the current scope's state after the existing edge-triggered _must_cancel check. The scope pushes/pops itself on a per-task _current_cancel_scope linked-list stack. Existing edge-triggered mechanisms (cancel(), uncancel(), Timeout, TaskGroup) remain completely unchanged. New public API: - asyncio.CancelScope(*, deadline=None, shield=False) - asyncio.cancel_scope(delay, *, shield=False) - asyncio.cancel_scope_at(when, *, shield=False) Both the Python (_PyTask) and C (_CTask) Task implementations are updated. Code not using CancelScope has zero overhead (_current_cancel_scope is None → single pointer check). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Member
|
See my comment. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
asyncio.CancelScope, an async context manager providing level-triggered cancellation for asynciocancel()or deadline expiry), every subsequentawaitinside the scope raisesCancelledErroruntil the scope exits — the coroutine cannot simply catch-and-ignore_PyTask) and C (_CTask) Task changescancel()/uncancel(),Timeout,TaskGroup) remain completely unchanged; code not usingCancelScopehas zero overheadMotivated by the discuss.python.org proposal to adopt proven Trio/anyio patterns into asyncio. Tracking issue: #145370.
New public API
asyncio.CancelScope(*, deadline=None, shield=False)— async context managerasyncio.cancel_scope(delay, *, shield=False)— convenience with relative delayasyncio.cancel_scope_at(when, *, shield=False)— convenience with absolute timeProperties:
deadline(r/w),shield(r/w),cancel_called(r),cancelled_caught(r)Methods:
cancel(),reschedule(deadline)How it works
CancelScopepushes/pops itself on a per-task_current_cancel_scopelinked-list stack. InTask.__step, after the existing_must_cancelcheck, a newelifchecks whether the current scope is cancelled and not shielded — if so, it re-injectsCancelledError. The fast path (_current_cancel_scope is None) is a single pointer check with zero overhead.Test plan
test_asyncio.test_cancelscopecovering both_PyTaskand_CTaskcancel_scope()/cancel_scope_at()convenience APIsshield=Trueblocking re-injectioncancelled_caughtsemanticstask.cancel()unchangedtest_asynciosuite passes (2,646 tests, 0 failures)