diff --git a/telethon/network/mtprotosender.py b/telethon/network/mtprotosender.py index e8262a62..eb167cff 100644 --- a/telethon/network/mtprotosender.py +++ b/telethon/network/mtprotosender.py @@ -305,6 +305,11 @@ class MTProtoSender: __log__.error('Failed to reconnect automatically.') await self._disconnect(error=ConnectionError()) + def _start_reconnect(self): + """Starts a reconnection in the background.""" + if self._user_connected: + self._loop.create_task(self._reconnect()) + def _clean_containers(self, msg_ids): """ Helper method to clean containers from the pending messages @@ -378,7 +383,7 @@ class MTProtoSender: __log__.exception('Unhandled exception while receiving') await asyncio.sleep(1) - self._loop.create_task(self._reconnect()) + self._start_reconnect() break else: # Remove the cancelled messages from pending @@ -417,7 +422,7 @@ class MTProtoSender: __log__.exception('Unhandled exception while receiving') await asyncio.sleep(1) - self._loop.create_task(self._reconnect()) + self._start_reconnect() break # TODO Check salt, session_id and sequence_number @@ -436,7 +441,7 @@ class MTProtoSender: # an actually broken authkey? __log__.warning('Broken authorization key?: {}'.format(e)) self.state.auth_key = None - self._loop.create_task(self._reconnect()) + self._start_reconnect() break except SecurityError as e: # A step while decoding had the incorrect data. This message