Skip to content

Commit

Permalink
Give cancelled tasks a chance to exit and cancel their children
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgodwin committed Feb 23, 2018
1 parent f31b4be commit daebab1
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions asgiref/testing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
from concurrent.futures import CancelledError

import async_timeout

Expand All @@ -23,9 +24,17 @@ async def wait(self, timeout=1):
"""
Waits for the application to stop itself and returns any exceptions.
"""
async with async_timeout.timeout(timeout):
await self.future
self.future.result()
try:
async with async_timeout.timeout(timeout):
await self.future
self.future.result()
finally:
if not self.future.done():
self.future.cancel()
try:
await self.future()
except CancelledError:
pass

def stop(self, exceptions=True):
if not self.future.done():
Expand Down Expand Up @@ -60,8 +69,14 @@ async def receive_output(self, timeout=1):
try:
async with async_timeout.timeout(timeout):
return await self.output_queue.get()
except asyncio.TimeoutError:
except asyncio.TimeoutError as e:
# See if we have another error to raise inside
if self.future.done():
self.future.result()
raise
else:
self.future.cancel()
try:
await self.future
except CancelledError:
pass
raise e

0 comments on commit daebab1

Please sign in to comment.