-
Notifications
You must be signed in to change notification settings - Fork 34
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
Fire and forget async ctx blocks anti-pattern #70
Open
gsalgado
wants to merge
1
commit into
ethereum:main
Choose a base branch
from
gsalgado:async-ctx-blocks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -164,3 +164,60 @@ class User: | |
Replacing `assert` statements with `raise AssertionError(...)` (or whatever | ||
exception class you prefer) ensures that these checks cannot be trivially | ||
disabled. | ||
|
||
|
||
## Fire and forget async context blocks | ||
|
||
When writing asyncio-based async context blocks (i.e. using async context managers), we sometimes | ||
fail to continuously check that the background task started is still running. For example, | ||
our Connection multiplexing was implemented | ||
(https://github.com/ethereum/trinity/blob/0db2a36706e5327fa040258bb5fef3fae75d9d8c/p2p/connection.py#L132-L153) | ||
using an async context manager that used a bare yield, so its callsites could not perform any | ||
health checks on the task running in the background, hence a crash in the background task that failed | ||
to cancel the connection would leave the service running forever. | ||
|
||
Here's a simpler, self-contained example: | ||
|
||
```python | ||
@contextlib.asynccontextmanager | ||
async def acmanager(stream_writer): | ||
future = asyncio.create_task(producer(stream_writer)) | ||
try: | ||
yield | ||
finally: | ||
if not future.done(): | ||
future.cancel() | ||
with contextlib.suppress(asyncio.CancelledError): | ||
await future | ||
|
||
async def run(): | ||
reader, writer = await asyncio.open_connection(...) | ||
async with acmanager(writer): | ||
await consumer(reader) | ||
``` | ||
|
||
In the above example, if the background task running `producer(stream_writer)` terminates without | ||
closing the the writer, the `run()` function would continue running indefinitely and the exception | ||
from the background task would only propagate once the `await consumer(reader)` was cancelled (e.g | ||
by some external event like a `KeyboardInterrupt`), causing us to leave the `acmanager()` context. | ||
|
||
In order to avoid this we need to make sure our async context managers always yield a reference | ||
to something that can be used to wait for (or check the state) of the background task. In the | ||
above example it could be something like: | ||
|
||
```python | ||
@contextlib.asynccontextmanager | ||
async def acmanager(stream_writer): | ||
future = asyncio.create_task(producer(stream_writer)) | ||
try: | ||
yield future | ||
finally: | ||
... | ||
|
||
async def run(): | ||
reader, writer = await asyncio.open_connection(...) | ||
async with acmanager(writer) as streaming_task: | ||
await asyncio.wait( | ||
[consumer(reader), streaming_task], | ||
return_when=asyncio.FIST_COMPLETED) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo: FIRST_COMPLETED |
||
``` |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.