uvicorn not closing sockets
See original GitHub issueWe 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:
- Created 2 years ago
- Reactions:9
- Comments:8 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
and run
nc -z localhost:8000
now you will have a new CLOSE_WAIT connection with netstatapparently, 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 functionIt 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