Gunicorn (Uvicorn Worker) continues processing requests after Heroku 30s timeout

I’m running a Django (ASGI) app on Heroku using Gunicorn with the Uvicorn worker to support WebSockets.

Heroku has a hard 30-second request timeout. When a request exceeds 30 seconds, Heroku closes the connection as expected.

Problem: Even after the Heroku timeout, Gunicorn/Uvicorn continues executing the request in the background, which wastes resources.

Gunicorn command:

newrelic-admin run-program gunicorn --workers 4 --worker-connections 200 --timeout 30 --max-requests 1000 --max-requests-jitter 500 --bind 0.0.0.0:8000  asgi:application

Questions

  1. Why does Gunicorn/Uvicorn keep running the request after Heroku times out?

  2. Is there a way to cancel the request when the client disconnects?

  3. Should this be handled in Django (async cancellation/middleware), or via Gunicorn settings?

Any help is appreciated.

  1. "HTTP is stateless—once the server receives the request, it doesn’t automatically stop processing when the client goes away.", see Reddit post: https://www.reddit.com/r/django/comments/1nu9tw2/django_request_continues_running_on_server_even/

  2. 3: Yes, you can cancel the request. Send a http.disconnect message, see Django forum: https://forum.djangoproject.com/t/asgihandler-doesnt-handle-http-disconnect-if-a-request-was-sent-before-the-disconnect/18981

Вернуться на верх