Skip to content

gh-157856: Fix asyncio.as_completed() outside a running event loop - #157857

Merged
kumaraditya303 merged 1 commit into
python:mainfrom
OsmnvAslan:fix-as-completed-no-running-loop
Oct 20, 2026
Merged

kumaraditya303 merged 1 commit into
python:mainfrom
OsmnvAslan:fix-as-completed-no-running-loop

Conversation

@OsmnvAslan

Copy link
Copy Markdown
Contributor

Since 3.14 (gh-91048, awaited-by tracking), _AsCompletedIterator.__init__ calls current_task() unconditionally, so creating an as_completed() iterator outside of a running event loop and driving it with loop.run_until_complete() raises RuntimeError: no running event loop. gather() received the same tracking but guards it with events._get_running_loop() and falls back to None, which future_add_to_awaited_by() / future_discard_from_awaited_by() already treat as a no-op in both the Python and C implementations.

This applies the same guard to as_completed() and adds a regression test that fails on main and passes with the fix, for all Task/Future implementation combinations.

AI disclaimer: the diagnosis, fix and test were drafted with Claude (Claude Code); I reviewed them and verified the repro and the full test_asyncio suite locally on a debug build.

Guard the awaited-by tracking in _AsCompletedIterator the same way
gather() does, so that creating the iterator outside of a running
event loop and driving it with loop.run_until_complete() works again.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@python-cla-bot

python-cla-bot Bot commented Sep 20, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@OsmnvAslan

Copy link
Copy Markdown
Contributor Author

Friendly ping now that 3.15 is at rc3: this is a 3.14 regression from gh-91048 (the awaited-by tracking), with a small guard mirroring what gather() already does, and CI is green. Since it's a regression, it may also be a candidate for backporting to 3.14. Happy to adjust anything.

@kumaraditya303
kumaraditya303 merged commit b12fcff into python:main Oct 20, 2026
58 checks passed
@kumaraditya303 kumaraditya303 added topic-asyncio needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Oct 22, 2026
@miss-islington-app

Copy link
Copy Markdown

Thanks @OsmnvAslan for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Thanks @OsmnvAslan for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Oct 22, 2026

Copy link
Copy Markdown

GH-160231 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Oct 22, 2026
@bedevere-app

bedevere-app Bot commented Oct 22, 2026

Copy link
Copy Markdown

GH-160232 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Oct 22, 2026
kumaraditya303 pushed a commit that referenced this pull request Oct 29, 2026
…t loop (GH-157857) (#160232)

gh-157856: Fix `asyncio.as_completed()` outside a running event loop (GH-157857)
(cherry picked from commit b12fcff)

Co-authored-by: Aslan Osmanov <32789812+OsmnvAslan@users.noreply.github.com>
Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
kumaraditya303 pushed a commit that referenced this pull request Oct 29, 2026
…t loop (GH-157857) (#160231)

gh-157856: Fix `asyncio.as_completed()` outside a running event loop (GH-157857)
(cherry picked from commit b12fcff)

Co-authored-by: Aslan Osmanov <32789812+OsmnvAslan@users.noreply.github.com>
Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants