GeezerVison is showing github.com — gh-157157: Make test_taskgroup_cancel_keeps_outer_cancellation deterministic by iamsharduld · Pull Request #157158 · python/cpython
Conversation
iamsharduld
commented
Sep 8, 2026
•
edited by bedevere-app
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
Fixes the timing dependence described in the issue: the test cancelled the parent task from outside after await asyncio.sleep(0.01), and lost the cancellation message whenever the loop stalled for more than 10 ms before the body task first ran (the mechanism, and a reproducer that stalls the loop with time.sleep(), are in the issue).
The test now synchronizes on two events instead of sleeping. The child signals from its finally block that the group is cancelling, which by then has cancelled the parent task and is waiting for the child, and waits there until the test releases it. Only then does the test cancel the parent task from outside. The ordering is fixed by the callback queue, not by time, so it holds on any build speed and for both task implementations.
Checked:
- The rewritten test passes for
TestTaskGroup and TestEagerTaskTaskGroup, 10 of 10 repeated runs, and the whole test_asyncio.test_taskgroups module passes.
- With the
asyncio.TaskGroup swallows cancellations coming from outside of it #155433 fix reverted locally, the rewritten test still fails in both classes (CancelledError not raised), so it keeps guarding the bug it was written for.
- The event-based ordering was also verified in a standalone harness with the same 20 ms loop stall that makes the current test fail.
Since the original test was added by the gh-155433 fix, this likely wants the same backports.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…deterministic
The test cancelled the parent task from outside after a 10 ms sleep,
expecting the group to still be waiting for its child. If the event loop
stalled for longer than that before the body task first ran, the outer
cancel() landed while the parent's awaited future was already cancelled
by the group, and its message was dropped, failing the test on slow
builds such as the TSan CI job.
Synchronize on events instead: the child signals from its finally block
that the group is cancelling and waits to be released, and only then is
the parent task cancelled from outside.
Uh oh!
There was an error while loading. Please reload this page.
|
I have made the requested changes; please review again
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Thanks for making the requested changes!
: please review the changes made to this pull request.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This was referenced Sep 8, 2026
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
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.
|