Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 191
Support async cancellations.#726
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
93b8a42
Add 'AsyncShieldCancellation' context manager
lovelydinosaur 55e4271
Merge branch 'master' into add-shield-cancellation-primitive
lovelydinosaur a3ba6df
Update _synchronization.py
lovelydinosaur 61cef57
Linting
lovelydinosaur aa86c6e
Fix docstring wording
lovelydinosaur 87706d1
Merge branch 'master' into add-shield-cancellation-primitive
lovelydinosaur 962eef9
Add interim 'nocover' to show tests passing.
lovelydinosaur a7afdf5
Add failing test case for HTTP/1.1 cancellations
lovelydinosaur 6846a4f
Neat cleanup for HTTP/1.1 write cancellations
lovelydinosaur 664d02b
Drop 'nocover' for ShieldCancellation
lovelydinosaur 956dbfd
Add failing test case for HTTP/1.1 cancellations during response reading
lovelydinosaur 991888a
Resolve failing test case
lovelydinosaur 57bee0f
Add failing test cases for cancellations on connection pools
lovelydinosaur 8bd548f
Resolve failing test cases
lovelydinosaur 7b9a3f4
Add failing test cases for cancellations on HTTP/2 connections
lovelydinosaur 9c85920
Resolve failing test cases
lovelydinosaur 317e17c
Add failing test cases for cancellations on HTTP/2 connections when r…
lovelydinosaur 5dc0af8
Resolve failing test cases
lovelydinosaur 988bab0
Update CHANGELOG
lovelydinosaur ab359f5
Fix yield behaviour
lovelydinosaur 29e61db
Merge branch 'master' into add-shield-cancellation-primitive
lovelydinosaur 2b8c94a
Merge branch 'master' into add-shield-cancellation-primitive
lovelydinosaur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -171,6 +171,55 @@ async def release(self) -> None: | ||
| self._anyio_semaphore.release() | ||
| class AsyncShieldCancellation: | ||
| # For certain portions of our codebase where we're dealing with | ||
| # closing connections during exception handling we want to shield | ||
| # the operation from being cancelled. | ||
| # | ||
| # with AsyncShieldCancellation(): | ||
| # ... # clean-up operations, shielded from cancellation. | ||
| def __init__(self) -> None: | ||
| """ | ||
| Detect if we're running under 'asyncio' or 'trio' and create | ||
| a shielded scope with the correct implementation. | ||
| """ | ||
| self._backend = sniffio.current_async_library() | ||
| if self._backend == "trio": | ||
| if trio is None: # pragma: nocover | ||
| raise RuntimeError( | ||
| "Running under trio requires the 'trio' package to be installed." | ||
| ) | ||
| self._trio_shield = trio.CancelScope(shield=True) | ||
| else: | ||
| if anyio is None: # pragma: nocover | ||
| raise RuntimeError( | ||
| "Running under asyncio requires the 'anyio' package to be installed." | ||
| ) | ||
| self._anyio_shield = anyio.CancelScope(shield=True) | ||
| def __enter__(self) -> "AsyncShieldCancellation": | ||
| if self._backend == "trio": | ||
| self._trio_shield.__enter__() | ||
| else: | ||
| self._anyio_shield.__enter__() | ||
lovelydinosaur marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return self | ||
| def __exit__( | ||
| self, | ||
| exc_type: Optional[Type[BaseException]] = None, | ||
| exc_value: Optional[BaseException] = None, | ||
| traceback: Optional[TracebackType] = None, | ||
| ) -> None: | ||
| if self._backend == "trio": | ||
| self._trio_shield.__exit__(exc_type, exc_value, traceback) | ||
| else: | ||
| self._anyio_shield.__exit__(exc_type, exc_value, traceback) | ||
| # Our thread-based synchronization primitives... | ||
| @@ -212,3 +261,19 @@ def acquire(self) -> None: | ||
| def release(self) -> None: | ||
| self._semaphore.release() | ||
| class ShieldCancellation: | ||
| # Thread-synchronous codebases don't support cancellation semantics. | ||
| # We have this class because we need to mirror the async and sync | ||
| # cases within our package, but it's just a no-op. | ||
| def __enter__(self) -> "ShieldCancellation": | ||
| return self | ||
| def __exit__( | ||
| self, | ||
| exc_type: Optional[Type[BaseException]] = None, | ||
| exc_value: Optional[BaseException] = None, | ||
| traceback: Optional[TracebackType] = None, | ||
| ) -> None: | ||
| pass | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!
There was an error while loading. Please reload this page.