Repository navigation
[3.15] gh-158097: Make test_staggered_race_with_eager_tasks deterministic (GH-158469) - #161223
Merged
Merged
Conversation
…tic (pythonGH-158469) * pythongh-158097: Make test_staggered_race_with_eager_tasks deterministic staggered_race() awaits the coroutines it is given inline, so the "await asyncio.sleep(0)" in the test's fail() helper suspended the run_one_coro() task running it and left a scheduled resumption behind. If the asyncio.sleep(1) winner (started 0.25s late by the stagger delay) completes before that resumption runs, the winner's cancel() sets _must_cancel and the resumption throws CancelledError into fail(), so excs[2] reports a CancelledError instead of a ValueError. Pending timers are only moved onto the ready queue at the top of BaseEventLoop._run_once() and are appended after the handles already queued, so the t=0.50s stagger timer and the t=1.25s sleep(1) timer run in due-time order. When both are due in the same _run_once() - which is what a busy CI machine causes - the stagger timer starts fail(), which immediately suspends again, and the very next handle is the winner. Raise straight away instead, so the ValueError is stored in excs[2] in the same step that starts the coroutine and there is no longer a scheduled resumption for a cancellation to overtake. The outcome no longer depends on the event loop getting another iteration. * Update Lib/test/test_asyncio/test_eager_task_factory.py * Delete Misc/NEWS.d/next/Tests/2026-09-30-00-00-00.gh-issue-158097.pcB3mO.rst --------- (cherry picked from commit 3945eaf2fcd9cbc3f3061275bd29751c8b2e7d3b) Co-authored-by: Abhinav <alpha9coder@gmail.com> Co-authored-by: abhinav-phi <abhinav-phi@users.noreply.github.com> Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
miss-islington
requested review from
1st1,
asvetlov,
kumaraditya303 and
willingc
as code owners
November 2, 2026 23:08
asvetlov
enabled auto-merge (squash)
November 2, 2026 23:10
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
staggered_race() awaits the coroutines it is given inline, so the
"await asyncio.sleep(0)" in the test's fail() helper suspended the
run_one_coro() task running it and left a scheduled resumption behind.
If the asyncio.sleep(1) winner (started 0.25s late by the stagger delay)
completes before that resumption runs, the winner's cancel() sets
_must_cancel and the resumption throws CancelledError into fail(), so
excs[2] reports a CancelledError instead of a ValueError.
Pending timers are only moved onto the ready queue at the top of
BaseEventLoop._run_once() and are appended after the handles already
queued, so the t=0.50s stagger timer and the t=1.25s sleep(1) timer run
in due-time order. When both are due in the same _run_once() - which is
what a busy CI machine causes - the stagger timer starts fail(), which
immediately suspends again, and the very next handle is the winner.
Raise straight away instead, so the ValueError is stored in excs[2] in
the same step that starts the coroutine and there is no longer a
scheduled resumption for a cancellation to overtake. The outcome no
longer depends on the event loop getting another iteration.
Update Lib/test/test_asyncio/test_eager_task_factory.py
Delete Misc/NEWS.d/next/Tests/2026-09-30-00-00-00.gh-issue-158097.pcB3mO.rst
(cherry picked from commit 3945eaf)
Co-authored-by: Abhinav alpha9coder@gmail.com
Co-authored-by: abhinav-phi abhinav-phi@users.noreply.github.com
Co-authored-by: Andrew Svetlov andrew.svetlov@gmail.com