[Python] RuntimeError: unable to perform operation on TCPTransport the handler is closed

Discussão em 'Python' iniciado por Stack, Março 6, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to make an HTTP benchmark test on my local machine with autocannon tool to my fresh, brand-new project setup that I've built on FastAPI + uvicorn.

    But at some point or randomly (I don't know what's happening) I'm having an ASGI exception.

    [2019-11-14 14:16:40 +0100] [98098] [ERROR] Exception in ASGI application
    Traceback (most recent call last):
    File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/uvicorn/protocols/http/httptools_impl.py", line 385, in run_asgi
    result = await app(self.scope, self.receive, self.send)
    File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
    return await self.app(scope, receive, send)
    File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/fastapi/applications.py", line 139, in __call__
    await super().__call__(scope, receive, send)
    File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/starlette/applications.py", line 134, in __call__
    await self.error_middleware(scope, receive, send)
    File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/starlette/middleware/errors.py", line 178, in __call__
    raise exc from None
    File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/starlette/middleware/errors.py", line 156, in __call__
    await self.app(scope, receive, _send)
    File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/starlette/exceptions.py", line 73, in __call__
    raise exc from None
    File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/starlette/exceptions.py", line 62, in __call__
    await self.app(scope, receive, sender)
    File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/starlette/routing.py", line 590, in __call__
    await route(scope, receive, send)
    File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/starlette/routing.py", line 208, in __call__
    await self.app(scope, receive, send)
    File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/starlette/routing.py", line 44, in app
    await response(scope, receive, send)
    File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/starlette/responses.py", line 125, in __call__
    "headers": self.raw_headers,
    File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/starlette/exceptions.py", line 59, in sender
    await send(message)
    File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/starlette/middleware/errors.py", line 153, in _send
    await send(message)
    File "/Users/yigidix/Projects/url_shortener/lib/python3.7/site-packages/uvicorn/protocols/http/httptools_impl.py", line 482, in send
    self.transport.write(b"".join(content))
    File "uvloop/handles/stream.pyx", line 673, in uvloop.loop.UVStream.write
    File "uvloop/handles/handle.pyx", line 159, in uvloop.loop.UVHandle._ensure_alive
    RuntimeError: unable to perform operation on <TCPTransport closed=True reading=False 0x7fb255569780>; the handler is closed


    To start my application, I do:

    gunicorn url_shortener:app -w 1 -k uvicorn.workers.UvicornWorker


    I tried to raise the number of the workers. I still get these exceptions.
    ​

    To benchmark test, I do:

    autocannon -c 100 -d 40 -p 10 localhost:8000/shorten -H "Content-Type: application/json" -m POST -b '{"url": "http://yigitgenc.com"}'


    P.S: My database (PostgreSQL) works on Docker container. And also I do async database interactions (for sure). Maybe adding this info would be helpful.

    Continue reading...

Compartilhe esta Página