Skip to content

Commit

Permalink
Merge branch 'master' into fix-695
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm committed Sep 12, 2024
2 parents f9a1e1a + d1aea98 commit 9bce41c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ repos:
- id: trailing-whitespace

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.3
rev: v0.6.4
hooks:
- id: ruff
args: [--fix, --show-fixes]
Expand Down
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"sphinx.ext.autodoc",
"sphinx.ext.intersphinx",
"sphinx_autodoc_typehints",
"sphinx_rtd_theme",
]

templates_path = ["_templates"]
Expand Down
1 change: 1 addition & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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
- Fixed asyncio task groups not yielding control to the event loop at exit if there were
no child tasks to wait on

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 @@ -2118,13 +2118,23 @@ async def _run_tests_and_fixtures(
tuple[Awaitable[T_Retval], asyncio.Future[T_Retval]]
],
) -> None:
from _pytest.outcomes import OutcomeException

with receive_stream, self._send_stream:
async for coro, future in receive_stream:
try:
retval = await coro
except CancelledError as exc:
if not future.cancelled():
future.cancel(*exc.args)

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)

0 comments on commit 9bce41c

Please sign in to comment.