Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle request.body timeout exception

I use request.body to get AJAX request data from jQuery to my django app, but sometime request.body cause a Dyno timeout, i guess it's because of the user droping the connection and django keep waiting for the user's request, the exception look like this:

File "/app/myapp/views.py", line 271, in proccess_api
File "/app/.heroku/python/lib/python2.7/site-packages/django/http/request.py", line 233, in body
File "/app/.heroku/python/lib/python2.7/site-packages/django/http/request.py", line 292, in read
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 51, in read
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 45, in _read_limited
File "/app/.heroku/python/lib/python2.7/site-packages/newrelic-2.60.0.46/newrelic/api/web_transaction.py", line 780, in read
File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/http/body.py", line 212, in read
File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/http/body.py", line 128, in read
File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/http/unreader.py", line 38, in read
File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/http/unreader.py", line 65, in chunk
File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/base.py", line 176, in handle_abort

The line in proccess_api:

req_data = json.loads(request.body)

I'm using Django 1.8.11 on Heroku, the exception is recorded using newrelic.

My questions are:

  • How to handle this exception?
  • Is it possible to set a timeout for request.body so the request won't block the gunicorn worker?
like image 985
ahmed Avatar asked Jun 27 '26 01:06

ahmed


1 Answers

It look like this is a design limitation in gunicorn:

Gunicorn is designed to be used behind a buffering reverse proxy

Gunicorn uses a pre-forking process model by default. This means that network requests are handed off to a pool of worker processes, and that these worker processes take care of reading and writing the entire HTTP request to the client. If the client has a fast network connection, the entire request/response cycle takes a fraction of a second. However, if the client is slow (or deliberately misbehaving), the request can take much longer to complete.

like image 179
ahmed Avatar answered Jun 29 '26 16:06

ahmed