From a38381665cb00bae93f801d7f1adeb8732dd60e8 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Mon, 4 Sep 2017 20:28:41 +0200 Subject: [PATCH] Support invoking requests from within the ReadThread (fixes #226) --- telethon/telegram_client.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/telethon/telegram_client.py b/telethon/telegram_client.py index 37e9b088..04f6bd82 100644 --- a/telethon/telegram_client.py +++ b/telethon/telegram_client.py @@ -2,6 +2,7 @@ import os from datetime import datetime, timedelta from mimetypes import guess_type from threading import RLock, Thread +import threading from . import TelegramBareClient from . import helpers as utils @@ -207,10 +208,16 @@ class TelegramClient(TelegramBareClient): try: self._lock.acquire() + # 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 = self._recv_thread is None or \ + threading.get_ident() == self._recv_thread.ident + # TODO Retry if 'result' is None? - return super().invoke( - request, call_receive=self._recv_thread is None - ) + return super().invoke(request, call_receive=call_receive) except (PhoneMigrateError, NetworkMigrateError, UserMigrateError) as e: self._logger.debug('DC error when invoking request, '