mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-01-25 00:34:19 +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():
|
if not self._sender or not self._sender.is_connected():
|
||||||
return
|
return
|
||||||
|
|
||||||
super().disconnect()
|
|
||||||
|
|
||||||
# The existing thread will close eventually, since it's
|
# The existing thread will close eventually, since it's
|
||||||
# only running while the MtProtoSender.is_connected()
|
# only running while the MtProtoSender.is_connected()
|
||||||
self._recv_thread = None
|
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
|
# Also disconnect all the cached senders
|
||||||
for sender in self._cached_clients.values():
|
for sender in self._cached_clients.values():
|
||||||
sender.disconnect()
|
sender.disconnect()
|
||||||
|
@ -911,13 +914,16 @@ class TelegramClient(TelegramBareClient):
|
||||||
#
|
#
|
||||||
# This way, sending and receiving will be completely independent.
|
# This way, sending and receiving will be completely independent.
|
||||||
def _recv_thread_impl(self):
|
def _recv_thread_impl(self):
|
||||||
while self._sender.is_connected():
|
while self._sender and self._sender.is_connected():
|
||||||
try:
|
try:
|
||||||
self._sender.receive()
|
self._sender.receive()
|
||||||
except TimeoutError:
|
except TimeoutError:
|
||||||
# No problem.
|
# No problem.
|
||||||
pass
|
pass
|
||||||
except ConnectionResetError:
|
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._logger.debug('Server disconnected us. Reconnecting...')
|
||||||
self.reconnect()
|
self.reconnect()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user