Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Databases and Redis

PostgreSQL logs unexpected EOF with an open transaction

Your error

LOG: unexpected EOF on client connection with an open transaction2 more lines it can show

Fix it fast

Most common cause, 1 of 3

A Celery hard time limit killed the task inside a transaction

Add a soft limit below the hard one.

Fix
# settings for the Celery app
task_soft_time_limit = 240
task_time_limit = 300

# tasks.py
import logging
from celery.exceptions import SoftTimeLimitExceeded
from django.db import transaction

logger = logging.getLogger(__name__)

@app.task
def import_orders():
    try:
        with transaction.atomic():
            do_import()
    except SoftTimeLimitExceeded:
        logger.warning("import_orders stopped at the soft limit; batch rolled back")
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

LOG: unexpected EOF on client connection with an open transaction means the client vanished mid-transaction, so PostgreSQL rolled the work back. In Django and Celery stacks the usual cause is a Celery hard time limit, a Gunicorn worker timeout, or a container stop that ends in SIGKILL. Match the log time to those events. Applies to PostgreSQL 16 to 19.

  • Applies to PostgreSQL 18.7 and 19.0, Celery 5.7.0, Gunicorn 27.0.1, Django 6.1.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Take the time of the PostgreSQL line and look for a kill event at the same moment in the worker and web server logs. A Celery hard time limit line is cause 1, a Gunicorn WORKER TIMEOUT is cause 2, and a deploy or container restart with neither line is cause 3. The paths below are examples; read the logs wherever your setup writes them.

Run this first
grep -h "unexpected EOF on client connection" /var/log/postgresql/*.log | tail -5
grep -hE "Hard time limit|WORKER TIMEOUT" /var/log/celery/*.log /var/log/gunicorn/*.log | tail -5

Then find the line you see in this list and open its fix:

  1. 1A Celery hard time limit killed the task inside a transactionThe Celery worker log shows Hard time limit (Ns) exceeded for the task at the same second as the PostgreSQL line
  2. 2Gunicorn killed a slow Django request that held a transactionThe Gunicorn error log has [CRITICAL] WORKER TIMEOUT (pid:N) right before the PostgreSQL line, usually on a slow view or one using ATOMIC_REQUESTS
  3. 3A container stop or redeploy sent SIGKILL to a busy processThe EOF lines cluster at deploy or restart times, and neither the Celery nor the Gunicorn log shows a timeout

What does the error look like?

  • LOG: unexpected EOF on client connection with an open transaction
  • Hard time limit (300s) exceeded for tasks.import_orders[...]
  • [CRITICAL] WORKER TIMEOUT (pid:1234)

Cause 1 of 3

A Celery hard time limit killed the task inside a transaction

You seeThe Celery worker log shows Hard time limit (Ns) exceeded for the task at the same second as the PostgreSQL line

Check

PostgreSQL writes unexpected EOF on client connection with an open transaction when the socket closes without a Terminate message while a transaction is active, and it then rolls that transaction back. Celery's hard time limit causes exactly that: the docs say the process running the task is terminated and replaced, and the hard limit cannot be caught. A task still inside transaction.atomic() loses its connection mid-write.

Check
grep -n "time_limit" -r . --include=*.py

Fix

Add a soft limit below the hard one. The soft limit raises SoftTimeLimitExceeded inside the task, so the atomic block exits with an exception and Django rolls the transaction back itself before the hard limit arrives. Split very long imports into smaller tasks.

Fix
# settings for the Celery app
task_soft_time_limit = 240
task_time_limit = 300

# tasks.py
import logging
from celery.exceptions import SoftTimeLimitExceeded
from django.db import transaction

logger = logging.getLogger(__name__)

@app.task
def import_orders():
    try:
        with transaction.atomic():
            do_import()
    except SoftTimeLimitExceeded:
        logger.warning("import_orders stopped at the soft limit; batch rolled back")

Verify

Run a long task. The worker logs the soft limit warning, and PostgreSQL no longer prints the EOF line for that task.

Verify
grep -h "unexpected EOF on client connection" /var/log/postgresql/*.log | tail -5

Confirmed by docs.celeryq.dev

Did this fix it?

Cause 2 of 3

Gunicorn killed a slow Django request that held a transaction

You seeThe Gunicorn error log has [CRITICAL] WORKER TIMEOUT (pid:N) right before the PostgreSQL line, usually on a slow view or one using ATOMIC_REQUESTS

Check

The EOF line also appears when Gunicorn ends a request worker in the middle of a database transaction. Gunicorn's timeout setting, 30 seconds by default, kills and restarts any worker that stays silent longer than that. A sync worker stuck in a long query or a slow external call inside an open transaction is cut off before it can commit.

Check
grep -rn "timeout" gunicorn.conf.py

Fix

Hand the long part of the view to a background task, such as a Celery job, so the response goes back fast. If one service really needs longer requests, raise its timeout on purpose instead of for every app.

Fix
gunicorn myproject.wsgi --workers 3 --timeout 120

Verify

Repeat the slow request. Gunicorn logs no WORKER TIMEOUT and PostgreSQL logs no EOF line for it.

Verify
grep -h "WORKER TIMEOUT" /var/log/gunicorn/*.log | tail -5

Confirmed by gunicorn.org

Did this fix it?

Cause 3 of 3

A container stop or redeploy sent SIGKILL to a busy process

You seeThe EOF lines cluster at deploy or restart times, and neither the Celery nor the Gunicorn log shows a timeout

Check

Stopping a container sends SIGTERM first and SIGKILL once the grace period ends, 10 seconds by default for Linux containers. A worker that is still finishing a task or request after that is killed with its transaction open, which is the same disconnect PostgreSQL reports.

Check
docker ps

Fix

Give the container enough stop time to finish its longest normal unit of work, either per stop or as the container's default.

Fix
docker stop --timeout 120 celery-worker
# or set the default when the container is created
docker run --stop-timeout 120 --name celery-worker my-celery-image

Verify

Redeploy while a task is running. The task finishes or stops at its soft limit, and no EOF line appears at the deploy time.

Verify
grep -h "unexpected EOF on client connection" /var/log/postgresql/*.log | tail -5

Confirmed by docs.docker.com

Did this fix it?

How do I stop it coming back?

  • Add %a and %h to log_line_prefix so each EOF line names the application and client host that disappeared.
  • Give every Celery task with database writes both a soft and a hard time limit, with the soft one far enough below to roll back.
  • Keep transaction.atomic() blocks short and never wrap a slow HTTP call to another service inside one.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.