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`

Open
#1,191 7 comments 1 reaction 0 assignees View on GitHub

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.