Revert disconnect() to be async again (#1133)

It's the only way to properly clean all background tasks,
which the library makes heavy use for in MTProto/Connection
send and receive loops.

Some parts of the code even relied on the fact that it was
asynchronous (it used to return a future so you could await
it and not be breaking).

It's automatically syncified to reduce the damage of being
a breaking change.
This commit is contained in:
Lonami Exo
2019-03-21 12:21:00 +01:00
parent 8f302bcdb0
commit 04ba2e1fc7
9 changed files with 68 additions and 72 deletions

View File

@@ -4,6 +4,7 @@ import socket
import ssl as ssl_mod
from ...errors import InvalidChecksumError
from ... import helpers
class Connection(abc.ABC):
@@ -92,18 +93,18 @@ class Connection(abc.ABC):
self._send_task = self._loop.create_task(self._send_loop())
self._recv_task = self._loop.create_task(self._recv_loop())
def disconnect(self):
async def disconnect(self):
"""
Disconnects from the server, and clears
pending outgoing and incoming messages.
"""
self._connected = False
if self._send_task:
self._send_task.cancel()
if self._recv_task:
self._recv_task.cancel()
await helpers._cancel(
self._log,
send_task=self._send_task,
recv_task=self._recv_task
)
if self._writer:
self._writer.close()
@@ -148,7 +149,7 @@ class Connection(abc.ABC):
else:
self._log.exception('Unexpected exception in the send loop')
self.disconnect()
await self.disconnect()
async def _recv_loop(self):
"""
@@ -170,7 +171,7 @@ class Connection(abc.ABC):
msg = 'Unexpected exception in the receive loop'
self._log.exception(msg)
self.disconnect()
await self.disconnect()
# Add a sentinel value to unstuck recv
if self._recv_queue.empty():

View File

@@ -111,7 +111,7 @@ class TcpMTProxy(ObfuscatedConnection):
await asyncio.sleep(2)
if self._reader.at_eof():
self.disconnect()
await self.disconnect()
raise ConnectionError(
'Proxy closed the connection after sending initial payload')