python / python/cpython

unusual behaviour in asyncio.TaskGroup in the presence of GeneratorExit, StopIteration, KeyboardInterrupt and SystemExit

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

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

3.11 3.12 topic-asyncio type-bug
主要言語
Python
スター
77.2k
フォーク
36k
PR マージ指標
PR 指標を取得中

説明

import gc
import asyncio
import logging

logger = logging.getLogger(__name__)


async def keyboard_interrupt():
    raise KeyboardInterrupt

async def system_exit():
    raise SystemExit

async def task_groups():
    async with asyncio.TaskGroup() as tg:
        tg.create_task(system_exit())
        tg.create_task(keyboard_interrupt())




loop = asyncio.SelectorEventLoop()
task = loop.create_task(task_groups())

while not task.done():
    try:
        loop.run_until_complete(asyncio.sleep(0))
    except (SystemExit, KeyboardInterrupt) as e:
        print(f"{e=}")

print(f"{task=} {task.exception()=}")
loop.close()

print("#######################################################")

async def gen_exit():
    async with asyncio.TaskGroup() as tg:
        await asyncio.get_running_loop().create_future()


loop = asyncio.SelectorEventLoop()
task = loop.create_task(gen_exit())
loop.run_until_complete(asyncio.sleep(0))
del task
gc.collect()

print("#######################################################")

gives the following output on 3.12:

e=SystemExit()
e=KeyboardInterrupt()
e=SystemExit()
task=<Task finished name='Task-1' coro=<task_groups() done, defined at /home/graingert/projects/cpython/demo.py:14> exception=SystemExit()> task.exception()=SystemExit()
#######################################################
Exception ignored in: <coroutine object gen_exit at 0x7fd62350cd60>
Traceback (most recent call last):
  File "/home/graingert/projects/cpython/demo.py", line 37, in gen_exit
    async with asyncio.TaskGroup() as tg:
  File "/home/graingert/projects/cpython/Lib/asyncio/taskgroups.py", line 134, in __aexit__
    raise me from None
BaseExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
Task was destroyed but it is pending!
task: <Task pending name='Task-7' coro=<gen_exit() done, defined at /home/graingert/projects/cpython/demo.py:36> wait_for=<Future pending cb=[Task.task_wakeup()]>>
#######################################################

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

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

はじめの一歩

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

調査の方向性

まず提供されている Python 3.12 の再現コードを実行し、その後 Lib/asyncio/taskgroups.py の TaskGroup.aexit 周辺と asyncio のタスク作成呼び出しを調べてください。GeneratorExit、StopIteration、KeyboardInterrupt、SystemExit の各経路を追跡し、カバレッジを追加する前に期待される動作を確定してください。この issue では、望ましい結果がまだ指定されていません。

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

評価

技術スタック
python
領域
backend
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

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

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