diff --git a/telethon/_updates/messagebox.py b/telethon/_updates/messagebox.py index 4f17d4bb..47416d3b 100644 --- a/telethon/_updates/messagebox.py +++ b/telethon/_updates/messagebox.py @@ -611,6 +611,19 @@ class MessageBox: return updates, diff.users, diff.chats + def end_difference(self): + account = ENTRY_ACCOUNT in self.getting_diff_for + secret = ENTRY_SECRET in self.getting_diff_for + + if not account and not secret: + raise RuntimeWarning('Should not be ending get difference when neither account or secret was diff was active') + + # Both may be active if both expired at the same time. + if account: + self.end_get_diff(ENTRY_ACCOUNT) + if secret: + self.end_get_diff(ENTRY_SECRET) + # endregion Getting and applying account difference. # region Getting and applying channel difference. diff --git a/telethon/client/updates.py b/telethon/client/updates.py index 735f45aa..05eb052e 100644 --- a/telethon/client/updates.py +++ b/telethon/client/updates.py @@ -270,7 +270,13 @@ class UpdateMethods: get_diff = self._message_box.get_difference() if get_diff: self._log[__name__].info('Getting difference for account updates') - diff = await self(get_diff) + try: + diff = await self(get_diff) + except (errors.UnauthorizedError, errors.AuthKeyError) as e: + # Not logged in or broken authorization key, can't get difference + self._log[__name__].info('Cannot get difference since the account is not logged in: %s', type(e).__name__) + self._message_box.end_difference() + continue updates, users, chats = self._message_box.apply_difference(diff, self._mb_entity_cache) updates_to_dispatch.extend(self._preprocess_updates(updates, users, chats)) continue @@ -280,7 +286,13 @@ class UpdateMethods: self._log[__name__].info('Getting difference for channel updates') try: diff = await self(get_diff) - except (errors.PersistentTimestampOutdatedError, errors.PersistentTimestampInvalidError, ValueError) as e: + except ( + errors.PersistentTimestampOutdatedError, + errors.PersistentTimestampInvalidError, + errors.UnauthorizedError, + errors.AuthKeyError, + ValueError + ) as e: # According to Telegram's docs: # "Channel internal replication issues, try again later (treat this like an RPC_CALL_FAIL)." # We can treat this as "empty difference" and not update the local pts. @@ -297,15 +309,10 @@ class UpdateMethods: # Somehow our pts is either too new or the server does not know about this. # We treat this as PersistentTimestampOutdatedError for now. # TODO investigate why/when this happens and if this is the proper solution - if isinstance(e, errors.PersistentTimestampOutdatedError): - reason = 'caused PersistentTimestampOutdated' - elif isinstance(e, errors.PersistentTimestampInvalidError): - reason = 'caused PersistentTimestampInvalidError' - else: - reason = 'is failing' self._log[__name__].warning( - 'Getting difference for channel updates %s;' - ' ending getting difference prematurely until server issues are resolved', reason + 'Getting difference for channel updates caused %s;' + ' ending getting difference prematurely until server issues are resolved', + type(e).__name__ ) self._message_box.end_channel_difference( get_diff,