From 57611a3f303ddc9a9a5e98ae3c5e9690ecc001d7 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 13 Aug 2021 12:58:35 +0300 Subject: [PATCH] Catch AuthKeyDuplicatedError in start() --- mautrix_telegram/user.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/mautrix_telegram/user.py b/mautrix_telegram/user.py index 96c6c448..bb643e15 100644 --- a/mautrix_telegram/user.py +++ b/mautrix_telegram/user.py @@ -27,6 +27,7 @@ from telethon.tl.custom import Dialog from telethon.tl.types.contacts import ContactsNotModified from telethon.tl.functions.contacts import GetContactsRequest, SearchRequest from telethon.tl.functions.account import UpdateStatusRequest +from telethon.errors import AuthKeyDuplicatedError from mautrix.client import Client from mautrix.errors import MatrixRequestError, MNotFound @@ -54,7 +55,7 @@ METRIC_CONNECTED = Gauge('bridge_connected', 'Users connected to Telegram') BridgeState.human_readable_errors.update({ "tg-not-connected": "Your Telegram connection failed", - "logged-out": "You're not logged into Telegram", + "tg-auth-key-duplicated": "The bridge accidentally logged you out", }) @@ -203,7 +204,16 @@ class User(AbstractUser, BaseUser): return cast(User, await super().ensure_started(even_if_no_session)) async def start(self, delete_unless_authenticated: bool = False) -> 'User': - await super().start() + try: + await super().start() + except AuthKeyDuplicatedError: + self.log.warning("Got AuthKeyDuplicatedError in start()") + await self.push_bridge_state(BridgeStateEvent.BAD_CREDENTIALS, + error="tg-auth-key-duplicated") + # Don't return, let the session be deleted if delete_unless_authenticated is true + except Exception: + await self.push_bridge_state(BridgeStateEvent.UNKNOWN_ERROR) + raise if await self.is_logged_in(): self.log.debug(f"Ensuring post_login() for {self.name}") self.loop.create_task(self.post_login()) @@ -392,12 +402,6 @@ class User(AbstractUser, BaseUser): return await self._search_remote(query), True - async def _catch(self, action: str, task: asyncio.Task) -> None: - try: - await task - except Exception: - self.log.exception(f"Error while {action}") - async def get_direct_chats(self) -> Dict[UserID, List[RoomID]]: return { pu.Puppet.get_mxid_from_id(portal.tgid): [portal.mxid]