This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: asyncio task can not be used to open_connection and read data.
Type: crash Stage: resolved
Components: asyncio Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, whitestockingirl, yselivanov
Priority: normal Keywords:

Created on 2021-09-28 09:54 by whitestockingirl, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg402762 - (view) Author: 穆兰 (whitestockingirl) Date: 2021-09-28 09:54
The server code:

import asyncio
import struct
counter = 0
async def on_connection(r: asyncio.StreamReader, w: asyncio.StreamWriter):
    msg = struct.pack("HB", 3, 0)
    w.write(msg)
    await w.drain()
    global counter
    counter += 1
    print(counter, "client")
async def main():
    server = await asyncio.start_server(on_connection, '0.0.0.0', 12345)
    await server.serve_forever()
if __name__ == "__main__":
    asyncio.run(main())

The client code:

import asyncio
loop = asyncio.get_event_loop()
counter = 0
c_counter = 0
async def connection_to():
    r, w = await asyncio.open_connection('192.168.3.2', 12345)
    global c_counter
    c_counter += 1
    print(c_counter, "connected")
    await r.readexactly(3)
    global counter
    counter += 1
    print(counter, "get_msg")
async def main():
    for i in range(7000):
        t = loop.create_task(connection_to())
try:
    loop.run_until_complete(main())
    loop.run_forever()
except Exception as e:
    print(e.with_traceback(None))


I open the server on wsl debian and run the client on host windows.
Try start more client at once, Then you will find that counter is not equal to c_counter.
It nearly always happend.
Now I can not trust create_task any more.
msg402764 - (view) Author: 穆兰 (whitestockingirl) Date: 2021-09-28 10:04
Hope some one could fix it.
History
Date User Action Args
2022-04-11 14:59:50adminsetgithub: 89472
2021-09-30 08:46:48whitestockingirlsetstatus: open -> closed
stage: resolved
2021-09-30 08:46:43whitestockingirlsetresolution: not a bug
2021-09-28 10:04:06whitestockingirlsetmessages: + msg402764
2021-09-28 09:54:32whitestockingirlcreate