From 4ab5c7d92dd37ec3e7c854c35a6a77478c76885d Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Wed, 20 Jun 2018 18:19:35 +0200 Subject: [PATCH] Fix reconnect sentinel when not reconnecting in send_loop It would cause issues with the debug logs, since it assumes it's a TLMessage, which have an .obj attribute. Second, the send_loop is also smarter regarding reconnects (since OSError often occur due to not being connected at all, namely ConnectionResetError). --- telethon/network/mtprotosender.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/telethon/network/mtprotosender.py b/telethon/network/mtprotosender.py index 12535bb3..2a423c08 100644 --- a/telethon/network/mtprotosender.py +++ b/telethon/network/mtprotosender.py @@ -338,8 +338,11 @@ class MTProtoSender: self._pending_ack.clear() messages = await self._send_queue.get() - if messages == _reconnect_sentinel and self._reconnecting: - break + if messages == _reconnect_sentinel: + if self._reconnecting: + break + else: + continue if isinstance(messages, list): message = self.state.create_message(MessageContainer(messages)) @@ -364,11 +367,17 @@ class MTProtoSender: continue except asyncio.CancelledError: return - except OSError as e: - __log__.warning('OSError while sending %s', e) - except: - __log__.exception('Unhandled exception while sending') - await asyncio.sleep(1) + except Exception as e: + if isinstance(e, ConnectionError): + __log__.info('Connection reset while sending %s', e) + elif isinstance(e, OSError): + __log__.warning('OSError while sending %s', e) + else: + __log__.exception('Unhandled exception while receiving') + await asyncio.sleep(1) + + self._loop.create_task(self._reconnect()) + break else: # Remove the cancelled messages from pending __log__.info('Some futures were cancelled, aborted send')