python / python/cpython

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

未关闭
#95,571 8 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

3.11 3.12 topic-asyncio type-bug
主要语言
Python
星标
77.2k
派生
36k
平均合并
1 天 9 小时
30 天内合并 PR
558

描述

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. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 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 摘要。