Telethon: InvalidBufferError: HTTP code 429 handling

Hi, We are using this library to control user interactions with our Telegram bot. Usually, we have no problems with Telegram. Still, lately, we get at night a vast count of InvalidBufferErrors (Invalid buffer Invalid response buffer (HTTP code 429)). This error occurs so often that our bot is just not available at night. The image below shows the log spam due to this error: image We looked at the library code and saw that it would just reconnect when this error occurs, which is a suboptimal way of handling it in our eyes and use-case. We are more than baffled because this error occurs at a specific time nearly every day, and it only affects our bot code which uses only one session file. Due to this behaviour, we suggest the following changes: https://github.com/CptKirk/Telethon/commit/59d8bb03626158b94c977fec1fcfcc628e29f5e0 We suggest cancelling the send loop, waiting for x seconds and then restarting it when encountering a 429 InvalidBufferError. Does anyone else have a better solution? Nonetheless, we will probably deploy these changes on Monday (27.06) to our production environment. We will report the days later if waiting fixes something.

Telethon version: 1.24.0 Python Version: 3.9

Checklist

  • The error is in the library’s code, and not in my own.
  • I have searched for this issue before posting it and there isn’t a duplicate. (rel: #3825 )
  • I ran pip install -U https://github.com/LonamiWebs/Telethon/archive/master.zip and triggered the bug in the latest version.

Traceback

Traceback (most recent call last):
File "/home/projects/our_project/code/bot.py", line 140, in <module>
    loop.run_forever()
File "/usr/lib64/python3.9/asyncio/base_events.py", line 596, in run_forever
    self._run_once()
File "/usr/lib64/python3.9/asyncio/base_events.py", line 1890, in _run_once
    handle._run()
File "/usr/lib64/python3.9/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
File "/home/projects/our_project/venv/lib64/python3.9/site-packages/telethon/network/mtprotosender.py", line 531, in _recv_loop
    self._log.warning('Invalid buffer %s', e)

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 6
  • Comments: 19 (6 by maintainers)

Most upvoted comments

Yes, we can start by marking all in-flight request states to the exception status and see how it goes. Because I don’t remember experiencing this, I can’t really test this in a real scenario

Ok, I will start implementing such thing at next week probably, it will also take some time to test the code

Yes, we can start by marking all in-flight request states to the exception status and see how it goes. Because I don’t remember experiencing this, I can’t really test this in a real scenario

Ok, I will start implementing such thing at next week probably, it will also take some time to test the code

Hi, is there any news?

So… finally, we have something to report. We deployed the changes as said on Monday (27.06) and have been waiting for the InvalidBufferError. Strangely enough, the first two nights, there were no errors. “Thankfully”, this night, it happened again, and our fix seems to remedy the problem. As you can see in the picture below, the spam was much less: image The logging output was the following: image The solution is to cancel the send_loop for 10 seconds when this error occurs. Are those changes viable for a PR? If not, tell me what to change, and I will implement it.