Only reconnect from background thread if .disconnect wasn't called

This commit is contained in:
Lonami Exo 2017-09-03 13:44:52 +02:00
parent 626778bd32
commit 27408b46da

View File

@ -155,12 +155,15 @@ class TelegramClient(TelegramBareClient):
if not self._sender or not self._sender.is_connected():
return
super().disconnect()
# The existing thread will close eventually, since it's
# only running while the MtProtoSender.is_connected()
self._recv_thread = None
# This will trigger a "ConnectionResetError", usually, the background
# thread would try restarting the connection but since the
# ._recv_thread = None, it knows it doesn't have to.
super().disconnect()
# Also disconnect all the cached senders
for sender in self._cached_clients.values():
sender.disconnect()
@ -911,13 +914,16 @@ class TelegramClient(TelegramBareClient):
#
# This way, sending and receiving will be completely independent.
def _recv_thread_impl(self):
while self._sender.is_connected():
while self._sender and self._sender.is_connected():
try:
self._sender.receive()
except TimeoutError:
# No problem.
pass
except ConnectionResetError:
if self._recv_thread is not None:
# Do NOT attempt reconnecting unless the connection was
# finished by the user -> ._recv_thread is None
self._logger.debug('Server disconnected us. Reconnecting...')
self.reconnect()