Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

AsyncioIntegration hides an eager task factory from anyio, which breaks anyio task groups

未关闭
#7,709 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
68/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python

调研方向

Start in sentry_sdk/integrations/asyncio.py at AsyncioIntegration and _sentry_task_factory, then reproduce the failure with Python 3.12+, anyio 4.8+, and the example in the issue. Check the eager-task-factory handling against asyncio.create_eager_task_factory(). Done means anyio recognizes the installed factory, task groups exit normally, and existing span behavior remains intact.

由索引模型根据 Issue 内容生成。

描述

Bug Waiting for: Product Owner
How do you use Sentry?

Sentry Saas (sentry.io)

Version

2.70.0

Steps to Reproduce

Python 3.12 or later, anyio 4.8 or later.

import asyncio

import anyio
import sentry_sdk
from sentry_sdk.integrations.asyncio import AsyncioIntegration


async def child(event):
    await event.wait()


async def main():
    asyncio.get_running_loop().set_task_factory(asyncio.eager_task_factory)
    sentry_sdk.init(integrations=[AsyncioIntegration()])

    async with anyio.create_task_group() as tg:
        tg.start_soon(child, anyio.Event())
        await anyio.sleep(0.01)
        tg.cancel_scope.cancel()


asyncio.run(main())
Expected Result

The task group exits normally, as it does without AsyncioIntegration.

Actual Result
ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
  RuntimeError: Attempted to exit a cancel scope that isn't the current tasks's current cancel scope
Cause

anyio starts the children of a task group non-eagerly, whatever the loop's task factory.
To do that it has to recognise an eager task factory, and asyncio offers no public way to ask.
anyio compares factory.__code__ with asyncio.eager_task_factory.__code__, which every factory returned by asyncio.create_eager_task_factory() shares, and then calls the task constructor from the factory's closure.

AsyncioIntegration replaces the loop's task factory with _sentry_task_factory, a plain function that calls the original.
Its code object is its own, so anyio takes it for an ordinary factory, the child starts eagerly inside create_task(), and anyio's bookkeeping for the task is written after the task has already run.

Proposed fix

When the original factory is an eager one, install a factory built with asyncio.create_eager_task_factory() and wrap the coroutine in its task constructor.
Tasks stay eager, spans are created as before, and anyio recognises the factory.
I have a patch with tests and can open a PR if this approach is acceptable.

主要语言
Python
星标
2.2k
派生
672
平均合并
22 小时 8 分钟
30 天内合并 PR
228

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

getsentry/sentry-python 的其他 Issue

查看 getsentry/sentry-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。