mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-01-24 16:24:15 +03:00
Only reconnect from background thread if .disconnect wasn't called
This commit is contained in:
parent
626778bd32
commit
27408b46da
|
@ -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()
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user