From ccf67d0f4f188ce46d17424219bba9b370eb8b0a Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sat, 29 Apr 2023 12:53:25 +0200 Subject: [PATCH] Exit receive loop on IOError or unhandled exceptions May help with #4088. --- telethon/network/connection/connection.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/telethon/network/connection/connection.py b/telethon/network/connection/connection.py index cb83b9d74..bbfc35c6e 100644 --- a/telethon/network/connection/connection.py +++ b/telethon/network/connection/connection.py @@ -253,7 +253,8 @@ async def disconnect(self): Disconnects from the server, and clears pending outgoing and incoming messages. """ - self._connected = False + if not self._connected: + return await helpers._cancel( self._log, @@ -278,6 +279,8 @@ async def disconnect(self): # * ConnectionResetError self._log.info('%s during disconnect: %s', type(e), e) + self._connected = False + def send(self, data): """ Sends a packet of data through this connection mode. @@ -333,6 +336,7 @@ async def _recv_loop(self): except (IOError, asyncio.IncompleteReadError) as e: self._log.warning('Server closed the connection: %s', e) await self._recv_queue.put((None, e)) + await self.disconnect() except InvalidChecksumError as e: self._log.warning('Server response had invalid checksum: %s', e) await self._recv_queue.put((None, e)) @@ -342,6 +346,7 @@ async def _recv_loop(self): except Exception as e: self._log.exception('Unexpected exception in the receive loop') await self._recv_queue.put((None, e)) + await self.disconnect() else: await self._recv_queue.put((data, None)) except asyncio.CancelledError: