diff --git a/Lib/test/test_asyncio/test_eager_task_factory.py b/Lib/test/test_asyncio/test_eager_task_factory.py index 594a48baa32e52..fd9ce08d43ee34 100644 --- a/Lib/test/test_asyncio/test_eager_task_factory.py +++ b/Lib/test/test_asyncio/test_eager_task_factory.py @@ -217,7 +217,12 @@ def test_staggered_race_with_eager_tasks(self): # See https://github.com/python/cpython/issues/124309 async def fail(): - await asyncio.sleep(0) + # Fail without suspending first. staggered_race() awaits the + # coroutines inline and cancels every one of them that has not + # finished once another one wins, so a coroutine that yields before + # raising may be cancelled instead, and excs[2] would report a + # CancelledError. Raising right away keeps the outcome + # independent of how busy the event loop is. raise ValueError("no good") async def blocked(): diff --git a/Misc/NEWS.d/next/Tests/2026-09-30-00-00-00.gh-issue-158097.pcB3mO.rst b/Misc/NEWS.d/next/Tests/2026-09-30-00-00-00.gh-issue-158097.pcB3mO.rst new file mode 100644 index 00000000000000..ccf4ecfcfdc190 --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2026-09-30-00-00-00.gh-issue-158097.pcB3mO.rst @@ -0,0 +1,2 @@ +Fix a rare flakiness in ``test_staggered_race_with_eager_tasks`` in +``test_asyncio``.