Repository navigation
Description
A prefork worker that is stopped with SIGQUIT never sends the worker_shutdown signal. With REMAP_SIGTERM=SIGQUIT that is every stop, so a handler that flushes metrics or closes a connection there never runs. It has been this way since 4.0.0 and is the same on 5.4.0 and 5.5.3. I'd send the signal from terminate() too, and I can send that PR.
| Main (9c2f238), prefork, sent to an idle worker | worker_shutting_down |
worker_shutdown |
|---|---|---|
| SIGTERM | Warm | Sent |
| SIGQUIT | Cold | Not sent |
SIGTERM with REMAP_SIGTERM=SIGQUIT |
Cold | Not sent |
| SIGTERM, then SIGQUIT | Warm, then Cold | Sent |
| SIGINT twice | Warm, then Cold | Sent |
Each row is 3 of 3 runs. It is the same while a task runs.
Cause
WorkController.stop() ends by sending the signal (celery/worker/worker.py:320). terminate() doesn't (worker.py:322). SIGQUIT raises WorkerTerminate, and start() answers that with terminate() alone (worker.py:272). 676a8b0 (2016, 4.0.0) moved the signal from a handler that ran at exit into stop() only.
The exit code is 0 in the SIGQUIT rows as well, although the handler raises WorkerTerminate(EX_FAILURE) (celery/apps/worker.py:382). I don't know if that is intended.
The one argument I see against the change is that a slow handler would then hold up a cold shutdown.
Checklist
Checklist
- I have verified that the issue exists against the
mainbranch of Celery. - This has already been asked to the discussions forum first.
- I have read the relevant section in the
contribution guide
on reporting bugs. - I have checked the issues list
for similar or identical bug reports. - I have checked the pull requests list
for existing proposed fixes. - I have checked the commit log
to find out if the bug was already fixed in the main branch. - I have included all related issues and possible duplicate issues in this issue (If there are none, check this box anyway).
- I have tried to reproduce the issue with pytest-celery and added the reproduction script below.
Mandatory Debugging Information
- I have included the output of
celery -A proj reportin the issue. - I have verified that the issue exists against the
mainbranch of Celery. - I have included the contents of
pip freezein the issue. - I have included all the versions of all the external dependencies required to reproduce this bug.
Optional Debugging Information
- I have tried reproducing the issue on more than one Python version and/or implementation.
- I have tried reproducing the issue on more than one message broker and/or result backend.
- I have tried reproducing the issue on more than one version of the message broker and/or result backend.
- I have tried reproducing the issue on more than one operating system.
- I have tried reproducing the issue on more than one workers pool.
- I have tried reproducing the issue with autoscaling, retries, ETA/Countdown & rate limits disabled.
- I have tried reproducing the issue after downgrading and/or upgrading Celery and its dependencies.
Related Issues and Possible Duplicates
Related Issues
- Worker Shutdown Signal Never Sent #1339 from 2013, "Worker Shutdown Signal Never Sent", is closed. Its fix was the exit handler that 676a8b0 replaced.
Possible Duplicates
- None
Environment & Settings
Celery version: 5.7.0 (9c2f238)
celery report Output:
software -> celery:5.7.0 (collider) kombu:5.7.0 py:3.14.6
billiard:4.3.2 redis:8.1.0
platform -> system:Linux arch:64bit, ELF
kernel version:6.18.44-fc-v378 imp:CPython
loader -> celery.loaders.app.AppLoader
settings -> transport:redis results:disabled
broker_url: 'redis://:********@localhost:6379/0'
deprecated_settings: None
Steps to Reproduce
Required Dependencies
- Minimal Python Version: N/A or Unknown
- Minimal Celery Version: 5.4.0 tested (4.0.0 by the code)
- Minimal Kombu Version: N/A or Unknown
- Minimal Broker Version: Redis 7.4
- Minimal Result Backend Version: N/A or Unknown
- Minimal OS and/or Kernel Version: N/A or Unknown
- Minimal Broker Client Version: N/A or Unknown
- Minimal Result Backend Client Version: N/A or Unknown
Python Packages
pip freeze Output:
billiard==4.3.2
celery @ main (9c2f238)
kombu @ main (445027e)
redis==8.1.0
Other Dependencies
Services
N/A
Minimally Reproducible Test Case
tasks.py, run.py and output
# tasks.py
import sys
from celery import Celery, signals
app = Celery("tasks", broker="redis://localhost:6379/0")
@signals.worker_shutting_down.connect
def shutting_down(sig=None, how=None, **kwargs):
sys.__stdout__.write(f"worker_shutting_down ({how})\n")
@signals.worker_shutdown.connect
def shutdown(**kwargs):
sys.__stdout__.write("worker_shutdown\n")# run.py
import signal
import subprocess
import sys
import time
worker = subprocess.Popen(
[sys.executable, "-m", "celery", "-A", "tasks", "worker", "-l", "error"],
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True)
time.sleep(6) # an idle worker
for name in sys.argv[1:]:
worker.send_signal(getattr(signal, name))
time.sleep(1)
out, _ = worker.communicate()
signals = [line for line in out.splitlines() if line.startswith("worker_")]
print(" ".join(sys.argv[1:]), "->", ", ".join(signals),
"| exit code", worker.returncode)$ python run.py SIGTERM
SIGTERM -> worker_shutting_down (Warm), worker_shutdown | exit code 0
$ python run.py SIGQUIT
SIGQUIT -> worker_shutting_down (Cold) | exit code 0
$ REMAP_SIGTERM=SIGQUIT python run.py SIGTERM
SIGTERM -> worker_shutting_down (Cold) | exit code 0
$ python run.py SIGTERM SIGQUIT
SIGTERM SIGQUIT -> worker_shutting_down (Warm), worker_shutting_down (Cold), worker_shutdown | exit code 0
$ python run.py SIGINT SIGINT
SIGINT SIGINT -> worker_shutting_down (Warm), worker_shutting_down (Cold), worker_shutdown | exit code 1Expected Behavior
worker_shutdown is sent on a cold shutdown too. The signals guide says it is "Dispatched when the worker is about to shut down".
Actual Behavior
It is only sent when a warm shutdown came first.
Activity
aniketatgithub commentedon Nov 14, 2026
I can reproduce this — I'll open a PR with a fix.