Repository navigation
Description
A hard time limit runs the failure handling twice. The link_error errback is called twice, and since 5.7.0 (#10226) the task_failure signal and the task-failed event are sent twice too. The double errback is old: it dates from 4.0.0 and is the same on 5.4.0 and 5.5.3. I'd have on_timeout() skip what on_failure() already did for the same limit, and I can send that PR.
Prefork, a 10 s task with time_limit=2 |
Errback calls | task_failure signals |
|---|---|---|
| Main (9c2f238) | 2 | 2, in 3 of 3 runs |
| 5.5.3 and 5.4.0 | 2 | 1, in 2 of 2 runs each |
| A task that raises, main | 1 | 1 |
Cause
On a hard time limit billiard first sets the job's result to TimeLimitExceeded, which calls Request.on_failure(), and then calls Request.on_timeout() (billiard/pool.py:747 and :755, 4.3.2). Both store FAILURE through mark_as_failure(), which calls the errbacks (celery/worker/request.py:552).
on_timeout()has stored the failure since 2.1.0, andmark_as_failure()has called errbacks since 4.0.0 (e6fb534, 2015). That is where the second errback call comes from.- fix: send the task failure in hard timeout #10226 added a
task_failuresignal toon_timeout()(request.py:574), which is the second signal since 5.7.0. - With
task_acks_on_failure_or_timeout = False,on_failure()requeues the message and doesn't store a state, andon_timeout()still stores FAILURE and calls the errback.
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
- fix: send the task failure in hard timeout #10226 added the
task_failuresignal for a hard time limit. - Request on_timeout should ignore soft time limit exception #4412 is about
on_timeout()and soft time limits.
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:redis://localhost:6379/1
broker_url: 'redis://:********@localhost:6379/0'
result_backend: 'redis://localhost:6379/1'
deprecated_settings: None
Steps to Reproduce
Required Dependencies
- Minimal Python Version: N/A or Unknown
- Minimal Celery Version: 5.4.0 tested for the errback (4.0.0 by the code), 5.7.0 for the signal
- Minimal Kombu Version: N/A or Unknown
- Minimal Broker Version: Redis 7.4
- Minimal Result Backend Version: Redis 7.4
- 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 os
import time
import redis
from celery import Celery
from celery.signals import task_failure
app = Celery("tasks", broker="redis://localhost:6379/0",
backend="redis://localhost:6379/1")
if os.environ.get("NO_ACK_ON_TIMEOUT"):
app.conf.task_acks_late = True
app.conf.task_acks_on_failure_or_timeout = False
log = redis.Redis(db=2)
@task_failure.connect
def count_signal(sender=None, **kwargs):
log.rpush("calls", "task_failure signal")
@app.task
def errback(request, exc, traceback):
log.rpush("calls", "errback")
@app.task(bind=True, time_limit=2)
def slow(self):
log.rpush("calls", "run")
if not (self.request.delivery_info or {}).get("redelivered"):
time.sleep(10)
@app.task
def fails():
log.rpush("calls", "run")
raise ValueError("boom")# run.py
import sys
import time
from collections import Counter
from tasks import errback, fails, log, slow
log.delete("calls")
task = fails if sys.argv[1:] == ["raise"] else slow
res = task.apply_async(link_error=errback.s())
time.sleep(8)
print(dict(Counter(c.decode() for c in log.lrange("calls", 0, -1))), "| state:", res.state)$ celery -A tasks worker -c 1 -l warning
$ python run.py
{'run': 1, 'errback': 2, 'task_failure signal': 2} | state: FAILURE
$ python run.py raise
{'run': 1, 'errback': 1, 'task_failure signal': 1} | state: FAILURE
$ NO_ACK_ON_TIMEOUT=1 celery -A tasks worker -c 1 -l warning
$ NO_ACK_ON_TIMEOUT=1 python run.py
{'run': 2, 'errback': 1, 'task_failure signal': 1} | state: SUCCESSExpected Behavior
One errback call, one task_failure signal and one task-failed event for a task that is killed by its hard time limit.
Actual Behavior
Two of each on main. With task_acks_late and task_acks_on_failure_or_timeout = False the task is also stored as FAILURE with its errback called, although its message is requeued and the next run ends SUCCESS (3 of 3 runs).
Activity
Add tests requested in review for hard time limit handling
Do not run failure handling twice for a hard time limit (#11105)