Skip to content

Commit

Permalink
Re-raise unhandled errors that occur during update handling
Browse files Browse the repository at this point in the history
This should help the situation in #3870.
  • Loading branch information
Lonami committed Sep 21, 2022
1 parent a83fe46 commit 49bdb76
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
1 change: 1 addition & 0 deletions telethon/client/telegrambaseclient.py
Expand Up @@ -386,6 +386,7 @@ def __missing__(self, key):
self._borrowed_senders = {}
self._borrow_sender_lock = asyncio.Lock()

self._updates_error = None
self._updates_handle = None
self._keepalive_handle = None
self._last_request = time.time()
Expand Down
12 changes: 10 additions & 2 deletions telethon/client/updates.py
Expand Up @@ -28,7 +28,10 @@ async def _run_until_disconnected(self: 'TelegramClient'):
try:
# Make a high-level request to notify that we want updates
await self(functions.updates.GetStateRequest())
return await self.disconnected
result = await self.disconnected
if self._updates_error is not None:
raise self._updates_error
return result
except KeyboardInterrupt:
pass
finally:
Expand All @@ -51,6 +54,8 @@ def run_until_disconnected(self: 'TelegramClient'):
It also notifies Telegram that we want to receive updates
as described in https://core.telegram.org/api/updates.
If an unexpected error occurs during update handling,
the client will disconnect and said error will be raised.
Manual disconnections can be made by calling `disconnect()
<telethon.client.telegrambaseclient.TelegramBaseClient.disconnect>`
Expand Down Expand Up @@ -246,6 +251,7 @@ async def catch_up(self: 'TelegramClient'):
# region Private methods

async def _update_loop(self: 'TelegramClient'):
self._updates_error = None
try:
if self._catch_up:
# User wants to catch up as soon as the client is up and running,
Expand Down Expand Up @@ -360,8 +366,10 @@ async def _update_loop(self: 'TelegramClient'):
updates_to_dispatch.extend(self._preprocess_updates(processed, users, chats))
except asyncio.CancelledError:
pass
except Exception:
except Exception as e:
self._log[__name__].exception('Fatal error handling updates (this is a bug in Telethon, please report it)')
self._updates_error = e
await self.disconnect()

def _preprocess_updates(self, updates, users, chats):
self._mb_entity_cache.extend(users, chats)
Expand Down

0 comments on commit 49bdb76

Please sign in to comment.