gh-157856: Fix asyncio.as_completed() outside a running event loop - #157857
Open
OsmnvAslan wants to merge 1 commit into
Open
OsmnvAslan wants to merge 1 commit into
OsmnvAslan wants to merge 1 commit into
Conversation
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>
OsmnvAslan
requested review from
1st1,
asvetlov,
kumaraditya303 and
willingc
as code owners
September 20, 2026 11:54
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Since 3.14 (gh-91048, awaited-by tracking),
_AsCompletedIterator.__init__callscurrent_task()unconditionally, so creating anas_completed()iterator outside of a running event loop and driving it withloop.run_until_complete()raisesRuntimeError: no running event loop.gather()received the same tracking but guards it withevents._get_running_loop()and falls back toNone, whichfuture_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 onmainand 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_asynciosuite locally on a debug build.