Skip to content

fix: handle ValueError when resolving unknown entities during backfill#1037

Open
AkshayRao27 wants to merge 2 commits intomautrix:masterfrom
AkshayRao27:master
Open

fix: handle ValueError when resolving unknown entities during backfill#1037
AkshayRao27 wants to merge 2 commits intomautrix:masterfrom
AkshayRao27:master

Conversation

@AkshayRao27
Copy link

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() and update_info() calls in a try/except block 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.

  • Bridge Version: 0.15.3
  • Python Version 3.11.13

Here's what the original Errors looked like:

ValueError: Could not find the input entity for PeerUser(user_id=204010804) (PeerUser). Please read https://docs.telethon.dev/en/stable/concepts/entities.html to find out more details.

Unhandled error while handling command:
Traceback (most recent call last):
  File "/usr/lib/python3.11/site-packages/mautrix/bridge/commands/handler.py", line 491, in handle
    await self._run_handler(handler, evt)
  File "/usr/lib/python3.11/site-packages/mautrix_telegram/commands/handler.py", line 194, in _run_handler
    return await handler(evt)
           ^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mautrix/bridge/commands/handler.py", line 340, in __call__
    return await self._handler(evt)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mautrix_telegram/commands/telegram/misc.py", line 515, in backfill
    output = await portal.forward_backfill(evt.sender, initial=False, override_limit=limit)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mautrix_telegram/portal.py", line 2907, in forward_backfill
    output = await asyncio.wait_for(task, timeout=timeout)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/asyncio/tasks.py", line 489, in wait_for
    return fut.result()
           ^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mautrix_telegram/portal.py", line 2925, in backfill
    return await self._locked_backfill(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mautrix_telegram/portal.py", line 2975, in _locked_backfill
    event_count, message_count, lowest_id = await self._backfill_messages(
                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mautrix_telegram/portal.py", line 3107, in _backfill_messages
    converted, intent = await self._convert_batch_msg(source, client, msg)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mautrix_telegram/portal.py", line 3015, in _convert_batch_msg
    entity = await client.get_entity(sender.peer)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/telethon/client/users.py", line 309, in get_entity
    inputs.append(await self.get_input_entity(x))
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/telethon/client/users.py", line 485, in get_input_entity
    raise ValueError(
ValueError: Could not find the input entity for PeerUser(user_id=204010804) (PeerUser). Please read https://docs.telethon.dev/en/stable/concepts/entities.html to find out more details.

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.
Copilot AI review requested due to automatic review settings March 3, 2026 09:19
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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() with try/except ValueError to 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants