From bfa3001f871566976dc427f9b34805eab63f99c1 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Fri, 22 Sep 2017 16:02:10 +0200 Subject: [PATCH] Fix MainThread would lock when reconnecting This is because it was thinking that the ReadThread would be ready to read the result, but actually, this thread is also locked trying to reconnect at the same time --- telethon/telegram_client.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/telethon/telegram_client.py b/telethon/telegram_client.py index 0deb9030..2f848434 100644 --- a/telethon/telegram_client.py +++ b/telethon/telegram_client.py @@ -245,19 +245,19 @@ class TelegramClient(TelegramBareClient): """ # This is only valid when the read thread is reconnecting, # that is, the connection lock is locked. - on_read_thread = self._on_read_thread() - if on_read_thread and not self._connect_lock.locked(): + if self._on_read_thread() and not self._connect_lock.locked(): raise AssertionError('Cannot invoke requests from the ReadThread') self.updates.check_error() try: - # Users may call this method from within some update handler. - # If this is the case, then the thread invoking the request - # will be the one which should be reading (but is invoking the - # request) thus not being available to read it "in the background" - # and it's needed to call receive. - call_receive = on_read_thread or self._recv_thread is None + # We should call receive from this thread if there's no background + # thread reading or if the server disconnected us and we're trying + # to reconnect. This is because the read thread may either be + # locked also trying to reconnect or we may be said thread already. + call_receive = \ + self._recv_thread is None or self._connect_lock.locked() + return super().invoke( request, call_receive=call_receive, retries=kwargs.get('retries', 5)