Skip to content

Commit

Permalink
Exit receive loop on IOError or unhandled exceptions
Browse files Browse the repository at this point in the history
May help with #4088.
  • Loading branch information
Lonami committed Apr 29, 2023
1 parent 03ff996 commit ccf67d0
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion telethon/network/connection/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand Down Expand Up @@ -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))
Expand All @@ -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:
Expand Down

0 comments on commit ccf67d0

Please sign in to comment.