Skip to content

Commit

Permalink
Fixed RuntimeError on asyncio when CancellationError falls through to…
Browse files Browse the repository at this point in the history
… BlockingPortal

Fixes #357.
  • Loading branch information
agronholm committed Aug 12, 2021
1 parent bd8d1a1 commit ca9a4b0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ Version history

This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.

**UNRELEASED**

- Fixed ``RuntimeError`` on asyncio when a ``CancelledError`` is raised from a task spawned through
a ``BlockingPortal`` (`#357 <https://github.com/agronholm/anyio/issues/357>`_)

**3.3.0**

- Added asynchronous ``Path`` class
Expand Down
2 changes: 1 addition & 1 deletion src/anyio/from_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ async def stop(self, cancel_remaining: bool = False) -> None:
async def _call_func(self, func: Callable, args: tuple, kwargs: Dict[str, Any],
future: Future) -> None:
def callback(f: Future) -> None:
if f.cancelled():
if f.cancelled() and self._event_loop_thread_id not in (None, threading.get_ident()):
self.call(scope.cancel)

try:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_from_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,14 @@ def taskfunc(*, task_status: TaskStatus) -> None:
future, start_value = portal.start_task(
taskfunc, name='testname') # type: ignore[arg-type]
assert start_value == 'testname'

@pytest.mark.parametrize('anyio_backend', ['asyncio'])
async def test_asyncio_run_sync_called(self, caplog):
"""Regression test for #357."""
async def in_loop():
raise CancelledError

async with BlockingPortal() as portal:
await to_thread.run_sync(portal.start_task_soon, in_loop)

assert not caplog.text

0 comments on commit ca9a4b0

Please sign in to comment.