Repository navigation
Description
Summary
Since 4.3.0, a pool worker catches the SystemExit that billiard's own shutdown handler raises and reports it as the task's result. Celery then treats the interrupted task as finished-with-failure and acknowledges its message, so a cold shutdown (SIGQUIT) silently discards in-flight work instead of returning it to the queue. With task_acks_late=True this is data loss on every restart.
Bisected to bd2f803 ("Propagate BaseException raised in pool workers", #452, closing #427). Parent 001b9f0 is good, bd2f803 is bad, and both v4.2.4 and v4.3.0 reproduce from source.
Reproduction
Needs a local Redis. celery==5.5.3, kombu==5.5.4, Python 3.12.
app.py:
import os
import time
from celery import Celery
app = Celery("repro", broker=os.environ["BROKER"])
app.conf.update(task_acks_late=True, worker_prefetch_multiplier=1,
result_backend=None, task_ignore_result=True)
@app.task(name="repro.slow")
def slow(marker):
with open(marker, "w") as fh:
fh.write("started")
time.sleep(300)run.py publishes one task, waits until the worker is executing it, sends SIGQUIT, and reports what the broker still holds:
import os
import signal
import subprocess
import sys
import time
os.environ.setdefault("BROKER", "redis://127.0.0.1:6379/15")
BROKER, QUEUE, MARKER = os.environ["BROKER"], "repro-cold-stop", "/tmp/repro-marker"
import redis
from app import slow
client = redis.Redis.from_url(BROKER)
for key in (QUEUE, f"_kombu.binding.{QUEUE}", "unacked", "unacked_index"):
client.delete(key)
if os.path.exists(MARKER):
os.remove(MARKER)
slow.apply_async(args=[MARKER], queue=QUEUE)
worker = subprocess.Popen(
[sys.executable, "-m", "celery", "-A", "app", "worker", "-Q", QUEUE,
"-c", "1", "--without-gossip", "--without-mingle", "--without-heartbeat", "-l", "error"],
cwd=os.path.dirname(os.path.abspath(__file__)), start_new_session=True,
)
for _ in range(60):
if os.path.exists(MARKER):
break
time.sleep(1)
else:
raise SystemExit("worker never started the task")
assert client.llen(QUEUE) == 0, "task should be claimed, not sitting on the queue"
worker.send_signal(signal.SIGQUIT)
worker.wait(timeout=30)
time.sleep(2)
import billiard
print(f"billiard {billiard.__version__}: ready_queue={client.llen(QUEUE)} "
f"unacked={client.hlen('unacked')} unacked_index={client.zcard('unacked_index')}")Result
billiard 4.2.4: ready_queue=1 unacked=0 unacked_index=0
billiard 4.3.0: ready_queue=0 unacked=0 unacked_index=0
On 4.2.4 the message is back on the queue and the task runs again on the next worker. On 4.3.0 it is on neither the ready queue nor the unacked hash: it is gone. Not delayed until the visibility timeout — acknowledged and destroyed.
4.3.0 also prints the mechanism directly, since the exception being marshalled is raised by billiard's own signal handling:
Traceback (most recent call last):
File ".../billiard/pool.py", line 363, in workloop
result = (True, prepare_result(fun(*args, **kwargs)))
File ".../celery/app/trace.py", line 651, in fast_trace_task
...
File ".../repro/app.py", line 16, in slow
time.sleep(300)
File ".../billiard/common.py", line 106, in _shutdown_cleanup
sys.exit(-(256 - signum))
File ".../billiard/pool.py", line 283, in exit
return _exit(status)
SystemExit: -241
Cause
try:
result = (True, prepare_result(fun(*args, **kwargs)))
- except Exception:
+ except BaseException:
result = (False, ExceptionInfo())
try:
put((READY, (job, i, result, inqW_fd)))_shutdown_cleanup raises SystemExit inside the worker to tear it down. Before this change that escaped the handler, the worker unwound and died with its task unacknowledged, and kombu's QoS.restore_unacked_once() put the message back. Now it is caught, packaged as ExceptionInfo(), and reported READY — an ordinary task failure, as far as everything downstream can tell.
The follow-up in the same release does not cover this
b1bb092 ("Exit the pool worker after a task raises SystemExit or KeyboardInterrupt") appears aimed at this, but 4.3.0 still reproduces. It re-raises after put((READY, ...)):
result = (False, ExceptionInfo())
if isinstance(exc, (SystemExit, KeyboardInterrupt)):
exit_exc = exc
...
put((READY, (job, i, result, inqW_fd)))
...
if exit_exc is not None:
raise exit_excThe result has already been reported and the message already acknowledged by the time the worker leaves, so exiting afterwards does not take it back.
Note on the underlying request
#427 asked for a clearer error than WorkerLostError when a task itself raises BaseException. That is a real gap, but at this line the two cases are indistinguishable: a task raising SystemExit and billiard telling the worker to exit arrive identically. Any fix that keeps #427 working probably needs to tell them apart before the result is put on the queue — for instance by having the shutdown path mark the worker as terminating, so the handler can decline to report a result for it — rather than by widening the except clause. I am happy to test a patch against the reproduction above.
For now we have pinned billiard==4.2.4.
Activity
auvipy commentedon Sep 23, 2026
please use celery 5.7.0a1 for testing this with kombu 5.7.0a1
Do not report a shutdown SystemExit as the job's result (celery#464)