This demonstrates how request_finished
signal may never fire when a client disconnects before the response is sent. Two print statements are used to demonstrate this. One is in the request_finished
signal handler and the other is a print for when http.disconnect
is received in ASGIHandler`` (django/core/handlers/asgi.py see custom django branch installed via
requirements.txt`)
To reproduce:
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
# Run either daphne or uvicorn asgi server
daphne -p 8002 asgi:application
# OR
gunicorn -k uvicorn.workers.UvicornWorker asgi -b 0.0.0.0:8002
Go to http://localhost:8002/ now if you refresh the page within 5 seconds you will see that for the first request only http.disconnect
is printed to the console and request_finished
is only printed for the last one. This means that cleanups (db connection closing) normally performed via request_finished
signal are not run for the first request.