Introducing Livops 2.0! Promo codeCEOgives you 8,000 so‘m on your balance.What’s new

Gunicorn [CRITICAL] WORKER TIMEOUT: what it means and how to fix it

Gunicorn's master killed a worker that sent no heartbeat for longer than --timeout, 30 seconds by default. With the default sync worker that means one request ran too long. Move the slow work out of the request, use gthread workers for slow I/O, load heavy models at start with --preload instead of in the first request, and raise --timeout only for the app that needs it.

By Livops team, the people who build and run LivopsUpdated

What the log shows when a worker runs out of time in the middle of a request, with Gunicorn’s default sync worker:

[CRITICAL] WORKER TIMEOUT (pid:42)
[ERROR] Error handling request GET /report
Traceback (most recent call last):
  ...
  File ".../gunicorn/workers/base.py", line ..., in handle_abort
    sys.exit(1)
SystemExit: 1
[INFO] Worker exiting (pid: 42)
[INFO] Booting worker with pid: 57

The SystemExit: 1 traceback is Gunicorn stopping the worker, not your code failing. This page explains what the master process is doing, lists the causes from the most common with the exact flags that fix each one, shows how to tell it apart from Worker failed to boot and from an out-of-memory kill, and says how to change the timeout on Livops.

What does the error mean?

Gunicorn’s master process forks workers and watches each one through a heartbeat file. A worker that does not update it within --timeout seconds, 30 by default, is considered silent and is killed and restarted[1]. Idle workers keep updating the heartbeat while they wait for requests[1], so a timeout means the worker was busy with something that did not let it get back to its loop.

What happens next is in Gunicorn’s source. The master logs WORKER TIMEOUT (pid:N) and sends the worker SIGABRT; if the worker is still there at the next check, it sends SIGKILL[2]. On SIGABRT the worker calls the worker_abort hook and exits with sys.exit(1)[3], which is the traceback above. The master then boots a new worker to keep the count[2].

What the visitor gets: 500, 502 or 504

  • 500. In a sync worker, if the response had not started yet, Gunicorn catches that exit, logs Error handling request and writes a 500 Internal Server Error to the client before the worker exits[4][3].
  • 502. If the response had already started, the worker closes the connection instead[4], and a worker killed with SIGKILL answers nothing at all. The proxy in front sees a connection closed without a full answer and reports 502 Bad Gateway.
  • 504. If the proxy’s own timeout is shorter than Gunicorn’s, the proxy gives up first and answers 504 while the worker is still busy.

Not the same as “Worker failed to boot”

Worker failed to boot means importing your app raised an exception: the worker exits with code 3, and the master shuts the whole server down with Reason: Worker failed to boot.[2]. After a WORKER TIMEOUT the master keeps running and boots a replacement[2]. Read the lines just above the message: a Python traceback from your own code ending in an exception like ModuleNotFoundError means a failed boot; WORKER TIMEOUT means a worker that took too long.

Causes, most common first

1. A request takes longer than the timeout

The default sync worker handles one request at a time[6], and while it runs your view it does not update its heartbeat. A report that queries for a minute, a CSV export, an image or PDF generated in the request, a call to a slow external API or a language model: anything past 30 seconds is killed.

With the sync worker, the request is the one named in Error handling request right after the timeout[4][3]. Then, in order of preference:

  • Move the work out of the request. Put the job on a queue, answer at once, and let a separate background process do it. The page polls, or the user is notified when it is done.
  • Make it faster. An index for the query, a smaller page, and a timeout on the outgoing HTTP call shorter than Gunicorn’s, so your code fails with a clear error instead of being killed.
  • Raise the timeout for this app. Gunicorn’s own advice for a worker timeout is to increase it or investigate slow requests[8]:
.venv/bin/gunicorn app:app --timeout 120

The settings reference says the default of thirty seconds should generally suffice, and to set it noticeably higher only if you are sure of the repercussions for sync workers[1]: a worker held for 120 seconds serves no one else for 120 seconds. --timeout 0 disables timeouts for all workers entirely[1], so a stuck worker is never replaced. Avoid it.

2. Sync workers blocked on I/O: use threads

Sync workers assume your app is CPU or network bound and avoids indefinite blocking operations; Gunicorn recommends async workers for long blocking calls such as external APIs and slow databases, and for streaming, long polling and WebSockets[6]. Two options, the first with no code changes:

# a thread pool in each worker
.venv/bin/gunicorn app:app -k gthread --workers 2 --threads 4

# greenlets: needs gevent installed (pip install "gunicorn[gevent]")
.venv/bin/gunicorn app:app -k gevent --worker-connections 1000

With gthread, threads can extend request time beyond the worker timeout while the worker keeps notifying the master[6]. For any worker class other than sync, the timeout only means the worker process is still communicating; it is not tied to the length of one request[1]. Setting --threads above 1 with the sync worker switches to gthread on its own[1]. gevent handles thousands of connections, but may need patches for some libraries, such as psycogreen for Psycopg, and does not suit code that relies on blocking behaviour[6].

An ASGI app such as FastAPI needs an ASGI worker class instead; Gunicorn’s design page lists them[6]. Threads do not make CPU-heavy work faster: a request that computes for 90 seconds still takes 90 seconds, so for that, go back to cause 1.

3. A model or cache is loaded in the first request

A common pattern to make the start faster is to load a machine learning model, a large file or a cache “lazily”, the first time a request needs it. That moves the load into a request, where the timeout applies: if loading takes 40 seconds, the first request after every worker start is killed, and the next worker starts the same way.

Load it at start instead. In Gunicorn’s current source, the worker loads your app before it enters its request loop, and its heartbeat file only starts counting with the first update from that loop[3][5][2], so loading at import is not cut off by --timeout. Better still, --preload loads the application code before the worker processes are forked[1]: the master does it once, before any worker starts[2], and the documentation says it can save RAM and speed up boot. The trade-off is that you can no longer reload the code by restarting workers[1].

.venv/bin/gunicorn app:app --preload --workers 2

A slow start has its own cost on a platform: until a worker is serving, nothing answers on the port. On Livops a new version that does not answer within a minute fails the deploy (see below).

4. The worker was killed for memory

A worker the kernel kills for using too much memory dies from SIGKILL, and Gunicorn logs it as[2]:

[ERROR] Worker (pid:42) was sent SIGKILL! Perhaps out of memory?

Look at the line before it. With WORKER TIMEOUT just above, the SIGKILL was Gunicorn’s own second step for a worker that did not exit after SIGABRT[2], and the cause is still a slow worker. With no timeout above it, suspect memory: the FAQ names the out-of-memory killer in cgroups-limited environments as a common culprit, and suggests dmesg | grep gunicorn to confirm[7]. Each worker holds its own copy of the app, so run fewer workers, use --preload to share what is loaded before the fork[1], or --max-requests to restart each worker after a number of requests, which limits the damage of memory leaks[1]. Gunicorn’s Docker guide lists the same three: fewer workers, --max-requests, a higher container memory limit[8].

5. The heartbeat itself blocks

The heartbeat involves a call on a temporary file that can block a worker for an arbitrary time if the directory is on a disk-backed filesystem[1]. If /tmp is not RAM-backed, the FAQ suggests a tmpfs mount and --worker-tmp-dir pointing at it[7]. This is rare next to causes 1 to 4; check it when timeouts hit fast endpoints.

How to find the slow code

Make the worker print where it was when it was stopped. worker_abort is called when a worker receives SIGABRT, which generally happens on timeout[1], and a gunicorn.conf.py in the working directory is read by default[9]:

# gunicorn.conf.py
import faulthandler

def worker_abort(worker):
    faulthandler.dump_traceback(all_threads=True)

The next timeout prints the stack of every thread, down to the line of your code that was running. To reproduce it locally with the server’s settings, run one worker and call the slow endpoint:

.venv/bin/gunicorn app:app --workers 1 --timeout 30 --bind 127.0.0.1:8000
curl -i http://127.0.0.1:8000/report

gunicorn --print-config app:app prints the fully resolved configuration[9], a quick way to see which timeout and worker class are really in effect.

On Livops

  • The start command Livops writes names no timeout and no worker count. For Flask with gunicorn in the requirements it is .venv/bin/gunicorn <module>:<app> --bind 0.0.0.0:3000; for Django, .venv/bin/gunicorn <project>.wsgi:application --bind 0.0.0.0:3000, after migrate. So Gunicorn’s defaults apply: the sync worker, a 30-second timeout, and one worker unless WEB_CONCURRENCY says otherwise[1].
  • Change it with a variable. Gunicorn reads settings from the GUNICORN_CMD_ARGS environment variable, and only flags on the command line take priority over it[9]. The command above sets neither, so a variable in the service’s Variables section is enough, for example GUNICORN_CMD_ARGS = --timeout 120 -k gthread --threads 4. New values take effect on the next deploy; to apply them now, restart the service.
  • Or write the start command yourself. start in livops.json is applied when the project is created; after that, the start command is changed in the project’s settings. The block below shows the form.
  • Slow jobs can move to a worker. A worker service is an always-on background process with no public URL, such as a queue consumer, and it receives databases and shared variables like a regular service. The web app answers at once and the worker does the slow part.
  • Memory is named in the log. An app that uses more memory than its plan allows is stopped, and the log says Killed or exit code 137. Choose a bigger plan in the settings, or use less: fewer workers, or --preload.
  • The log shows the timeout. The Logs page shows the app’s output, and livops logs <service> -f follows it in the terminal. A new version that does not answer within a minute, for example because loading takes too long, fails the deploy as start_failed.
{
  "start": ".venv/bin/gunicorn app:app --preload --timeout 120 --bind 0.0.0.0:$PORT"
}

More in the docs: The app did not start, Variables and Frameworks and livops.json.

Questions

What is the default Gunicorn timeout?

30 seconds. A worker that has not updated its heartbeat for longer than that is killed and replaced. Change it with --timeout or -t on the command line, timeout in gunicorn.conf.py, or the GUNICORN_CMD_ARGS variable. 0 turns the timeout off for every worker, so a stuck worker is never replaced.

Is WORKER TIMEOUT the same as Worker failed to boot?

No. Worker failed to boot means importing your app raised an exception: the worker exits with code 3 and the master shuts the server down. WORKER TIMEOUT means a worker stayed busy too long: the master kills that worker, keeps running and boots a new one. The first is fixed in your code or configuration, the second by making the slow work shorter or the timeout longer.

Why does the log say Perhaps out of memory right after WORKER TIMEOUT?

Gunicorn first sends a timed-out worker SIGABRT. If the worker is still there at the next check, it sends SIGKILL, and every worker that dies from SIGKILL is logged as was sent SIGKILL! Perhaps out of memory? In that order the SIGKILL came from Gunicorn itself. The same line with no WORKER TIMEOUT before it points at the kernel's out-of-memory killer.

Should I just set a much higher timeout?

Only for an app where long requests are expected, and only as high as the slowest legitimate request. A sync worker serves one request at a time, so a worker held for two minutes serves nobody else for two minutes. Moving the slow job to a background process fixes the cause; a higher timeout only gives it more room.

Do gthread or gevent workers get WORKER TIMEOUT?

Less often. For worker classes other than sync, the timeout only means the worker process has stopped communicating with the master; it is not tied to how long one request takes. A gthread worker keeps notifying the master while its threads handle long requests. A worker whose whole process stops responding is still killed.

How do I raise the timeout on Livops without changing the start command?

Add a variable named GUNICORN_CMD_ARGS with the value --timeout 120 in the service's Variables section. Gunicorn reads it, and the start command Livops writes for Flask and Django sets no timeout of its own, so the variable applies. New values take effect on the next deploy, or at once if you restart the service.

Sources

  1. Gunicorn — Settings (gunicorn.org). Checked .
  2. Gunicorn source — gunicorn/arbiter.py (raw.githubusercontent.com). Checked .
  3. Gunicorn source — gunicorn/workers/base.py (raw.githubusercontent.com). Checked .
  4. Gunicorn source — gunicorn/workers/sync.py (raw.githubusercontent.com). Checked .
  5. Gunicorn source — gunicorn/workers/workertmp.py (raw.githubusercontent.com). Checked .
  6. Gunicorn — Design (gunicorn.org). Checked .
  7. Gunicorn — FAQ (gunicorn.org). Checked .
  8. Gunicorn — Docker (gunicorn.org). Checked .
  9. Gunicorn — Configuration overview (gunicorn.org). Checked .

Put your project online today

Connect GitHub or drop a folder. No card is asked for.

Deploy your Python app

Got a question? — @livops_uz