Python

Gunicorn WORKER TIMEOUT: why your worker was killed mid-request

The log says out of memory, with a question mark, and it is usually neither memory nor Gunicorn. A request kept one worker busy past the timeout. Here is how to find which request, see the exact line it was stuck on, and stop it happening.

The app is up, most pages are fine, and every so often a visitor gets a 502. The Gunicorn log has the same pair of lines each time: [CRITICAL] WORKER TIMEOUT (pid:2481), then [ERROR] Worker (pid:2481) was sent SIGKILL! Perhaps out of memory?. A replacement worker boots a moment later, and nothing explains what the old one was doing.

What does WORKER TIMEOUT mean?

It means one worker stopped checking in with the Gunicorn master for longer than the timeout setting, 30 seconds by default, so the master killed it and started a new one. The master first sends SIGABRT, then SIGKILL if the worker does not exit, and then boots a replacement. nginx, which was waiting for an answer on that connection, sees it close and returns a 502 to the visitor.

The detail that matters is the worker type. With the default sync worker, a worker handles one request at a time and cannot check in while it is busy. So for sync workers, the timeout is effectively a time limit on every single request. Gunicorn's own documentation spells out the difference: for the other worker types, the timeout only means the worker process is still alive and communicating, and has nothing to do with how long one request takes.

What keeps a worker busy that long?

1. An outbound call with no timeout. The most common cause by a wide margin. A payment provider, a shipping API or an internal service stops answering, and the request waits on it until Gunicorn gives up.

2. A slow database query. A report that scans a large table without an index, or a page that runs one query per row in a loop.

3. Real work inside the request. Generating a large export, resizing images or calling a machine-learning model synchronously. Work that legitimately takes a minute does not belong in a web request.

4. A slow start. If loading the app takes longer than the timeout, new workers are killed while still booting, and the log shows the same timeout over and over with no traffic at all. With sync workers, --preload loads the app once in the master before forking, so workers start almost instantly. Making the startup itself faster is the better fix.

import requests

# connect timeout, read timeout (seconds)
response = requests.get(url, timeout=(3.05, 10))
Diagram: a Gunicorn sync worker handling one slow request. The master process expects each worker to check in regularly. A sync worker busy with one request cannot check in until the request ends. After the timeout, 30 seconds by default, the master logs WORKER TIMEOUT, sends the worker SIGABRT, then SIGKILL, and boots a replacement. nginx sees the connection close and returns a 502 to the visitor. The usual causes are an outbound HTTP call with no timeout, a slow database query, a long report or export built inside the request, and an app that takes longer than the timeout to start.
With sync workers, the timeout is effectively a time limit for every request.

How do you find the request that was stuck?

Gunicorn's access log only writes a line when a request finishes, so the request that was killed never appears there. Two places still have it. nginx logs every request it answered, including the 502, with the path and the exact time, so match its timestamp against the WORKER TIMEOUT line. Better still, ask the worker where it was stuck before it dies. Gunicorn calls a worker_abort hook when a worker receives SIGABRT, which is exactly what happens on a timeout:

# gunicorn.conf.py
import faulthandler
import sys

def worker_abort(worker):
    worker.log.warning("worker timed out, dumping stack")
    faulthandler.dump_traceback(file=sys.stderr, all_threads=True)

Restart Gunicorn with that config file. The next timeout prints the Python stack of every thread in the dying worker, and the frame inside your code usually points straight at the cause: a requests.get, a cursor executing a query, or a loop.

Should you raise the timeout?

Only once you know why the request is slow and have decided it is allowed to be. Raising --timeout to 120 means a hung request now holds a sync worker for two minutes instead of thirty seconds, and with only a few workers, a handful of those block the whole site. The usual order of fixes:

  • Put a timeout on every outbound call, and decide what the page does when the call fails.
  • Move long work out of the request. Queue it with Celery or RQ, return straight away, and let the page check on the result.
  • Fix the query with an index, or by fetching related rows in one query instead of one per row.
  • Pick a worker type that fits. Threaded workers (--threads) or async workers keep one slow request from blocking the whole worker. They change what the timeout means, so you still need your own request timeouts.

Keep the layers consistent too. nginx's proxy_read_timeout defaults to 60 seconds, so a Gunicorn timeout raised above that turns the visitor's 502 into a 504 without the request ever succeeding.

The Python connector guide covers setup. If the worker never manages to start at all, the cause is usually an import, covered in ModuleNotFoundError in production.

Found something wrong in here? Tell me.

Checking API...
Checking AI Gateway...
Check status
⚠️ Setup Required