Skip to content

fix(asyncio): Keep an eager task factory recognizable as eager - #7710

Open
iainmcgin wants to merge 1 commit into
getsentry:masterfrom
iainmcgin:eager-task-factory
Open

iainmcgin wants to merge 1 commit into
getsentry:masterfrom
iainmcgin:eager-task-factory

Conversation

@iainmcgin

Copy link
Copy Markdown

Description

AsyncioIntegration wraps the loop's task factory in a plain function. When the original is an eager task factory, libraries can no longer tell: asyncio has no public way to ask, so anyio compares factory.__code__ with the standard library's. anyio then starts a task group's child eagerly, and the task group fails with Attempted to exit a cancel scope that isn't the current tasks's current cancel scope.

When the original factory is eager, the integration now installs one built with asyncio.create_eager_task_factory() and wraps the coroutine in its task constructor. The task is created with the original factory's constructor, read from its closure, because before Python 3.14 the eager factory does not accept eager_start. If the closure is not what is expected, the integration falls back to the plain wrapper. When a library calls the task constructor directly, as anyio does, the constructor sets the task's name itself, because no create_task() follows to apply it.

Issues

AsyncioIntegration wraps the loop's task factory in a plain function. When
the original is an eager task factory, a library that needs to know can no
longer tell: asyncio has no public way to ask, so anyio compares the
factory's code object with the standard library's. anyio then starts the
children of a task group eagerly and the task group fails on exit.

When the original factory is eager, install one built with
asyncio.create_eager_task_factory() and wrap the coroutine in its task
constructor. Any other task factory is wrapped as before.

Fixes getsentry#7709

Assisted-By: Claude <[email protected]>
@iainmcgin
iainmcgin marked this pull request as ready for review September 24, 2026 18:24
@iainmcgin
iainmcgin requested a review from a team as a code owner September 24, 2026 18:24

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant