Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERROR:telethon.network.connection.connection:Unexpected exception in the receive loop #4092

Open
Samjith888 opened this issue Apr 18, 2023 · 15 comments

Comments

@Samjith888
Copy link

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 a duplicate.
  • [X ] I ran pip install -U https://github.com/LonamiWebs/Telethon/archive/v1.zip and triggered the bug in the latest version.

Traceback

ERROR:telethon.network.connection.connection:Unexpected exception in the receive loop
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/telethon/network/connection/connection.py", line 332, in _recv_loop
    data = await self._recv()
  File "/usr/local/lib/python3.6/dist-packages/telethon/network/connection/connection.py", line 369, in _recv
    return await self._codec.read_packet(self._reader)
  File "/usr/local/lib/python3.6/dist-packages/telethon/network/connection/tcpfull.py", line 34, in read_packet
    body = await reader.readexactly(packet_len - 8)
  File "/usr/lib/python3.6/asyncio/streams.py", line 660, in readexactly
    raise ValueError('readexactly size can not be less than zero')
ValueError: readexactly size can not be less than zero
ERROR:telethon.network.mtprotosender:Server sent invalid buffer
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/telethon/network/mtprotosender.py", line 505, in _recv_loop
    body = await self._connection.recv()
  File "/usr/local/lib/python3.6/dist-packages/telethon/network/connection/connection.py", line 301, in recv
    raise err
  File "/usr/local/lib/python3.6/dist-packages/telethon/network/connection/connection.py", line 332, in _recv_loop
    data = await self._recv()
  File "/usr/local/lib/python3.6/dist-packages/telethon/network/connection/connection.py", line 369, in _recv
    return await self._codec.read_packet(self._reader)
  File "/usr/local/lib/python3.6/dist-packages/telethon/network/connection/tcpfull.py", line 32, in read_packet
    raise InvalidBufferError(body)
telethon.errors.common.InvalidBufferError: Invalid response buffer (HTTP code -1907221142)
ERROR:telethon.network.connection.connection:Unexpected exception in the receive loop
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/telethon/network/connection/connection.py", line 332, in _recv_loop
    data = await self._recv()
  File "/usr/local/lib/python3.6/dist-packages/telethon/network/connection/connection.py", line 369, in _recv
    return await self._codec.read_packet(self._reader)
  File "/usr/local/lib/python3.6/dist-packages/telethon/network/connection/tcpfull.py", line 34, in read_packet
    body = await reader.readexactly(packet_len - 8)
  File "/usr/lib/python3.6/asyncio/streams.py", line 660, in readexactly
    raise ValueError('readexactly size can not be less than zero')
ValueError: readexactly size can not be less than zero
ERROR:telethon.network.mtprotosender:Unhandled error while receiving data
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/telethon/network/mtprotosender.py", line 505, in _recv_loop
    body = await self._connection.recv()
  File "/usr/local/lib/python3.6/dist-packages/telethon/network/connection/connection.py", line 301, in recv
    raise err
  File "/usr/local/lib/python3.6/dist-packages/telethon/network/connection/connection.py", line 332, in _recv_loop
    data = await self._recv()
  File "/usr/local/lib/python3.6/dist-packages/telethon/network/connection/connection.py", line 369, in _recv
    return await self._codec.read_packet(self._reader)
  File "/usr/local/lib/python3.6/dist-packages/telethon/network/connection/tcpfull.py", line 34, in read_packet
    body = await reader.readexactly(packet_len - 8)
  File "/usr/lib/python3.6/asyncio/streams.py", line 660, in readexactly
    raise ValueError('readexactly size can not be less than zero')
ValueError: readexactly size can not be less than zero
ERROR:telethon.network.connection.connection:Unexpected exception in the receive loop
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/telethon/network/connection/connection.py", line 332, in _recv_loop
    data = await self._recv()
  File "/usr/local/lib/python3.6/dist-packages/telethon/network/connection/connection.py", line 369, in _recv
    return await self._codec.read_packet(self._reader)
  File "/usr/local/lib/python3.6/dist-packages/telethon/network/connection/tcpfull.py", line 25, in read_packet
    packet_len_seq = await reader.readexactly(8)  # 4 and 4
  File "/usr/lib/python3.6/asyncio/streams.py", line 674, in readexactly
    yield from self._wait_for_data('readexactly')
  File "/usr/lib/python3.6/asyncio/streams.py", line 464, in _wait_for_data
    yield from self._waiter
concurrent.futures._base.CancelledError


@Lonami
Copy link
Member

Lonami commented Apr 18, 2023

You need to provide the necessary code, preferably under 100 lines long, and steps needed to reproduce the issue.

@superarray
Copy link

i have same error
my system : windows 10
Python version: Python 3.7.9

client = TelegramClient(........)
await client.connect()
print(await client.get_me())

####################################################

ERROR:telethon.network.connection.connection:Unexpected exception in the receive loop
Traceback (most recent call last):
  File "D:\Python379\lib\site-packages\telethon\network\connection\connection.py", line 332, in _recv_loop
    data = await self._recv()
  File "D:\Python379\lib\site-packages\telethon\network\connection\connection.py", line 369, in _recv
    return await self._codec.read_packet(self._reader)
  File "D:\Python379\lib\site-packages\telethon\network\connection\tcpfull.py", line 25, in read_packet
    packet_len_seq = await reader.readexactly(8)  # 4 and 4
  File "D:\Python379\lib\asyncio\streams.py", line 679, in readexactly
    await self._wait_for_data('readexactly')
  File "D:\Python379\lib\asyncio\streams.py", line 473, in _wait_for_data
    await self._waiter
concurrent.futures._base.CancelledError
ERROR:telethon.network.mtprotosender:Unhandled error while receiving data
Traceback (most recent call last):
  File "D:\Python379\lib\site-packages\telethon\network\mtprotosender.py", line 505, in _recv_loop
    body = await self._connection.recv()
  File "D:\Python379\lib\site-packages\telethon\network\connection\connection.py", line 299, in recv
    result, err = await self._recv_queue.get()
  File "D:\Python379\lib\asyncio\queues.py", line 159, in get
    await getter
concurrent.futures._base.CancelledError
INFO:telethon.network.mtprotosender:Closing current connection to begin reconnect...
ERROR:telethon.network.connection.connection:Unexpected exception in the receive loop
Traceback (most recent call last):
  File "D:\Python379\lib\site-packages\telethon\network\connection\connection.py", line 332, in _recv_loop
    data = await self._recv()
  File "D:\Python379\lib\site-packages\telethon\network\connection\connection.py", line 369, in _recv
    return await self._codec.read_packet(self._reader)
  File "D:\Python379\lib\site-packages\telethon\network\connection\tcpfull.py", line 25, in read_packet
    packet_len_seq = await reader.readexactly(8)  # 4 and 4
  File "D:\Python379\lib\asyncio\streams.py", line 679, in readexactly
    await self._wait_for_data('readexactly')
  File "D:\Python379\lib\asyncio\streams.py", line 473, in _wait_for_data
    await self._waiter
concurrent.futures._base.CancelledError```

@Samjith888
Copy link
Author

hi @Lonami While looking at the error Traceback, i couldn't find any error in the code. The errors are from the library!

msg = app.get_messages(id, limit=20)
I have used the above line continuously to monitor to receive a message from a specific ID.
I was using this code from past 1 year without any errors. This issue started from the last two days only!

@caixinminnihao
Copy link

hi @Lonami While looking at the error Traceback, i couldn't find any error in the code. The errors are from the library!

msg = app.get_messages(id, limit=20) I have used the above line continuously to monitor to receive a message from a specific ID. I was using this code from past 1 year without any errors. This issue started from the last two days only!

I also meet this question, and i try several version of telethon all get the same question, it must bu the platform API had changed something.

@Samjith888
Copy link
Author

Hi @Lonami any updates on this issue🙂

@DefSw0rd
Copy link

hi @Lonami While looking at the error Traceback, i couldn't find any error in the code. The errors are from the library!

msg = app.get_messages(id, limit=20) I have used the above line continuously to monitor to receive a message from a specific ID. I was using this code from past 1 year without any errors. This issue started from the last two days only!

meet the same question too(Windows 10 1809 withi Python 3.10.1 amd64,telethon==1.28.5),when I try to reinstall the telethon version below telethon==1.28.0(like 1.27.0,1.24.0 etc.), no error produced.

@Lonami Lonami closed this as completed in 03ff996 Apr 29, 2023
@vomikan
Copy link

vomikan commented May 31, 2023

I've downgraded to 1.27.0. The fix does not help.

Python 3.6.8
Linux ruvds-zm5fa 3.10.0-1160.53.1.el7.x86_64 #1 SMP Fri Jan 14 13:59:45 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
3.10.0-1160.53.1.el7.x86_64
CentOS Linux release 7.9.2009 (Core)
Derived from Red Hat Enterprise Linux 7.9 (Source)

@Lonami
Copy link
Member

Lonami commented Jun 1, 2023

As I've said before I've been unable to reproduce the issue myself so it's hard for me to reliably trigger it and try to diagnose it. Someone that experiences the issue and looks into it would be of great help. If older versions truly "work" and newer ones don't, it would be truly great to git bisect to find where it "broke", because AFAICT, none of my changes should have changed the functionality in the library WRT reconnection. All that should happen is that now some errors are now propagated instead of swallowed.

@vomikan
Copy link

vomikan commented Jun 1, 2023

As I've said before I've been unable to reproduce the issue myself so it's hard for me to reliably trigger it and try to diagnose it. Someone that experiences the issue and looks into it would be of great help. If older versions truly "work" and newer ones don't, it would be truly great to git bisect to find where it "broke", because AFAICT, none of my changes should have changed the functionality in the library WRT reconnection. All that should happen is that now some errors are now propagated instead of swallowed.

How can I help?
Unfortunately, I don't work with Python.
I can provide a VPS with this problem.

@DefSw0rd
Copy link

DefSw0rd commented Jun 1, 2023

I've downgraded to 1.27.0. The fix does not help.

Python 3.6.8 Linux ruvds-zm5fa 3.10.0-1160.53.1.el7.x86_64 #1 SMP Fri Jan 14 13:59:45 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux 3.10.0-1160.53.1.el7.x86_64 CentOS Linux release 7.9.2009 (Core) Derived from Red Hat Enterprise Linux 7.9 (Source)

my program get this error on higher version because of my server's unstable proxy connection(like unstable VPN etc). Its a pitty that I cannot provide any support to you, Sry😢

@Lonami
Copy link
Member

Lonami commented Jun 1, 2023

I can provide a VPS with this problem.

You can message me privately, with a link to this issue so I know you are not spam. Then depending on the setup perhaps I can generate and give you one SSH key so I can login and try to diagnose the problem.

Not sure when I'll get to it though. But I'll keep this open so I remember.

@Lonami Lonami reopened this Jun 1, 2023
@wolf-joe
Copy link

wolf-joe commented Aug 21, 2023

same problem

Unexpected exception in the receive loop
Traceback (most recent call last):
  File "/home/worker/data/WD10_8Z/telegram/venv/lib/python3.8/site-packages/telethon/network/connection/connection.py", line 335, in _recv_loop
    data = await self._recv()
  File "/home/worker/data/WD10_8Z/telegram/venv/lib/python3.8/site-packages/telethon/network/connection/connection.py", line 374, in _recv
    return await self._codec.read_packet(self._reader)
  File "/home/worker/data/WD10_8Z/telegram/venv/lib/python3.8/site-packages/telethon/network/connection/tcpfull.py", line 25, in read_packet
    packet_len_seq = await reader.readexactly(8)  # 4 and 4
  File "/usr/lib/python3.8/asyncio/streams.py", line 723, in readexactly
    await self._wait_for_data('readexactly')
  File "/usr/lib/python3.8/asyncio/streams.py", line 503, in _wait_for_data
    raise RuntimeError(
RuntimeError: readexactly() called while another coroutine is already waiting for incoming data

try fix:

/home/worker/data/WD10_8Z/telegram/venv/lib/python3.8/site-packages/telethon/network/connection/connection.py
   348	                except Exception as e:
   349	                    self._log.exception('Unexpected exception in the receive loop')
   350	                    exit(-1) # exit process to prevent hang forever  <----- add this line

@JavadZahedi
Copy link

JavadZahedi commented Jan 23, 2024

I have same problem. I use proxy to connect to telegram. Sometimes, while downloading large files, following log appeared continuously and I must reset my program manually to get rid of that.

Unhandled error while receiving data
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/telethon/network/mtprotosender.py", line 507, in _recv_loop
    body = await self._connection.recv()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/telethon/network/connection/connection.py", line 304, in recv
    raise err
  File "/usr/local/lib/python3.11/site-packages/telethon/network/connection/connection.py", line 335, in _recv_loop
    data = await self._recv()
           ^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/telethon/network/connection/connection.py", line 374, in _recv
    return await self._codec.read_packet(self._reader)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/telethon/network/connection/tcpfull.py", line 25, in read_packet
    packet_len_seq = await reader.readexactly(8)  # 4 and 4
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/asyncio/streams.py", line 747, in readexactly
    await self._wait_for_data('readexactly')
  File "/usr/local/lib/python3.11/asyncio/streams.py", line 526, in _wait_for_data
    raise RuntimeError(
RuntimeError: readexactly() called while another coroutine is already waiting for incoming data

to fix it, should change telethon/network/mtprotosender.py:524.

current code:

  522          except Exception as e:
  523              self._log.exception('Unhandled error while receiving data')
  524              self._start_reconnect(e)
  525              return

should be:

  522          except Exception as e:
  523              self._log.exception('Unhandled error while receiving data')
  524              raise e

@Lonami

@Lonami
Copy link
Member

Lonami commented Jan 23, 2024

I believe the idea behind that is that, after enough failures, the library would give up.

But, maybe it's fine to always raise on unknown errors. Feel free to send a PR @JavadZahedi.

@igorivaniuk
Copy link

Install python-socks helps me with RuntimeError: readexactly()

pip install python-socks[asyncio]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants