Skip to content

Commit

Permalink
Fixed cancellation propagation in inner task groups with spawn (nedba…
Browse files Browse the repository at this point in the history
  • Loading branch information
ermakov-oleg authored and agronholm committed Nov 25, 2019
1 parent 2dba52d commit ccfc86a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
9 changes: 5 additions & 4 deletions anyio/_backends/_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,11 @@ def _filter_cancellation_errors(exceptions: Sequence[BaseException]) -> List[Bas
for exc in exceptions:
if isinstance(exc, ExceptionGroup):
exc.exceptions = TaskGroup._filter_cancellation_errors(exc.exceptions)
if len(exc.exceptions) > 1:
filtered_exceptions.append(exc)
else:
filtered_exceptions.append(exc.exceptions[0])
if exc.exceptions:
if len(exc.exceptions) > 1:
filtered_exceptions.append(exc)
else:
filtered_exceptions.append(exc.exceptions[0])
elif not isinstance(exc, CancelledError):
filtered_exceptions.append(exc)

Expand Down
9 changes: 5 additions & 4 deletions anyio/_backends/_curio.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,11 @@ def _filter_cancellation_errors(exceptions: Sequence[BaseException]) -> List[Bas
for exc in exceptions:
if isinstance(exc, ExceptionGroup):
exc.exceptions = TaskGroup._filter_cancellation_errors(exc.exceptions)
if len(exc.exceptions) > 1:
filtered_exceptions.append(exc)
else:
filtered_exceptions.append(exc.exceptions[0])
if exc.exceptions:
if len(exc.exceptions) > 1:
filtered_exceptions.append(exc)
else:
filtered_exceptions.append(exc.exceptions[0])
elif not isinstance(exc, CancelledError):
filtered_exceptions.append(exc)

Expand Down
15 changes: 15 additions & 0 deletions tests/test_taskgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,3 +507,18 @@ async def fn():
assert len(exc.value.exceptions) == 2
assert str(exc.value.exceptions[0]) == 'parent task failed'
assert str(exc.value.exceptions[1]) == 'child task failed'


@pytest.mark.anyio
async def test_cancel_propagation_with_inner_spawn():
async def g():
async with anyio.create_task_group() as g:
await g.spawn(anyio.sleep, 10)
await anyio.sleep(5)

assert False

async with anyio.create_task_group() as group:
await group.spawn(g)
await anyio.sleep(0.1)
await group.cancel_scope.cancel()

0 comments on commit ccfc86a

Please sign in to comment.