"Attempted to exit cancel scope in a different task than it was entered in" when used with pytest_asyncio.fixture decorator #79
Replies: 3 comments 3 replies
-
It looks like a misuse of Check |
Beta Was this translation helpful? Give feedback.
-
Hey @jeremy656 # conftest.py
import pytest
from pytest_asyncio import is_async_test
def pytest_collection_modifyitems(items) -> None: # noqa: ANN001
"""Set the loop scope to session for async tests."""
pytest_asyncio_tests = (item for item in items if is_async_test(item))
session_scope_marker = pytest.mark.asyncio(loop_scope="session")
for async_test in pytest_asyncio_tests:
async_test.add_marker(session_scope_marker, append=False) You can read more at pytest-dev/pytest-asyncio#61 If this was your issue as well, it might be helpful to include a recipe/cookbook section in the docs for folks. (CC: @frankie567 ) |
Beta Was this translation helpful? Give feedback.
-
@frankie567 I'm able to reproduce this with just an async generator and
(I originally encountered the exception when writing some doctests for an SDK, but initially thought it might be a bad interaction with the Sphinx doctest machinery. This minimal reproducer came from trying to eliminate the doctest failure by using a generator of my own to force the cleanup, but it didn't resolve the issue (since the |
Beta Was this translation helpful? Give feedback.
-
Describe the bug
Using aconnect_ws with @pytest_asyncio.fixture triggers
RuntimeError: Attempted to exit cancel scope in a different task than it was entered in
To Reproduce
NOTE: I am using the patch described in this issue: #72
Expected behavior
Similar behavior to manually using this part in the tests directly. Works perfectly without declaring as fixture.
Configuration
Beta Was this translation helpful? Give feedback.
All reactions