You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Reject exception doesn't cause the task to fail #4222
I'm seeing the behaviour described in #2944.
If I reject a task, it is kept as STARTED. I'm not sure if did not understand the solution to that issue - and this is the expected behaviuor - or if this is a regression.
I have verified that the behaviour is the same in the master branch of Celery (installed from commit bda678b).
Steps to reproduce
fromceleryimporttask, Taskfromcelery.exceptionsimportReject@task(name='test_reject')deftest_reject():
raiseReject(reason="Some business logic...", requeue=False)
>>> from mymodule.tasks import *
>>> test_reject.delay()
<AsyncResult: 712bed3e-39e1-4455-b1f6-445aa37ef236>
Reacted by Ashon, jason_dev315, Francesco, Tord Johan Espe, Adnan Abdulmuttaleb, Alexandre Rosenfeld, Alexey Trofimov, Rob Percival, guillaumedsde, J. Wolf Oberholtzer and 8 more
I am seeing this behavior as well celery (4.0.2). Further it seems when a celery.exceptions.Reject or celery.exceptions.Retry exception are thrown in a task none of the Task on_failure(), on_success(), on_retry(), or even after_return() handlers run. I would expect the after_return() handler to at least run... I guess celery is handling these exceptions differently to cause this discrepancy?
I'm seeing the same behavior on 4.1.0 when trying with Reject. Alsotask_failure signal is also not dispatched (neither task_retry or task_rejected). I am not sure which would be the proper signal to be dispatched in this situation.
Has anyone found a workaround for this? My use case is that I want to reject the task so the messages ends up in a deadletter queue - the message does go to the dlq but the task remains in a 'STARTED' state. I have even tried forcefully setting the state before rejecting the task but it does not seem to work either.
As a workaround I overwrite the on_failure method to forward the message to the DLQ.
Have a look here.
Here is the class I use.
class PersistOnFailureTask(Task, ABC):
"""
A task should extend this class in order to forward failing tasks to a dead letter queue
"""
abstract = True
def on_failure(self, exc, task_id, args, kwargs, einfo):
delivery_info = self.request.delivery_info
self.apply_async(args, kwargs, queue=f"dead_letter_{delivery_info['routing_key']}")
Activity
pea1ce commentedon Oct 13, 2017
If the task is rejected by all workers, the AsyncResult still says PENDING?
pitervergara commentedon Nov 1, 2017
I'm not sure. I'll try to check it.
pitervergara commentedon Nov 28, 2017
Hello,
I have taken down the extra workers. Even when running only one, I get the same results.
KBoehme commentedon Jan 10, 2018
I am seeing this behavior as well
celery (4.0.2). Further it seems when a celery.exceptions.Reject or celery.exceptions.Retry exception are thrown in a task none of the Task on_failure(), on_success(), on_retry(), or even after_return() handlers run. I would expect the after_return() handler to at least run... I guess celery is handling these exceptions differently to cause this discrepancy?auvipy commentedon Jan 15, 2018
does this happen in 4.1/master?
ifoukarakis commentedon Feb 2, 2018
I'm seeing the same behavior on 4.1.0 when trying with
Reject. Alsotask_failuresignal is also not dispatched (neithertask_retryortask_rejected). I am not sure which would be the proper signal to be dispatched in this situation.georgepsarakis commentedon Feb 3, 2018
@pitervergara can you share your Celery configuration, mostly the broker you are using (RabbitMQ, Redis etc) ?
ifoukarakis commentedon Feb 3, 2018
Perhaps I'm able to help. I reproduced this using RabbitMQ as broker and backend. Dependency is
celery==4.1.0. Here's the worker's code:and an example using ipython:
Please note that when
test_reject.apply_async()is called, no signals are dispatched.sailtwist commentedon May 27, 2018
Confirming (celery==4.1.1) that any exception does not put task in failed state.
calling example:
9 remaining items
haseebnaseem commentedon Feb 14, 2024
Has anyone found a workaround for this? My use case is that I want to reject the task so the messages ends up in a deadletter queue - the message does go to the dlq but the task remains in a 'STARTED' state. I have even tried forcefully setting the state before rejecting the task but it does not seem to work either.
akarnavasco commentedon Feb 14, 2024
As a workaround I overwrite the
on_failuremethod to forward the message to the DLQ.Have a look here.
Here is the class I use.
auvipy commentedon Jun 27, 2026
we got a proposed fix for this, feel free to check and try it.