LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!
https://docs.telethon.dev
MIT License
9.68k stars 1.38k forks source link

DateEmptyError raised in _update_loop #3974

Closed JaskaranSM closed 1 year ago

JaskaranSM commented 1 year ago

Checklist

Code that causes the issue

from telethon.sync import TelegramClient as Client
client = Client(str(config.session_name), config.api_id, config.api_hash)
await client.run_until_disconnected()

Traceback

Traceback (most recent call last):
  File "code.py", line 1, in <code>
userbot  | [E 221113 15:16:30 updates:384] Fatal error handling updates (this is a bug in Telethon, please report it)
userbot  |     Traceback (most recent call last):
userbot  |       File "/usr/local/lib/python3.8/dist-packages/telethon/client/updates.py", line 282, in _update_loop
userbot  |         diff = await self(get_diff)
userbot  |       File "/usr/local/lib/python3.8/dist-packages/telethon/client/users.py", line 30, in __call__
userbot  |         return await self._call(self._sender, request, ordered=ordered)
userbot  |       File "/usr/local/lib/python3.8/dist-packages/telethon/client/users.py", line 84, in _call
userbot  |         result = await future
userbot  |     telethon.errors.rpcerrorlist.DateEmptyError: Date empty (caused by GetDifferenceRequest)

Reporting the bug here as the traceback suggested to do.

Lonami commented 1 year ago

Can you reliably trigger this error? If the answer is yes and you add a print(self.date) before returning in:

https://github.com/LonamiWebs/Telethon/blob/2c85ffea1210c602a15776374656adea95dcd7a3/telethon/_updates/messagebox.py#L534-L547

What does it show?

JaskaranSM commented 1 year ago

Can you reliably trigger this error? If the answer is yes and you add a print(self.date) before returning in:

https://github.com/LonamiWebs/Telethon/blob/2c85ffea1210c602a15776374656adea95dcd7a3/telethon/_updates/messagebox.py#L534-L547

What does it show? added:

def get_difference(self):
print(f"Getting difference with date={self.date} for={self.getting_diff_for}")

traceback:

Getting difference with date=1970-01-01 00:00:00+00:00 for={<object object at 0x000001A2363C6280>}
[E 221113 10:37:32 updates:384] Fatal error handling updates (this is a bug in Telethon, please report it)
Traceback (most recent call last):
File "C:\Users\jaska\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\client\updates.py", line 282, in _update_loop
diff = await self(get_diff)
File "C:\Users\jaska\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\client\users.py", line 30, in __call__
return await self._call(self._sender, request, ordered=ordered)
File "C:\Users\jaska\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\client\users.py", line 84, in _call
result = await future
telethon.errors.rpcerrorlist.DateEmptyError: Date empty (caused by GetDifferenceRequest)
Lonami commented 1 year ago

It is strange but the date is indeed 0. It should have been initialized when you logged in but for some reason it was not. If you print((await client(functions.updates.GetStateRequest())).stringify()), is the date the same as above?

Lonami commented 1 year ago

The library could pretend there was no gap when attempting to get difference and the date is not known yet. A naive implementation might get in a loop however so perhaps a different fix would be needed. If you login again the error should disappear though (but I'd still like to fix this).

JaskaranSM commented 1 year ago

No, its not State( pts=46220, qts=0, date=datetime.datetime(2022, 11, 13, 17, 44, 54, tzinfo=datetime.timezone.utc), seq=311, unread_count=44 )

Lonami commented 1 year ago

I cannot reproduce this issue myself. Please install master with:

pip3 install -U https://github.com/LonamiWebs/Telethon/archive/v1.zip

and configure logging with something like this:

import logging
logging.basicConfig(format='[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s', level=logging.WARNING)
logging.getLogger('telethon.messagebox').setLevel(logging.NOTSET + 1)

(You can configure it to your liking, but the messagebox level must be as low as possible. If you have multiple TelegramClient, please set a different base_logger='name' for each in their constructor).

This will print all updates being processed along with the current state of the MessageBox (which deals with the updates).

This log will contain sensitive information, so only share it with people you trust (or redact it before sharing it). You can send it to me privately so that I and nobody else can analyze it (please link to this issue, so I know you're not spam).

With it I'll be able to reproduce the issue reliably locally, pinpoint the problem, and fix it.

Lonami commented 1 year ago

Closing due to inactivity.