Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed KeyboardInterrupt hanging the asyncio test runner #779

Merged
merged 5 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.
arrives in an exception group)
- Fixed support for Linux abstract namespaces in UNIX sockets that was broken in v4.2
(#781 <https://github.com/agronholm/anyio/issues/781>_; PR by @tapetersen)
- Fixed ``KeyboardInterrupt`` (ctrl+c) hanging the asyncio pytest runner

**4.4.0**

Expand Down
10 changes: 10 additions & 0 deletions src/anyio/_backends/_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2082,13 +2082,23 @@ async def _run_tests_and_fixtures(
tuple[Awaitable[T_Retval], asyncio.Future[T_Retval]]
],
) -> None:
from _pytest.outcomes import OutcomeException
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might cause problems in environments where pytest is not installed? My habit is to do something like

OutcomeException = getattr(sys.modules.get("_pytest.outcomes"), "OutcomeException", ())

which doesn't require it to be installed and also avoids paying for the import if it wouldn't otherwise be used.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not understanding. This is an import in the AnyIO pytest runner. Why would someone try to use that without pytest in the first place?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right, I just wasn't looking at a wide enough context 🤦


with receive_stream, self._send_stream:
async for coro, future in receive_stream:
try:
retval = await coro
except CancelledError:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to extract and pass on the cancel message?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, might be a good idea.

if not future.cancelled():
future.cancel()

raise
except BaseException as exc:
if not future.cancelled():
future.set_exception(exc)

if not isinstance(exc, (Exception, OutcomeException)):
raise
else:
if not future.cancelled():
future.set_result(retval)
Expand Down
27 changes: 27 additions & 0 deletions tests/test_pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,30 @@ async def test_debugger_exit():

result = testdir.runpytest(*pytest_args)
result.assert_outcomes()


@pytest.mark.parametrize("anyio_backend", get_all_backends(), indirect=True)
def test_keyboardinterrupt_during_test(
testdir: Pytester, anyio_backend_name: str
) -> None:
testdir.makepyfile(
f"""
import pytest
from anyio import create_task_group, sleep

@pytest.fixture
def anyio_backend():
return {anyio_backend_name!r}

async def send_keyboardinterrupt():
raise KeyboardInterrupt

@pytest.mark.anyio
async def test_anyio_mark_first():
async with create_task_group() as tg:
tg.start_soon(send_keyboardinterrupt)
await sleep(10)
"""
)

testdir.runpytest_subprocess(*pytest_args, timeout=3)
Loading