From 6f1c05633e242bbd458a2f471ffa4c190f1f46d4 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Mon, 9 Oct 2017 11:47:10 +0200 Subject: [PATCH] Join all threads when calling .disconnect() (fix #252) --- telethon/telegram_bare_client.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/telethon/telegram_bare_client.py b/telethon/telegram_bare_client.py index 2447a67d..bc56fd42 100644 --- a/telethon/telegram_bare_client.py +++ b/telethon/telegram_bare_client.py @@ -271,18 +271,16 @@ class TelegramBareClient: def disconnect(self): """Disconnects from the Telegram server and stops all the spawned threads""" - self._user_connected = False - self._recv_thread = None - - # Stop the workers from the background thread + self._user_connected = False # This will stop recv_thread's loop self.updates.stop_workers() - # This will trigger a "ConnectionResetError", for subsequent calls - # to read or send (from another thread) and usually, the background - # thread would try restarting the connection but since the - # ._recv_thread = None, it knows it doesn't have to. + # This will trigger a "ConnectionResetError" on the recv_thread, + # which won't attempt reconnecting as ._user_connected is False. self._sender.disconnect() + if self._recv_thread: + self._recv_thread.join() + # TODO Shall we clear the _exported_sessions, or may be reused? pass