Aiohttp server critical error when disconnecting from clients

Viewed 604

There is aiohttp server. To it goes a very large number of requests from the client. If the connection between the client and the server abruptly breaks, the server may stop with an error:

Task exception was never retrieved
future: <Task finished name='Task-45565' coro=<IocpProactor.accept.<locals>.accept_coro() done, defined at D:\Python\Python_3_8_2\lib\asyncio\windows_events.py:559> exception=OSError(22, 'The specified network name is no longer available', None, 64, None)>
Traceback (most recent call last):
  File "D:\Python\Python_3_8_2\lib\asyncio\windows_events.py", line 562, in accept_coro
    await future
  File "D:\Python\Python_3_8_2\lib\asyncio\windows_events.py", line 808, in _poll
    value = callback(transferred, key, ov)
  File "D:\Python\Python_3_8_2\lib\asyncio\windows_events.py", line 551, in finish_accept
    ov.getresult()
OSError: [WinError 64] The specified network name is no longer available
Accept failed on a socket
socket: <asyncio.TransportSocket fd=604, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('0.0.0.0', 80)>
Traceback (most recent call last):
  File "D:\Python\Python_3_8_2\lib\asyncio\proactor_events.py", line 801, in loop
    conn, addr = f.result()
  File "D:\Python\Python_3_8_2\lib\asyncio\windows_events.py", line 562, in accept_coro
    await future
  File "D:\Python\Python_3_8_2\lib\asyncio\windows_events.py", line 808, in _poll
    value = callback(transferred, key, ov)
  File "D:\Python\Python_3_8_2\lib\asyncio\windows_events.py", line 551, in finish_accept
    ov.getresult()
OSError: [WinError 64] The specified network name is no longer available

Server Code:

from aiohttp import web

async def page(request):
    try:
        return web.Response(text='OK')
    except Exception as ex:
        print('Exception:', ex)

app = web.Application()
app.router.add_route('*', '/', page)
web.run_app(app, port=80)

Client code:

import asyncio
import aiohttp

async def do_get(url):
    while True:
        try:
            await asyncio.sleep(1)
            async with aiohttp.ClientSession() as session:
                async with session.get(url) as resp:
                    data = await resp.text()
        except Exception as ex:
            print('Exception:', ex)

async def client():
    workers = 5000
    tasks = [asyncio.Task(do_get('http://127.0.0.1')) for i in range(workers)]
    await asyncio.gather(*tasks)

asyncio.run(client())

To reproduce the error, you need to start the server, then start the client and after ~ 10 seconds terminate the client. An error in the server does not always appear; you may need to restart the client 3-7 times. At the same time, on the server side, I can’t catch this state in any way and I don’t know how to prevent it or restart the server.

OS: Windows 10 - Python: 3.8.2 - aiohttp: 3.6.2

0 Answers
Related