Cancel tasks on reconnect instead of awaiting them

This prevents us from locking forever on any task that doesn't
rely on cancellation tokens, in this case, Connection.recv()'s
_recv_queue.get() would never complete after the server closed
the connection.

Additionally, working with cancellation tokens in asyncio is
somewhat annoying to do.

Last but not least removing the Connection._disconnected future
avoids the need to use its state (if an exception was set it
should be retrieved) to prevent asyncio from complaining, which
it was before.
This commit is contained in:
Lonami Exo
2018-10-21 16:20:05 +02:00
parent f2e77f4030
commit 7dece209a0
3 changed files with 14 additions and 36 deletions

View File

@@ -38,7 +38,7 @@ class MessagePacker:
self._deque.extend(states)
self._ready.set()
async def get(self, cancellation):
async def get(self):
"""
Returns (batch, data) if one or more items could be retrieved.
@@ -47,19 +47,7 @@ class MessagePacker:
"""
if not self._deque:
self._ready.clear()
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, None
await self._ready.wait()
buffer = io.BytesIO()
batch = []