Skip to content

Commit

Permalink
TaskGroup.cancel_remaining must wait for the tasks
Browse files Browse the repository at this point in the history
Add test that cancel_remaining waits
  • Loading branch information
Neil Booth committed Aug 9, 2018
1 parent 6a903a7 commit d3695a7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 3 additions & 1 deletion aiorpcx/curio.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
CancelledError, get_event_loop, Queue, Event, sleep, Task
)
from collections import deque
from contextlib import suppress
from functools import partial

from aiorpcx.util import normalize_corofunc, check_task
Expand Down Expand Up @@ -243,7 +244,8 @@ async def cancel_remaining(self):
pending = list(self._pending)
for task in pending:
task.cancel()
await sleep(0)
with suppress(CancelledError):
await task

def __aiter__(self):
return self
Expand Down
18 changes: 17 additions & 1 deletion tests/test_curio.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from asyncio import (
sleep, CancelledError, get_event_loop, Event, InvalidStateError,
sleep, CancelledError, get_event_loop, Event, InvalidStateError
)
import time

Expand Down Expand Up @@ -1381,6 +1381,22 @@ async def main():
await main()


@pytest.mark.asyncio
async def test_task_group_cancel_remaining_waits():
async def sleep_soundly():
try:
await sleep(0.01)
except CancelledError:
await sleep(0.01)

task = await spawn(sleep_soundly)
with pytest.raises(CancelledError):
async with TaskGroup([task]) as g:
await sleep(0) # ensure the tasks are scheduled
raise CancelledError
assert task.done()


@pytest.mark.asyncio
async def test_task_group_use_error():
async def main():
Expand Down

0 comments on commit d3695a7

Please sign in to comment.