python / python/cpython

asyncio.as_completed() raises "no running event loop" when created outside a running loop (3.14 regression)

オープン
#157,856 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
Python
スター
77.2k
フォーク
36k
PR マージ指標
PR 指標を取得中

説明

Bug report

Bug description:

Since 3.14, asyncio.as_completed() raises RuntimeError: no running event loop when the iterator is created outside of a running event loop, even though the futures are then driven with loop.run_until_complete(). This pattern works on 3.13 and is the same pattern that asyncio.gather() still supports.

import asyncio

async def work(i):
    await asyncio.sleep(0.01 * i)
    return i

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
tasks = [loop.create_task(work(i)) for i in (2, 1)]
for f in asyncio.as_completed(tasks):
    print(loop.run_until_complete(f))
loop.close()

3.13:

1
2

main (and 3.14):

Traceback (most recent call last):
  File "repro.py", line 10, in <module>
    for f in asyncio.as_completed(tasks):
             ~~~~~~~~~~~~~~~~~~~~^^^^^^^
  File ".../Lib/asyncio/tasks.py", line 581, in __init__
    self._cur_task = current_task()
RuntimeError: no running event loop

The cause is the awaited-by tracking added in gh-91048: _AsCompletedIterator.__init__ calls current_task() unconditionally. gather() got the same tracking but guards it with events._get_running_loop() and falls back to cur_task = None (which future_add_to_awaited_by() already treats as a no-op). as_completed() should do the same.

I have a fix with a regression test ready.

CPython versions tested on:

CPython main branch, 3.14

Operating systems tested on:

macOS

Linked PRs
  • gh-157857

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Lib/asyncio/tasks.py の _AsCompletedIterator.init から開始し、その後 asyncio.gather() における関連する処理と比較します。issue で言及されている回帰テストを確認し、関連する asyncio テストスイートを実行します。報告された loop.run_until_complete() パターンが 3.14 で動作し、既存の asyncio テストを壊さなければ完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
backend
issue の種類
バグ
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。