PostgreSQL logs unexpected EOF with an open transaction
Fix it fast
Most common cause, 1 of 3A Celery hard time limit killed the task inside a transaction
Add a soft limit below the hard one.
# 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")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.
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.
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 -5Then find the line you see in this list and open its fix:
- 1A Celery hard time limit killed the task inside a transaction
The Celery worker log shows Hard time limit (Ns) exceeded for the task at the same second as the PostgreSQL line - 2Gunicorn killed a slow Django request that held a transaction
The Gunicorn error log has [CRITICAL] WORKER TIMEOUT (pid:N) right before the PostgreSQL line, usually on a slow view or one using ATOMIC_REQUESTS - 3A container stop or redeploy sent SIGKILL to a busy process
The 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 transactionHard 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.
grep -n "time_limit" -r . --include=*.pyFix
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.
# 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.
grep -h "unexpected EOF on client connection" /var/log/postgresql/*.log | tail -5Confirmed by docs.celeryq.dev
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.
grep -rn "timeout" gunicorn.conf.pyFix
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.
gunicorn myproject.wsgi --workers 3 --timeout 120Verify
Repeat the slow request. Gunicorn logs no WORKER TIMEOUT and PostgreSQL logs no EOF line for it.
grep -h "WORKER TIMEOUT" /var/log/gunicorn/*.log | tail -5Confirmed by gunicorn.org
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.
docker psFix
Give the container enough stop time to finish its longest normal unit of work, either per stop or as the container's default.
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-imageVerify
Redeploy while a task is running. The task finishes or stops at its soft limit, and no EOF line appears at the deploy time.
grep -h "unexpected EOF on client connection" /var/log/postgresql/*.log | tail -5Confirmed by docs.docker.com
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.