pytest-dev / pytest-dev/pytest-asyncio
Creating `anyio.CancelScope` in async fixture raises `Attempted to exit cancel scope in a different task than it was entered in`
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.7k
- Forks
- 207
- Avg merge
- 5h 35m
- Merged PRs (30d)
- 9
Description
import anyio
import pytest
@pytest.fixture
async def scope():
with anyio.CancelScope() as scope:
yield scope
async def test(scope: anyio.CancelScope):
pass
This will raise a RuntimeError: Attempted to exit cancel scope in a different task than it was entered in.
After looking at the code for fixture cleanup, I don't see an easy way to work around this, but it's quite a severe issue as it hinders working with cancel scopes / code that uses cancel scopes.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the async fixture example and then inspect pytest-asyncio's fixture cleanup handling for async fixtures. Trace how the anyio.CancelScope is entered and exited across tasks. Done means the example completes without the cross-task RuntimeError, with regression coverage for the fixture cleanup behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100