Unhandled error while receiving data concurrent.futures._base.CancelledError
concurrent.futures._base.CancelledError
- Isn’t it okay to throw an exception directly? Directly report an error in the file code
Test Code
- I ignore it here. I have no problem debugging the code. This happens when the Session fails.
Error
Unhandled error while receiving data
Traceback (most recent call last):
File "C:\Python\python-3.7.3-embed-amd64\lib\site-packages\telethon\network\mtprotosender.py", line 505, in _recv_loop
body = await self._connection.recv()
File "C:\Python\python-3.7.3-embed-amd64\lib\site-packages\telethon\network\connection\connection.py", line 302, in recv
result, err = await self._recv_queue.get()
File "C:\Python\python-3.7.3-embed-amd64\lib\asyncio\queues.py", line 159, in get
await getter
concurrent.futures._base.CancelledError
Press any key to continue . . .
Environment
# Use the latest source code
https://github.com/LonamiWebs/Telethon/tree/v1/telethon
Same issue here, I upgraded to last version 1.28.5 and it starts same error
asyncio==3.4.3
Telethon==1.28.5
Python==3.7
concurrent.futures._base.CancelledError
Unhandled error while receiving data
Traceback (most recent call last):
File "/envs/telegram/lib/python3.7/site-packages/telethon/network/mtprotosender.py", line 505, in _recv_loop
body = await self._connection.recv()
File "/envs/telegram/lib/python3.7/site-packages/telethon/network/connection/connection.py", line 301, in recv
raise err
File "/envs/telegram/lib/python3.7/site-packages/telethon/network/connection/connection.py", line 332, in _recv_loop
data = await self._recv()
File "/envs/telegram/lib/python3.7/site-packages/telethon/network/connection/connection.py", line 369, in _recv
return await self._codec.read_packet(self._reader)
File "/envs/telegram/lib/python3.7/site-packages/telethon/network/connection/tcpfull.py", line 25, in read_packet
packet_len_seq = await reader.readexactly(8) # 4 and 4
File "/envs/telegram/lib/python3.7/asyncio/streams.py", line 679, in readexactly
await self._wait_for_data('readexactly')
File "/envs/telegram/lib/python3.7/asyncio/streams.py", line 473, in _wait_for_data
await self._waiter
concurrent.futures._base.CancelledError
Unexpected exception in the receive loop
Traceback (most recent call last):
File "/envs/telegram/lib/python3.7/site-packages/telethon/network/connection/connection.py", line 332, in _recv_loop
data = await self._recv()
File "/envs/telegram/lib/python3.7/site-packages/telethon/network/connection/connection.py", line 369, in _recv
return await self._codec.read_packet(self._reader)
File "/envs/telegram/lib/python3.7/site-packages/telethon/network/connection/tcpfull.py", line 25, in read_packet
packet_len_seq = await reader.readexactly(8) # 4 and 4
File "/envs/telegram/lib/python3.7/asyncio/streams.py", line 679, in readexactly
await self._wait_for_data('readexactly')
File "/envs/telegram/lib/python3.7/asyncio/streams.py", line 473, in _wait_for_data
await self._waiter
There's not much I can do without clear reproduction steps.
2023-05-30 17:31:46 - [ERROR] - connection[line:343]: Unexpected exception in the receive loop Traceback (most recent call last): File "E:\pycharm_virtual_environment\telegram-client_env\lib\site-packages\telethon\network\connection\connection.py", line 332, in _recv_loop data = await self._recv() File "E:\pycharm_virtual_environment\telegram-client_env\lib\site-packages\telethon\network\connection\connection.py", line 369, in _recv return await self._codec.read_packet(self._reader) File "E:\pycharm_virtual_environment\telegram-client_env\lib\site-packages\telethon\network\connection\tcpfull.py", line 25, in read_packet packet_len_seq = await reader.readexactly(8) # 4 and 4 File "E:\python3.6.5\lib\asyncio\streams.py", line 674, in readexactly yield from self._wait_for_data('readexactly') File "E:\python3.6.5\lib\asyncio\streams.py", line 464, in _wait_for_data yield from self._waiter concurrent.futures._base.CancelledError
Process finished with exit code 0
2023-05-30 17:31:46 - [ERROR] - connection[line:343]: Unexpected exception in the receive loop Traceback (most recent call last): File "E:\pycharm_virtual_environment\telegram-client_env\lib\site-packages\telethon\network\connection\connection.py", line 332, in _recv_loop data = await self._recv() File "E:\pycharm_virtual_environment\telegram-client_env\lib\site-packages\telethon\network\connection\connection.py", line 369, in _recv return await self._codec.read_packet(self._reader) File "E:\pycharm_virtual_environment\telegram-client_env\lib\site-packages\telethon\network\connection\tcpfull.py", line 25, in read_packet packet_len_seq = await reader.readexactly(8) # 4 and 4 File "E:\python3.6.5\lib\asyncio\streams.py", line 674, in readexactly yield from self._wait_for_data('readexactly') File "E:\python3.6.5\lib\asyncio\streams.py", line 464, in _wait_for_data yield from self._waiter concurrent.futures._base.CancelledError
Process finished with exit code 0 I had the same problem, I was using python3.6, and after starting the project, telethon would get an error inside and keep loping
time.sleep blocks the thread and should not be used with asyncio unless you have a very good reason to. I wouldn't be surprised if stalling the connection that long was what caused the issue.
I'm doubtful that InviteToChannelRequest in particular is the problem. But who knows, perhaps Telegram is kicking out the user who tries using it.
time.sleepblocks the thread and should not be used withasynciounless you have a very good reason to. I wouldn't be surprised if stalling the connection that long was what caused the issue.I'm doubtful that
InviteToChannelRequestin particular is the problem. But who knows, perhaps Telegram is kicking out the user who tries using it.
time.sleep is used to not be blocked by telegram.
This script works perfectly fine with old version (tested on 1.25.4), but when upgrades it, starts this error. I haven't tested with other versions only last one.
I meet the same problem, and this is my code([email protected]):
import os
from dotenv import load_dotenv
from telethon import TelegramClient, events, sync
import python_socks
load_dotenv()
api_id = os.environ['TEL_APPID']
api_hash = os.environ['TEL_APPHASH']
temp_phone = os.environ['TEMP_PHONE']
temp_proxy_host = os.environ['TEMP_PROXY_HOST']
temp_proxy_port = os.environ['TEMP_PROXY_PORT']
# https://docs.telethon.dev/en/stable/basic/signing-in.html?highlight=proxy#signing-in-behind-a-proxy
client = TelegramClient(
'sessions/woo_elo',
api_id=api_id,
api_hash=api_hash,
proxy=(python_socks.ProxyType.SOCKS5, temp_proxy_host, 7891)
)
@client.on(events.NewMessage)
async def my_event_handler(event):
print(event.raw_text)
client.start()
print('client start -------------')
client.run_until_disconnected()
- Every message from channel works fine
- the account is a test account for the issue, and it only join one channel
- I also get the same error when the member in the channel don't send any msg for 30min-60min
so if there is no msg from the telegram for long time(30min-60min), The connect to telegram will be auto close and the telethon will try to reconnect the telegram. the full processing will throw the error the issuce mentioned
The error log:
Server closed the connection: 0 bytes read on a total of 8 expected bytes
Server closed the connection: 0 bytes read on a total of 8 expected bytes
Unhandled error while receiving data
Traceback (most recent call last):
File "/root/.virtualenvs/telethon/lib/python3.10/site-packages/telethon/network/mtprotosender.py", line 505, in _recv_loop
body = await self._connection.recv()
File "/root/.virtualenvs/telethon/lib/python3.10/site-packages/telethon/network/connection/connection.py", line 301, in recv
raise err
File "/root/.virtualenvs/telethon/lib/python3.10/site-packages/telethon/network/connection/connection.py", line 332, in _recv_loop
data = await self._recv()
File "/root/.virtualenvs/telethon/lib/python3.10/site-packages/telethon/network/connection/connection.py", line 369, in _recv
return await self._codec.read_packet(self._reader)
File "/root/.virtualenvs/telethon/lib/python3.10/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.10/asyncio/streams.py", line 706, in readexactly
raise exceptions.IncompleteReadError(incomplete, n)
asyncio.exceptions.IncompleteReadError: 0 bytes read on a total of 8 expected bytes
Maybe the telethon can use other way for check the connection when receiving msg,The current strategy seems checking the receiving bytes
Maybe the telethon can use other way for check the connection when receiving msg,The current strategy seems checking the receiving bytes
Telethon does not intentionally check just whether the connection is alive. The only correct way to handle this problem is to attempt to perform the read and be prepared to handle the lack of connection. Any other way to check if the connection is alive is likely wrong and meaningless.
If the failed connection error bubbles up and you can handle it, it's working as intended.
If it's caught, logged, and the library attempts to reconnect, it's working as intended.
If it's swallowed, then we have a problem.
Maybe the telethon can use other way for check the connection when receiving msg,The current strategy seems checking the receiving bytes
Telethon does not intentionally check just whether the connection is alive. The only correct way to handle this problem is to attempt to perform the read and be prepared to handle the lack of connection. Any other way to check if the connection is alive is likely wrong and meaningless.
If the failed connection error bubbles up and you can handle it, it's working as intended.
If it's caught, logged, and the library attempts to reconnect, it's working as intended.
If it's swallowed, then we have a problem.
OK, I get it. It's just a error log and telethon works fine for long time for me (keep alive and receive every msg. Thanks :)
I have the exact same error as @wujohns has, a lot of them started showing up massively in version 1.28. I guess Telegram could've "optimized" something on their side for the latest API layer.