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)."""