Skip to content

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

Open
OsmnvAslan wants to merge 1 commit into
python:mainfrom
OsmnvAslan:fix-as-completed-no-running-loop
Open

OsmnvAslan wants to merge 1 commit into
python:mainfrom
OsmnvAslan:fix-as-completed-no-running-loop

Conversation

@OsmnvAslan

Copy link
Copy Markdown

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

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.

1 participant