Telethon hangs when using a corrupt session file without errors or timeout
Code that causes the issue
If we use telethon with a corrupt session file, the script hangs indefinitely without throwing an error:
from telethon.sync import TelegramClient
with TelegramClient("12252174310.session", api_id=..., api_hash=...) as client:
client.send_message("me", "hi")
print("message sent!") # never
here is session file: session.zip
Expected behavior
Telethon should detect that the session is invalid and either:
- Raise an error, tell the user that the session is corrupt.
- Timeout after a certain period, preventing indefinite hanging.
Actual behavior
The script hangs indefinitely and does not raise any errors.
Traceback
Ctrl+C shows destroyed but pending tasks.
^CTraceback (most recent call last):
File "/root/test-telethon/main.py", line 3, in <module>
with TelegramClient(
File "/root/test-telethon/.venv/lib/python3.11/site-packages/telethon/helpers.py", line 219, in _sync_enter
return loop.run_until_complete(self.__aenter__())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/.local/share/uv/python/cpython-3.11.9-linux-x86_64-gnu/lib/python3.11/asyncio/base_events.py", line 641, in run_until_complete
self.run_forever()
File "/root/.local/share/uv/python/cpython-3.11.9-linux-x86_64-gnu/lib/python3.11/asyncio/base_events.py", line 608, in run_forever
self._run_once()
File "/root/.local/share/uv/python/cpython-3.11.9-linux-x86_64-gnu/lib/python3.11/asyncio/base_events.py", line 1898, in _run_once
event_list = self._selector.select(timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/.local/share/uv/python/cpython-3.11.9-linux-x86_64-gnu/lib/python3.11/selectors.py", line 468, in select
fd_event_list = self._selector.poll(timeout, max_ev)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyboardInterrupt
Task was destroyed but it is pending!
task: <Task pending name='Task-5' coro=<MTProtoSender._send_loop() running at /root/test-telethon/.venv/lib/python3.11/site-packages/telethon/network/mtprotosender.py:464> wait_for=<Future pending cb=[Task.task_wakeup()]>>
Task was destroyed but it is pending!
task: <Task pending name='Task-6' coro=<MTProtoSender._recv_loop() running at /root/test-telethon/.venv/lib/python3.11/site-packages/telethon/network/mtprotosender.py:507> wait_for=<Future pending cb=[Task.task_wakeup()]>>
Task was destroyed but it is pending!
task: <Task pending name='Task-3' coro=<Connection._send_loop() running at /root/test-telethon/.venv/lib/python3.11/site-packages/telethon/network/connection/connection.py:322> wait_for=<Future pending cb=[Task.task_wakeup()]>>
Task was destroyed but it is pending!
task: <Task pending name='Task-4' coro=<Connection._recv_loop() running at /root/test-telethon/.venv/lib/python3.11/site-packages/telethon/network/connection/connection.py:341> wait_for=<Future pending cb=[Task.task_wakeup()]>>
Exception ignored in: <coroutine object Connection._recv_loop at 0x7debac2256c0>
RuntimeError: coroutine ignored GeneratorExit
Telethon version
1.39.0
Python version
3.11.9
Operating system (including distribution name and version)
Ubuntu 24.04.1 LTS
Other details
No response
Checklist
- [x] The error is in the library's code, and not in my own.
- [x] I have searched for this issue before posting it and there isn't an open duplicate.
- [x] I ran
pip install -U https://github.com/LonamiWebs/Telethon/archive/v1.zipand triggered the bug in the latest version.
I've no plans to look into this. If wrapping start with a timeout is not enough for you, someone will need to look into it and propose a fix.
I just realized this issue isn't limited to corrupt sessions. It also occurs when the internet is unavailable and we try to start a client, making it more of a connection problem.
As a temporary fix, I used this workaround:
import asyncio
from telethon import TelegramClient
class Client(TelegramClient):
async def __aenter__(self):
return await asyncio.wait_for(super().__aenter__(), self._timeout)