Telethon icon indicating copy to clipboard operation
Telethon copied to clipboard

Unhandled error while receiving data concurrent.futures._base.CancelledError

Open AtlantisDe opened this issue 2 years ago • 12 comments

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

AtlantisDe avatar May 21 '23 04:05 AtlantisDe

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

rrfaria avatar May 29 '23 23:05 rrfaria

There's not much I can do without clear reproduction steps.

Lonami avatar May 30 '23 06:05 Lonami

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

HuangShiJin1015 avatar May 30 '23 09:05 HuangShiJin1015

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

HuangShiJin1015 avatar May 30 '23 09:05 HuangShiJin1015

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.

Lonami avatar Jun 01 '23 15:06 Lonami

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.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.

rrfaria avatar Jun 02 '23 02:06 rrfaria

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()
  1. Every message from channel works fine
  2. the account is a test account for the issue, and it only join one channel
  3. 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

wujohns avatar Jun 16 '23 05:06 wujohns

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

wujohns avatar Jun 16 '23 05:06 wujohns

Maybe the telethon can use other way for check the connection when receiving msg,The current strategy seems checking the receiving bytes

wujohns avatar Jun 16 '23 05:06 wujohns

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.

Lonami avatar Jun 19 '23 18:06 Lonami

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 :)

wujohns avatar Jun 19 '23 18:06 wujohns

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.

ingria avatar Jul 18 '23 23:07 ingria