-
Notifications
You must be signed in to change notification settings - Fork 407
Tear down blocked task chains iteratively #747
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
davidhewitt
merged 3 commits into
pydantic:main
from
rewitt94:fix/iterative-task-cancellation
Aug 19, 2026
Merged
Changes from all commits
Commits
Show all changes
3 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
23 changes: 23 additions & 0 deletions
23
crates/monty/test_cases/refcount__gather_nested_gather_cancel.py
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,23 @@ | ||
| # Test that a nested GatherFuture *item* is cleaned up when the task awaiting it | ||
| # is cancelled. Unlike refcount__gather_nested_cancel, the inner gather is a direct | ||
| # item of the gather the cancelled task is blocked on, not reached via a coroutine. | ||
| import asyncio | ||
|
|
||
|
|
||
| async def leaf(): | ||
| return 1 | ||
|
|
||
|
|
||
| async def task_with_gather_item(): | ||
| return await asyncio.gather(asyncio.gather(leaf())) | ||
|
|
||
|
|
||
| async def task_fail(): | ||
| raise ValueError('outer task failed') | ||
|
|
||
|
|
||
| try: | ||
| result = await asyncio.gather(task_with_gather_item(), task_fail()) # pyright: ignore | ||
| except ValueError: | ||
| pass | ||
| # ref-counts={'asyncio': 1} |
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 |
|---|---|---|
|
|
@@ -53,3 +53,21 @@ Concurrency is cooperative and host-driven. `gather` suspends Monty whenever | |
| every branch is blocked on an external call, hands the pending calls to the | ||
| host, and resumes when the host returns results. There is no preemption, no | ||
| threads, and no in-sandbox scheduler. | ||
|
|
||
| ### A failing `gather` cancels its siblings | ||
|
|
||
| When one child of a `gather` raises, every sibling still running is cancelled where it is blocked and never resumes. | ||
| That includes the tasks of any gather a sibling was itself awaiting. | ||
| CPython leaves those siblings running as tasks on the loop, so: | ||
|
|
||
| ```python | ||
| async def worker(): | ||
| for _ in range(3): | ||
| await asyncio.gather(step()) | ||
| done.append('finished') | ||
| ``` | ||
|
|
||
| appends `'finished'` under CPython after a sibling of `worker()` raises, but not under Monty. | ||
|
|
||
| External calls already passed to the host are not cancelled. | ||
| The host still resolves them and the results are discarded. | ||
|
Comment on lines
+57
to
+73
Contributor
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. How hard would it be to fix this divergence?
Contributor
Author
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. Good spot - I've now created a plan to fix this! This PR remains separate and valid though |
||
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.
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.
P2: When a very large nested-gather tree is cancelled,
queue_gather_taskstraverses every node without pollingResourceTracker. Teardown can therefore exceed the configured time limit and monopolize a worker; propagate a time-check failure or otherwise bound this traversal.(Based on your team's feedback about native-loop time guards.)
View Feedback
Prompt for AI agents
Uh oh!
There was an error while loading. Please reload this page.
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.
Currently, Monty polls the ResourceTracker only in fallible native loops that produce a value and never in any teardown path. So there is no precedent for this.
Timeout enforcement happens at the end of the turn still.