encode / uvicorn

An ASGI web server, for Python. 🦄
https://www.uvicorn.org/
BSD 3-Clause "New" or "Revised" License
8.57k stars 745 forks source link

WebSockets and --max-requests does not reload Workers #2201

Closed lesc6 closed 9 months ago

lesc6 commented 10 months ago

Initial Checks

Discussion Link

https://github.com/encode/uvicorn/discussions/2146

Description

If you use independent server for websocket connection, you expect that Worker process will be killed after N requests, but it will never killed it consume more and more memory. use any ws client to reproduce.

Example Code

gunicorn --bind=unix:/tmp/ws.sock --worker-class=uvicorn.workers.UvicornWorker --workers=1 --max-requests=10

Python, Uvicorn & OS Version

any

[!IMPORTANT]

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.

Fund with Polar

lesc6 commented 10 months ago

https://github.com/benoitc/gunicorn/issues/3086

fatelei commented 9 months ago

from the code, ws protocol not update total_requests, so the max requests is not worked. ws is long connection, so it doesn't have request and response, i think it worked as expected.

lesc6 commented 9 months ago

a client still has to start the WebSocket handshake process by contacting the server and requesting a WebSocket connection. The client will send a pretty standard HTTP request with headers that looks like https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers

GET /chat HTTP/1.1
Host: example.com:8000
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
lesc6 commented 9 months ago

the problem is more global, suppose you have a web chat that works only on websockets and you have 10 workers that constantly work without rebooting; they constantly increase memory costs, which leads to complete failure of the application.

Kludex commented 9 months ago

from the code, ws protocol not update total_requests, so the max requests is not worked. ws is long connection, so it doesn't have request and response, i think it worked as expected.

Actually, this is it.