Skip to content

Creating anyio.CancelScope in async fixture raises Attempted to exit cancel scope in a different task than it was entered in #1191

@provinzkraut

Description

@provinzkraut
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions