Skip to content

Commit 7626ae4

Browse files
committed
gh-129195: use future_add_to_awaited_by/future_discard_from_awaited_by in staggered race
1 parent 9bb80fa commit 7626ae4

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

Lib/asyncio/staggered.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from . import exceptions as exceptions_mod
99
from . import locks
1010
from . import tasks
11+
from . import futures
1112

1213

1314
async def staggered_race(coro_fns, delay, *, loop=None):
@@ -63,6 +64,7 @@ async def staggered_race(coro_fns, delay, *, loop=None):
6364
"""
6465
# TODO: when we have aiter() and anext(), allow async iterables in coro_fns.
6566
loop = loop or events.get_running_loop()
67+
parent_task = tasks.current_task(loop)
6668
enum_coro_fns = enumerate(coro_fns)
6769
winner_result = None
6870
winner_index = None
@@ -73,6 +75,7 @@ async def staggered_race(coro_fns, delay, *, loop=None):
7375

7476
def task_done(task):
7577
running_tasks.discard(task)
78+
futures.future_discard_from_awaited_by(task, parent_task)
7679
if (
7780
on_completed_fut is not None
7881
and not on_completed_fut.done()
@@ -110,6 +113,7 @@ async def run_one_coro(ok_to_start, previous_failed) -> None:
110113
this_failed = locks.Event()
111114
next_ok_to_start = locks.Event()
112115
next_task = loop.create_task(run_one_coro(next_ok_to_start, this_failed))
116+
futures.future_add_to_awaited_by(next_task, parent_task)
113117
running_tasks.add(next_task)
114118
next_task.add_done_callback(task_done)
115119
# next_task has been appended to running_tasks so next_task is ok to
@@ -148,6 +152,7 @@ async def run_one_coro(ok_to_start, previous_failed) -> None:
148152
try:
149153
ok_to_start = locks.Event()
150154
first_task = loop.create_task(run_one_coro(ok_to_start, None))
155+
futures.future_add_to_awaited_by(first_task, parent_task)
151156
running_tasks.add(first_task)
152157
first_task.add_done_callback(task_done)
153158
# first_task has been appended to running_tasks so first_task is ok to start.
@@ -171,4 +176,4 @@ async def run_one_coro(ok_to_start, previous_failed) -> None:
171176
raise propagate_cancellation_error
172177
return winner_result, winner_index, exceptions
173178
finally:
174-
del exceptions, propagate_cancellation_error, unhandled_exceptions
179+
del exceptions, propagate_cancellation_error, unhandled_exceptions, parent_task

0 commit comments

Comments
 (0)