-
Notifications
You must be signed in to change notification settings - Fork 39
Ensure spawned libcoro tasks complete when cancelling #1131
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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
13 changes: 13 additions & 0 deletions
13
python/rapidsmpf/rapidsmpf/streaming/core/cancellation.pyi
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 |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import asyncio | ||
|
wence- marked this conversation as resolved.
|
||
| from collections.abc import Awaitable, Callable | ||
|
|
||
| from rapidsmpf.streaming.core.channel import Channel | ||
| from rapidsmpf.streaming.core.context import Context | ||
|
|
||
| async def shutdown_channels(ctx: Context, *chs: Channel) -> None: ... | ||
| async def await_cpp_future( | ||
| future: asyncio.Future[None], *, on_cancel: Callable[[], Awaitable[None]] | None | ||
| ) -> None: ... | ||
77 changes: 77 additions & 0 deletions
77
python/rapidsmpf/rapidsmpf/streaming/core/cancellation.pyx
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 |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| import asyncio | ||
|
|
||
| from rapidsmpf.streaming.core.context cimport Context | ||
|
|
||
|
|
||
| async def shutdown_channels(Context ctx not None, *chs): | ||
| """ | ||
| Shutdown channels, recording and then propagating any exceptions | ||
|
|
||
| Parameters | ||
| ---------- | ||
| ctx | ||
| Streaming context for channel shutdown | ||
| chs | ||
| Channels to shutdown | ||
|
|
||
| Raises | ||
| ------ | ||
| Any exceptions that shutting down the channels produces. | ||
| """ | ||
| results = await asyncio.gather( | ||
| *(ch.shutdown(ctx) for ch in chs), return_exceptions=True | ||
| ) | ||
| errors = [r for r in results if isinstance(r, BaseException)] | ||
| if errors: | ||
| cause = ( | ||
| errors[0] | ||
| if len(errors) == 1 | ||
| else ExceptionGroup("Errors during shutdown of Channels", errors) | ||
| ) | ||
| raise cause | ||
|
|
||
|
|
||
| async def await_cpp_future(future, *, on_cancel=None): | ||
| """ | ||
| Await a C++ future handling cancellation. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| future | ||
| Awaitable future returning None bridging into libcoro. | ||
| on_cancel | ||
| Optional callback to run if cancellation is raised while awaiting | ||
| the future. Must be a zero-argument function that returns an | ||
| awaitable. | ||
|
wence- marked this conversation as resolved.
|
||
| """ | ||
| try: | ||
| # This shield makes sure that if a cancellation is raised, the | ||
| # future is not cancelled. | ||
| await asyncio.shield(future) | ||
| except asyncio.CancelledError as cancelled: | ||
| # The outer awaitable was cancelled, but we must still ensure the | ||
| # C++ future still runs to completion. | ||
| errors = [] | ||
| if on_cancel is not None: | ||
| try: | ||
| # Run cancellation callback, e.g. to shutdown channels that are live | ||
| await on_cancel() | ||
| except BaseException as error: | ||
| errors.append(error) | ||
| try: | ||
| # This could still fail so we catch and reraise the | ||
| # cancellation but recording the C++ exception as well. | ||
| await asyncio.shield(future) | ||
| except BaseException as error: | ||
| errors.append(error) | ||
| if errors: | ||
| cause = ( | ||
| errors[0] | ||
| if len(errors) == 1 | ||
| else ExceptionGroup("Errors during cancellation of C++ awaitable", errors) | ||
| ) | ||
| raise cancelled from cause | ||
| # Otherwise just reraise the cancellation | ||
| raise | ||
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
Oops, something went wrong.
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.