fix: handle ValueError when resolving unknown entities during backfill#1037
Open
AkshayRao27 wants to merge 2 commits intomautrix:masterfrom
Open
fix: handle ValueError when resolving unknown entities during backfill#1037AkshayRao27 wants to merge 2 commits intomautrix:masterfrom
AkshayRao27 wants to merge 2 commits intomautrix:masterfrom
Conversation
During backfill, get_entity() can fail with a ValueError for users that are not in Telethon's local entity cache (e.g. deleted accounts, users the session has never directly encountered). This caused the entire backfill to abort. I wrapped the `get_entity()` and `update_info()` calls in a `try`/`except` block so that messages from unresolvable senders are still bridged, just without a refreshed display name for that sender. This way, history is still pulled, even if some of the entity names are wonky.
There was a problem hiding this comment.
Pull request overview
Improves reliability of Telegram message backfill by preventing missing/unknown Telegram entities from aborting the entire backfill process.
Changes:
- Wrapes
client.get_entity(sender.peer)+sender.update_info(...)in_convert_batch_msg()withtry/except ValueErrorto continue bridging when entity resolution fails. - Adds a warning log when an entity can’t be resolved.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…c_info Addresses two issues with the initial ValueError fix: 1. Moved `update_info()` to the `else` block so it only runs if `get_entity()` succeeds, preventing unrelated ValueErrors from being silently swallowed. 2. Added `exc_info=True` to the warning log so the full exception is captured for debugging. 3. Added a `_unresolvable_senders` cache so repeated messages from the same unresolvable sender don't trigger redundant `get_entity()` calls and log spam during large backfills.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
During backfill,
get_entity()can fail with a ValueError. This has been causing entire backfills to abort. My best guess is that it occurs when backfilling messages from users whose accounts have been deleted, users that the Telegram session has never directly encountered and therefore has no cached entity data for, or something of the sort.I wrapped the
get_entity()andupdate_info()calls in atry/exceptblock so that messages from unresolvable senders are still bridged, just without a display name for that sender. This way, my message history is still backfilled even if some of the names are wonky.Since making this change, I've managed to backfill several Telegram chats on my self-hosted Matrix server, some with tens of thousands of messages without the backfill failing.
Here's what the original Errors looked like: