From 12b84c929a5906b728c34dff0515d6b15bd1a5be Mon Sep 17 00:00:00 2001 From: Tural Date: Mon, 21 Aug 2017 09:00:23 +0200 Subject: [PATCH] Fix log_out problems on macOS devices --- telethon/telegram_client.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/telethon/telegram_client.py b/telethon/telegram_client.py index bae29e49..249d2a7b 100644 --- a/telethon/telegram_client.py +++ b/telethon/telegram_client.py @@ -1,3 +1,4 @@ +import errno from datetime import timedelta from mimetypes import guess_type from threading import Event, RLock, Thread @@ -315,22 +316,27 @@ class TelegramClient(TelegramBareClient): def log_out(self): """Logs out and deletes the current session. Returns True if everything went okay.""" - # Special flag when logging out (so the ack request confirms it) self._sender.logging_out = True + try: self(LogOutRequest()) self.disconnect() - if not self.session.delete(): - return False - - self.session = None - return True + except OSError as e: + # macOS issue: https://github.com/veusz/veusz/issues/54 + # Socket has been already closed (Errno 57) + # Fail on any other error + if e.errno != errno.ENOTCONN: + raise except (RPCError, ConnectionError): # Something happened when logging out, restore the state back self._sender.logging_out = False return False + self.session.delete() + self.session = None + return True + def get_me(self): """Gets "me" (the self user) which is currently authenticated, or None if the request fails (hence, not authenticated)."""