question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

uvicorn not closing sockets

See original GitHub issue

We are using the latest uvicorn 0.15. It seems that after we upgraded to uvicorn 0.15 that our AWS load balancer health checks fail after a certain amount of time. Upon further investigation we found that all of our sockets are being used up because they are staying open in a CLOSE_WAIT status.

# netstat -ant | awk '{print $6}' | sort | uniq -c | sort -n
   1 Foreign
   1 LISTEN
   1 established)
   7 TIME_WAIT
  13 ESTABLISHED
7047 CLOSE_WAIT

Workaround

For now we have switched our worker from UvicornWorker to UvicornH11Worker and it has resolved our issue.

Environment

Running uvicorn 0.15.0 with CPython 3.7.12 on Linux

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:9
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
shahar-medigatecommented, Dec 1, 2021

We encountered the issue in our systems as well, a simple fastapi service behind AWS EKS load-balancer we also managed to reproduce the issue to a single commit https://github.com/encode/uvicorn/commit/960d4650db0259b64bc41f69bc7cdcdb1fdbcbf3

A simple program to reproduce

import uvicorn

async def app(scope, receive, send):
    assert scope["type"] == "http"

    await send(
        {
            "type": "http.response.start",
            "status": 200,
            "headers": [
                [b"content-type", b"text/plain"],
            ],
        }
    )
    await send(
        {
            "type": "http.response.body",
            "body": b"Hello, world!",
        }
    )


if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", reload=False, log_level=5)

and run nc -z localhost:8000 now you will have a new CLOSE_WAIT connection with netstat

apparently, it got something to do with uvloop don’t call connection_lost or something we bypass the issue by setting loop="asyncio" to the run function

3reactions
pacoyangcommented, Sep 29, 2021

I don’t see how the change of worker class would solve this issue 🤔

It maybe because h11 will close the transport when if the connection is invalid HTTP request, this issue similarity to #1192 https://github.com/encode/uvicorn/blob/master/uvicorn/protocols/http/h11_impl.py#L140

Read more comments on GitHub >

github_iconTop Results From Across the Web

Uvicorn not closing connection when directly ... - Stack Overflow
This is apparently an issue in channels , where the close() method is not ready to be called during the socket setup.
Read more >
Server Behavior - Uvicorn
Stop the previous server processes from listening on the existing socket. Close any connections that are not currently waiting on an HTTP response,...
Read more >
The Socket.IO Server — python-socketio documentation
The default filename to serve for slash-ending URLs can be set in the static files dictionary with an empty key: ... If the...
Read more >
Server Workers - Gunicorn with Uvicorn - FastAPI
Gunicorn by itself is not compatible with FastAPI, as FastAPI uses the newest ... But Gunicorn supports working as a process manager and...
Read more >
[Solved]-Uvicorn not closing connection when directly closing ...
This is apparently an issue in channels , where the close() method is not ready to be called during the socket setup. From...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found