Properly handle cancellation in _ReadyQueue

This commit is contained in:
Lonami Exo 2018-09-29 13:36:05 +02:00
parent 105bd52eee
commit 2d275989cb
2 changed files with 12 additions and 8 deletions

View File

@ -111,12 +111,16 @@ class _ReadyQueue:
Returns a list of all the items added to the queue until now and
clears the list from the queue itself. Returns ``None`` if cancelled.
"""
ready = asyncio.ensure_future(self._ready.wait(), loop=self._loop)
done, pending = await asyncio.wait(
[ready, cancellation],
return_when=asyncio.FIRST_COMPLETED,
loop=self._loop
)
ready = self._loop.create_task(self._ready.wait())
try:
done, pending = await asyncio.wait(
[ready, cancellation],
return_when=asyncio.FIRST_COMPLETED,
loop=self._loop
)
except asyncio.CancelledError:
done = [cancellation]
if cancellation in done:
ready.cancel()
return None

View File

@ -51,8 +51,8 @@ class Connection(abc.ABC):
@property
def disconnected(self):
if not self._disconnected_future:
self._disconnected_future = asyncio.ensure_future(
self._disconnected.wait(), loop=self._loop)
self._disconnected_future = \
self._loop.create_task(self._disconnected.wait())
return self._disconnected_future
def clone(self):