From 0c868065c7fd10d806c3e6287095c469c8296b89 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Tue, 25 Oct 2022 12:28:40 +0200 Subject: [PATCH] Handle ConnectionError during update handling --- telethon/client/updates.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/telethon/client/updates.py b/telethon/client/updates.py index ac206797..5d437e81 100644 --- a/telethon/client/updates.py +++ b/telethon/client/updates.py @@ -290,6 +290,13 @@ class UpdateMethods: self._log[__name__].info('Cannot get difference since the account is not logged in: %s', type(e).__name__) self._message_box.end_difference() continue + except OSError as e: + # Network is likely down, but it's unclear for how long. + # If disconnect is called this task will be cancelled along with the sleep. + # If disconnect is not called, getting difference should be retried after a few seconds. + self._log[__name__].info('Cannot get difference since the network is down: %s: %s', type(e).__name__, e) + await asyncio.sleep(5) + continue updates, users, chats = self._message_box.apply_difference(diff, self._mb_entity_cache) if updates: self._log[__name__].info('Got difference for account updates') @@ -351,6 +358,13 @@ class UpdateMethods: self._mb_entity_cache ) continue + except OSError as e: + self._log[__name__].info( + 'Cannot get difference for channel %d since the network is down: %s: %s', + get_diff.channel.channel_id, type(e).__name__, e + ) + await asyncio.sleep(5) + continue updates, users, chats = self._message_box.apply_channel_difference(get_diff, diff, self._mb_entity_cache) if updates: