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

RuntimeError: readexactly() called while another coroutine is already waiting for incoming data #4314

Open
3 tasks done
wwang-w opened this issue Feb 18, 2024 · 3 comments
Open
3 tasks done

Comments

@wwang-w
Copy link

wwang-w commented Feb 18, 2024

Code that causes the issue

the code is:

msg_iter = client.iter_messages(entity.id, reverse=True, min_id=100)
async for msg in msg_iter:
     if msg.media and (isinstance(msg.media, telethon.tl.types.MessageMediaPhoto) or isinstance(msg.media, telethon.tl.types.MessageMediaDocument)):
        media_path = await msg.download_media('media/{}/{}'.format(msg.file.mime_type, media_id))
Traceback (most recent call last):
  File "。。。(my path)/python3.9/site-packages/telethon/network/connection/connection.py", line 335, in _recv_loop
    data = await self._recv()
  File "。。。(my path)/python3.9/site-packages/telethon/network/connection/connection.py", line 374, in _recv
    return await self._codec.read_packet(self._reader)
  File "。。。(my path)/python3.9/site-packages/telethon/network/connection/tcpfull.py", line 25, in read_packet
    packet_len_seq = await reader.readexactly(8)  # 4 and 4
  File "。。。(my path)/python3.9/asyncio/streams.py", line 723, in readexactly
    await self._wait_for_data('readexactly')
  File "。。。(my path)/python3.9/asyncio/streams.py", line 503, in _wait_for_data
    raise RuntimeError(
RuntimeError: readexactly() called while another coroutine is already waiting for incoming data
Unhandled error while receiving data
Traceback (most recent call last):
  File "。。。(my path)/python3.9/site-packages/telethon/network/mtprotosender.py", line 507, in _recv_loop
    body = await self._connection.recv()
  File "。。。(my path)/python3.9/site-packages/telethon/network/connection/connection.py", line 304, in recv
    raise err
  File "。。。(my path)/python3.9/site-packages/telethon/network/connection/connection.py", line 335, in _recv_loop
    data = await self._recv()
  File "。。。(my path)/python3.9/site-packages/telethon/network/connection/connection.py", line 374, in _recv
    return await self._codec.read_packet(self._reader)
  File "。。。(my path)/python3.9/site-packages/telethon/network/connection/tcpfull.py", line 25, in read_packet
    packet_len_seq = await reader.readexactly(8)  # 4 and 4
  File "。。。(my path)/python3.9/asyncio/streams.py", line 723, in readexactly
    await self._wait_for_data('readexactly')
  File "。。。(my path)/python3.9/asyncio/streams.py", line 503, in _wait_for_data
    raise RuntimeError(
RuntimeError: readexactly() called while another coroutine is already waiting for incoming data
Server closed the connection: 0 bytes read on a total of 8 expected bytes

Expected behavior

I was using "msg.download_media" in a loop,the error appear frequently. how can i do to resolve it

Actual behavior

msg.download_media

Traceback

No response

Telethon version

1.33.1

Python version

3.9.18

Operating system (including distribution name and version)

mac os 11.1

Other details

No response

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 an open duplicate.
  • I ran pip install -U https://github.com/LonamiWebs/Telethon/archive/v1.zip and triggered the bug in the latest version.
@Lonami
Copy link
Member

Lonami commented Feb 18, 2024

Don't really know why this happens. I have never been able to reproduce it. Telegram might be closing the connection. I doubt I'll look into this any time soon.

@wwang-w
Copy link
Author

wwang-w commented Feb 19, 2024

import telethon
import socks
import asyncio

async def spider(client):
    async for dialog in client.iter_dialogs():
        try:
            entity = await client.get_entity(dialog.title)
        except Exception as e:
            print("(ERROR) Entity: get_entity '%s' error: %s" % (dialog.title, e))
            continue
        if isinstance(entity, telethon.tl.types.Channel):  # and dialog.is_group:
                msg_iter = client.iter_messages(entity.id, reverse=True, min_id=100)
                async for msg in msg_iter:
                     if msg.media and (isinstance(msg.media, telethon.tl.types.MessageMediaPhoto) or isinstance(msg.media, telethon.tl.types.MessageMediaDocument)):
                         if msg.photo:
                             media_id = msg.photo.id
                         else:
                             media_id = msg.media.document.id  

                         media_path = await msg.download_media('media/{}/{}'.format(msg.file.mime_type, media_id))
                         print("fetch msg: dialog_id=%s, msg_id=%s" % (msg.chat_id, msg.id))



async def main(client):
    await spider(client)


api_id = XXX # my api_id
api_hash = "XXX"  # my api_hash
# session_name = 'session_name'
phone = "my phone"
proxy_param = (socks.HTTP, '127.0.0.1', 15236)  # Proxy settings, if you need

client = telethon.TelegramClient(session_name, api_id, api_hash,
                                 proxy=proxy_param).start(phone=phone)


loop = asyncio.get_event_loop()
loop.run_until_complete(main(client))

this is my main code,when i run it , i got the error said above frequently。you can try to run this code at least 20min, maybe you can reproduce this error.

@FreeM1ne
Copy link

import telethon
import socks
import asyncio

async def spider(client):
    async for dialog in client.iter_dialogs():
        try:
            entity = await client.get_entity(dialog.title)
        except Exception as e:
            print("(ERROR) Entity: get_entity '%s' error: %s" % (dialog.title, e))
            continue
        if isinstance(entity, telethon.tl.types.Channel):  # and dialog.is_group:
                msg_iter = client.iter_messages(entity.id, reverse=True, min_id=100)
                async for msg in msg_iter:
                     if msg.media and (isinstance(msg.media, telethon.tl.types.MessageMediaPhoto) or isinstance(msg.media, telethon.tl.types.MessageMediaDocument)):
                         if msg.photo:
                             media_id = msg.photo.id
                         else:
                             media_id = msg.media.document.id  

                         media_path = await msg.download_media('media/{}/{}'.format(msg.file.mime_type, media_id))
                         print("fetch msg: dialog_id=%s, msg_id=%s" % (msg.chat_id, msg.id))



async def main(client):
    await spider(client)


api_id = XXX # my api_id
api_hash = "XXX"  # my api_hash
# session_name = 'session_name'
phone = "my phone"
proxy_param = (socks.HTTP, '127.0.0.1', 15236)  # Proxy settings, if you need

client = telethon.TelegramClient(session_name, api_id, api_hash,
                                 proxy=proxy_param).start(phone=phone)


loop = asyncio.get_event_loop()
loop.run_until_complete(main(client))

this is my main code,when i run it , i got the error said above frequently。you can try to run this code at least 20min, maybe you can reproduce this error.

The problem is not in the code, maybe the internet or the ip is dirty

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

3 participants