-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
gh-128479: fix asyncio staggered race leaking tasks, and logging unhandled exception.append exception #128475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ambv
merged 16 commits into
python:main
from
graingert:fix-exeptions-append-after-cancel-async-stagger
Jan 23, 2025
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
2518c76
fix asyncio staggered leaking tasks, and logging unhandled exceptions…
graingert 9501879
add test
graingert dfe8384
Update Lib/asyncio/staggered.py
graingert dc7c3e3
Merge branch 'main' into fix-exeptions-append-after-cancel-async-stagger
graingert 1c34edc
📜🤖 Added by blurb_it.
blurb-it[bot] 62c83a2
Update Misc/NEWS.d/next/Library/2025-01-04-11-10-04.gh-issue-128479.j…
graingert f75d77a
Merge branch 'main' into fix-exeptions-append-after-cancel-async-stagger
graingert 4a4c027
Update Misc/NEWS.d/next/Library/2025-01-04-11-10-04.gh-issue-128479.j…
graingert dd55eae
Merge branch 'main' into fix-exeptions-append-after-cancel-async-stagger
graingert 9fc105f
Apply suggestions from code review
graingert c4ed6aa
Merge branch 'main' into fix-exeptions-append-after-cancel-async-stagger
graingert 04532ab
Merge branch 'main' into fix-exeptions-append-after-cancel-async-stagger
graingert 620a344
Merge branch 'main' into fix-exeptions-append-after-cancel-async-stagger
graingert 58f39d9
Apply suggestions from code review
graingert fb41c8d
Merge branch 'main' into fix-exeptions-append-after-cancel-async-stagger
graingert f387540
Merge branch 'main' into fix-exeptions-append-after-cancel-async-stagger
graingert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,8 +66,27 @@ async def staggered_race(coro_fns, delay, *, loop=None): | |
enum_coro_fns = enumerate(coro_fns) | ||
winner_result = None | ||
winner_index = None | ||
unhandled_exceptions = [] | ||
exceptions = [] | ||
running_tasks = [] | ||
running_tasks = set() | ||
on_completed_fut = None | ||
|
||
def task_done(task): | ||
running_tasks.discard(task) | ||
if ( | ||
on_completed_fut is not None | ||
and not on_completed_fut.done() | ||
and not running_tasks | ||
): | ||
on_completed_fut.set_result(None) | ||
|
||
if task.cancelled(): | ||
return | ||
|
||
exc = task.exception() | ||
if exc is None: | ||
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 | ||
|
@@ -91,11 +110,11 @@ async def run_one_coro(ok_to_start, previous_failed) -> None: | |
this_failed = locks.Event() | ||
next_ok_to_start = locks.Event() | ||
next_task = loop.create_task(run_one_coro(next_ok_to_start, this_failed)) | ||
running_tasks.append(next_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() | ||
assert len(running_tasks) == this_index + 2 | ||
# Prepare place to put this coroutine's exceptions if not won | ||
exceptions.append(None) | ||
assert len(exceptions) == this_index + 1 | ||
|
@@ -120,31 +139,36 @@ async def run_one_coro(ok_to_start, previous_failed) -> None: | |
# up as done() == True, cancelled() == False, exception() == | ||
# asyncio.CancelledError. This behavior is specified in | ||
# https://bugs.python.org/issue30048 | ||
for i, t in enumerate(running_tasks): | ||
if i != this_index: | ||
current_task = tasks.current_task(loop) | ||
for t in running_tasks: | ||
if t is not current_task: | ||
t.cancel() | ||
|
||
ok_to_start = locks.Event() | ||
first_task = loop.create_task(run_one_coro(ok_to_start, None)) | ||
running_tasks.append(first_task) | ||
# first_task has been appended to running_tasks so first_task is ok to start. | ||
ok_to_start.set() | ||
propagate_cancellation_error = None | ||
try: | ||
# Wait for a growing list of tasks to all finish: poor man's version of | ||
# curio's TaskGroup or trio's nursery | ||
done_count = 0 | ||
while done_count != len(running_tasks): | ||
done, _ = await tasks.wait(running_tasks) | ||
done_count = len(done) | ||
ok_to_start = locks.Event() | ||
first_task = loop.create_task(run_one_coro(ok_to_start, None)) | ||
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: | ||
graingert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
on_completed_fut = loop.create_future() | ||
try: | ||
await on_completed_fut | ||
except exceptions_mod.CancelledError as ex: | ||
propagate_cancellation_error = ex | ||
for task in running_tasks: | ||
task.cancel(*ex.args) | ||
on_completed_fut = None | ||
if __debug__ and unhandled_exceptions: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably this should just always raise it, not sure why this was suppressed in the original code |
||
# If run_one_coro raises an unhandled exception, it's probably a | ||
# programming error, and I want to see it. | ||
if __debug__: | ||
for d in done: | ||
if d.done() and not d.cancelled() and d.exception(): | ||
raise d.exception() | ||
raise ExceptionGroup("staggered race failed", unhandled_exceptions) | ||
if propagate_cancellation_error is not None: | ||
raise propagate_cancellation_error | ||
return winner_result, winner_index, exceptions | ||
finally: | ||
del exceptions | ||
# Make sure no tasks are left running if we leave this function | ||
for t in running_tasks: | ||
ZeroIntensity marked this conversation as resolved.
Show resolved
Hide resolved
|
||
t.cancel() | ||
del exceptions, propagate_cancellation_error, unhandled_exceptions | ||
graingert marked this conversation as resolved.
Show resolved
Hide resolved
|
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
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Library/2025-01-04-11-10-04.gh-issue-128479.jvOrF-.rst
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix :func:`!asyncio.staggered.staggered_race` leaking tasks and issuing an unhandled exception. |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.