From fb5c43b5396c89e96960d5e7c206916382ea89d8 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Tue, 22 Aug 2017 23:17:05 +0200 Subject: [PATCH] Ensure .connect() always returns True/False --- telethon/telegram_bare_client.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/telethon/telegram_bare_client.py b/telethon/telegram_bare_client.py index 8945d6f5..51293de6 100644 --- a/telethon/telegram_bare_client.py +++ b/telethon/telegram_bare_client.py @@ -4,6 +4,8 @@ from hashlib import md5 from os import path # Import some externalized utilities to work with the Telegram types and more +from telethon.tl.functions import PingRequest + from . import helpers as utils from .errors import ( RPCError, FloodWaitError, FileMigrateError, TypeNotFoundError @@ -92,10 +94,14 @@ class TelegramBareClient: determine the authorization key for the current session. """ if self._sender and self._sender.is_connected(): - self._logger.debug( - 'Attempted to connect when the client was already connected.' - ) - return + # Try sending a ping to make sure we're connected already + # TODO Maybe there's a better way to check this + try: + self(PingRequest(utils.generate_random_long())) + return True + except: + # If ping failed, ensure we're disconnected first + self.disconnect() transport = TcpTransport(self.session.server_address, self.session.port, @@ -146,7 +152,7 @@ class TelegramBareClient: # This is fine, probably layer migration self._logger.debug('Found invalid item, probably migrating', e) self.disconnect() - self.connect(exported_auth=exported_auth) + return self.connect(exported_auth=exported_auth) except (RPCError, ConnectionError) as error: # Probably errors from the previous session, ignore them