Gunicorn & Django: Handle timeout

258 Views Asked by At

We run a Django REST API behind Gunicorn (on a Kubernetes Cluster in EKS exposed via AWS ALB).

The Gunicorn command:

gunicorn config.wsgi --timeout 20 (...)

When a request takes over 20 seconds to be processed by Django (due to various reasons), gunicorn will timeout by emitting a SigAbt signal and restart the gunicorn worker.

However, this causes an issue in tracking the error in various tools such as Datadog or Sentry which are not able to track the error correctly.

Instead, we would like to emit an explicit error (customer error) called TimeoutError.

After a thorough investigation, we want to find the best way to raise this TimeoutError at Django Level when the request takes 20 seconds to complete.

What would be a recommended solution?

1

There are 1 best solutions below

0
Yassine Belmamoun On

We added a configuration file to gunicorn:

gunicorn config.wsgi -c gunicorn_config.py --timeout 20 (...)

in which we defined a Gunicorn Server Hook used to raise the exception:

# gunicorn_config.py

from (...) import CustomWorkerAbortException 

def worker_abort(worker):
    raise CustomWorkerAbortException()

Documentation:

worker_abort is called when a worker received the SIGABRT signal. This call generally happens on timeout. The callable needs to accept one instance variable for the initialized Worker.