anyio: Assertion error when `current_task()` returns an instance of `_asyncio.Task`
Things to check first
-
I have searched the existing issues and didn’t find my bug already reported there
-
I have checked that my bug is still present in the latest release
AnyIO version
4.7.0
Python version
3.12.8
What happened?
It seems that sometimes when asyncio.current_task() is called in
an instance of _asyncio.Task is returned. This then causes an assertion to be raised in
How can we reproduce the bug?
This is triggered in our test suite (fastapi, httpx, pytest-asyncio) after upgrading to anyio 4.7.0, but I haven’t been able to come up with a MRE so hopefully this is enough to point maintainers to the potential fix.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 3
- Comments: 16 (10 by maintainers)
Commits related to this issue
- Fixed AssertionError when using nest_asyncio This stems from the incorrect placement of `nest_asyncio.apply()`, as it should be called before `asyncio.run()`. Fixes #840. — committed to agronholm/anyio by agronholm 2 years ago
- fix: Add support for task-like objects without strict asyncio.Task checks Expects to fix https://github.com/agronholm/anyio/issues/840 This commit removes the strict `isinstance(key, asyncio.Task)` ... — committed to sigridjineth/anyio by sigridjineth 2 years ago
- Fixed AssertionError when using nest_asyncio (#841) This stems from the incorrect placement of `nest_asyncio.apply()`, as it should be called before `asyncio.run()`. Fixes #840. — committed to sysfce2/python-anyio by agronholm 2 years ago
- Bump anyio from 4.7.0 to 4.8.0 in /api (#350) [//]: # (dependabot-start) ⚠️ **Dependabot is rebasing this PR** ⚠️ Rebasing might not happen immediately, so don't worry if this takes some time.... — committed to azimuth-cloud/azimuth by dependabot[bot] 2 years ago
- Bump anyio from 4.7.0 to 4.8.0 (#226) Bumps [anyio](https://github.com/agronholm/anyio) from 4.7.0 to 4.8.0. <details> <summary>Release notes</summary> <p><em>Sourced from <a href="https://github... — committed to azimuth-cloud/cluster-api-addon-provider by dependabot[bot] 2 years ago
- chore(deps-dev): bump the python-dependencies group in /.github/actions/codecov-cli with 13 updates (#519) Bumps the python-dependencies group in /.github/actions/codecov-cli with 13 updates: | Pack... — committed to DataDog/orchestrion by dependabot[bot] 2 years ago
- chore(deps-dev): bump the python-dependencies group in /.github/actions/codecov-cli with 13 updates (#519) Bumps the python-dependencies group in /.github/actions/codecov-cli with 13 updates: | Pack... — committed to DataDog/orchestrion by dependabot[bot] 2 years ago
- chore(deps-dev): bump the python-dependencies group in /.github/actions/codecov-cli with 13 updates (#519) Bumps the python-dependencies group in /.github/actions/codecov-cli with 13 updates: | Pack... — committed to DataDog/orchestrion by dependabot[bot] 2 years ago
We also found related issue in our project. Here it is: https://github.com/taskiq-python/taskiq/issues/387
As people claim, it only happens on 4.7.0.
I could narrow down the MRE to this code:
Here is the setup:
I tried running this code on
anyio==4.6.2and it worked out.Would you mind verifying that the linked PR works for you?
I figured out what’s happening here. It seems that
nest_asyncio.apply()replaces the asyncio modules insys.moduleswith its own implementations, but not before they’ve already been imported by AnyIO. That’s why theisinstancechecks fail even though the MRO looks identical to real asyncio. I could confirm this by movingnest_asyncio.apply()to the module level:The above snippet works fine.
Did you run nest-asyncio or something similar that replaced asyncio.Task with PyTask?
I think the assertion should be less strict and support anything hashable with a get_coro method