From 86c0958e094fd8f16c3876ec413dbaf8c14504b7 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Sun, 22 Feb 2026 11:24:18 +0000 Subject: [PATCH 1/4] simplify staggered race --- Lib/asyncio/staggered.py | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/Lib/asyncio/staggered.py b/Lib/asyncio/staggered.py index 845aed4c6a3b352..a1ba9d6e75fff54 100644 --- a/Lib/asyncio/staggered.py +++ b/Lib/asyncio/staggered.py @@ -90,11 +90,7 @@ def task_done(task): return unhandled_exceptions.append(exc) - async def run_one_coro(ok_to_start, previous_failed) -> None: - # in eager tasks this waits for the calling task to append this task - # to running_tasks, in regular tasks this wait is a no-op that does - # not yield a future. See gh-124309. - await ok_to_start.wait() + async def run_one_coro(previous_failed) -> None: # Wait for the previous task to finish, or for delay seconds if previous_failed is not None: with contextlib.suppress(exceptions_mod.TimeoutError): @@ -110,14 +106,13 @@ async def run_one_coro(ok_to_start, previous_failed) -> None: return # Start task that will run the next coroutine this_failed = locks.Event() - next_ok_to_start = locks.Event() - next_task = loop.create_task(run_one_coro(next_ok_to_start, this_failed)) + next_task = loop.create_task( + run_one_coro(this_failed), + eager_start=False, + ) futures.future_add_to_awaited_by(next_task, parent_task) running_tasks.add(next_task) next_task.add_done_callback(task_done) - # next_task has been appended to running_tasks so next_task is ok to - # start. - next_ok_to_start.set() # Prepare place to put this coroutine's exceptions if not won exceptions.append(None) assert len(exceptions) == this_index + 1 @@ -149,13 +144,11 @@ async def run_one_coro(ok_to_start, previous_failed) -> None: propagate_cancellation_error = None try: - ok_to_start = locks.Event() - first_task = loop.create_task(run_one_coro(ok_to_start, None)) + first_task = loop.create_task(run_one_coro(None), eager_start=False) futures.future_add_to_awaited_by(first_task, parent_task) running_tasks.add(first_task) first_task.add_done_callback(task_done) # first_task has been appended to running_tasks so first_task is ok to start. - ok_to_start.set() propagate_cancellation_error = None # Make sure no tasks are left running if we leave this function while running_tasks: From 2642a990fab6d8ae0bfd150ca1f77323dd0164bf Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 22 Feb 2026 11:28:08 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2026-02-22-11-27-57.gh-issue-145107.ZlQXEZ.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2026-02-22-11-27-57.gh-issue-145107.ZlQXEZ.rst diff --git a/Misc/NEWS.d/next/Library/2026-02-22-11-27-57.gh-issue-145107.ZlQXEZ.rst b/Misc/NEWS.d/next/Library/2026-02-22-11-27-57.gh-issue-145107.ZlQXEZ.rst new file mode 100644 index 000000000000000..affd886ef6a0628 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-02-22-11-27-57.gh-issue-145107.ZlQXEZ.rst @@ -0,0 +1 @@ +simplify asyncio's ``staggered_race`` using ``eager_start=False`` From 07157b648916bc9a6359890e6d3889e08d2210b0 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Fri, 24 Jul 2026 10:53:47 +0530 Subject: [PATCH 3/4] Update Lib/asyncio/staggered.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- Lib/asyncio/staggered.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/asyncio/staggered.py b/Lib/asyncio/staggered.py index a1ba9d6e75fff54..b9e7b9eb29594fb 100644 --- a/Lib/asyncio/staggered.py +++ b/Lib/asyncio/staggered.py @@ -148,7 +148,7 @@ async def run_one_coro(previous_failed) -> None: futures.future_add_to_awaited_by(first_task, parent_task) running_tasks.add(first_task) first_task.add_done_callback(task_done) - # first_task has been appended to running_tasks so first_task is ok to start. + # first_task has been appended to running_tasks before the event loop starts running it. propagate_cancellation_error = None # Make sure no tasks are left running if we leave this function while running_tasks: From c269d9580aa9f6a3778ea214b0390d3d4fa85259 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Fri, 24 Jul 2026 10:53:55 +0530 Subject: [PATCH 4/4] Update Misc/NEWS.d/next/Library/2026-02-22-11-27-57.gh-issue-145107.ZlQXEZ.rst Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../next/Library/2026-02-22-11-27-57.gh-issue-145107.ZlQXEZ.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2026-02-22-11-27-57.gh-issue-145107.ZlQXEZ.rst b/Misc/NEWS.d/next/Library/2026-02-22-11-27-57.gh-issue-145107.ZlQXEZ.rst index affd886ef6a0628..c15afb53d6db4a6 100644 --- a/Misc/NEWS.d/next/Library/2026-02-22-11-27-57.gh-issue-145107.ZlQXEZ.rst +++ b/Misc/NEWS.d/next/Library/2026-02-22-11-27-57.gh-issue-145107.ZlQXEZ.rst @@ -1 +1 @@ -simplify asyncio's ``staggered_race`` using ``eager_start=False`` +Simplify ``asyncio.staggered_race`` by using ``eager_start=False``.