Skip to content

Reject exception doesn't cause the task to fail #4222

Closed

Description

@pitervergara

Hi!

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

from celery import task, Task
from celery.exceptions import Reject

@task(name='test_reject')
def test_reject():
    raise Reject(reason="Some business logic...", requeue=False)
>>> from mymodule.tasks import *
>>> test_reject.delay()
<AsyncResult: 712bed3e-39e1-4455-b1f6-445aa37ef236>

Expected behavior

I would expect the task to go into FAILURE state

Actual behavior

Task still pending

>>> from celery.result import AsyncResult
>>> res = AsyncResult('712bed3e-39e1-4455-b1f6-445aa37ef236')
>>> res.status
'PENDING'
>>> res.state
'PENDING'
>>> res.failed()
False
>>> res.successful()
False

captura de tela de 2017-08-22 11-06-21

Activity

  1. pea1ce commented on Oct 13, 2017

    @pea1ce

    If the task is rejected by all workers, the AsyncResult still says PENDING?

  2. pitervergara commented on Nov 1, 2017

    @pitervergara
    Author

    I'm not sure. I'll try to check it.

  3. pitervergara commented on Nov 28, 2017

    @pitervergara
    Author

    Hello,
    I have taken down the extra workers. Even when running only one, I get the same results.

    celery

  4. KBoehme commented on Jan 10, 2018

    @KBoehme

    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?

  5. auvipy commented on Jan 15, 2018

    @auvipy
    Member

    does this happen in 4.1/master?

  6. ifoukarakis commented on Feb 2, 2018

    @ifoukarakis

    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.

  7. georgepsarakis commented on Feb 3, 2018

    @georgepsarakis
    Contributor

    @pitervergara can you share your Celery configuration, mostly the broker you are using (RabbitMQ, Redis etc) ?

  8. ifoukarakis commented on Feb 3, 2018

    @ifoukarakis

    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:

    from celery import Celery
    from celery.exceptions import Reject
    from celery.signals import task_rejected, task_retry, task_failure
    
    celery = Celery(broker='amqp://guest:guest@localhost:5672//', backend='rpc://')
    
    def on_failure(*args, **kwargs):
        print('==> Failure')
    
    def on_reject(*args, **kwargs):
        print('==> Reject')
    
    def on_retry(*args, **kwargs):
        print('==> Retry')
    
    task_failure.connect(on_failure)
    task_retry.connect(on_retry)
    task_rejected.connect(on_reject)
    
    @celery.task(name='test_reject')
    def test_reject():
        raise Reject(reason="Some business logic...", requeue=False)
    

    and an example using ipython:

    In [1]: from worker import test_reject
    
    In [2]: res = test_reject.apply_async()
    
    In [3]: res.status
    Out[3]: 'PENDING'
    

    Please note that when test_reject.apply_async() is called, no signals are dispatched.

  9. sailtwist commented on May 27, 2018

    @sailtwist

    Confirming (celery==4.1.1) that any exception does not put task in failed state.

    from celery import Celery
    
    app = Celery('wip',broker='amqp://localhost', backend='redis://')
    
    @app.task(bind=True, ignore_result=False)
    def add_throws(self,x,y):
        raise Exception('Forced exception')
        return x + y
    

    calling example:

    >>>r = add_throws.s(1,1).delay()
    >>>r.ready()
    False
    >>>r.state
    'PENDING'
    >>>r.failed()
    False
    >>>r.result
    >>>r.traceback
    
  10. added this to the v4.3 milestone on May 28, 2018
  11. modified the milestones: v4.3, v5.0.0 on Nov 17, 2018
  12. 9 remaining items

  13. modified the milestones: 4.5, 5.2 on Feb 25, 2021
  14. modified the milestones: 5.2, 5.2.x on Oct 30, 2021
  15. modified the milestones: 5.2.x, 5.3.x on Jun 29, 2022
  16. haseebnaseem commented on Feb 14, 2024

    @haseebnaseem

    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.

  17. akarnavasco commented on Feb 14, 2024

    @akarnavasco

    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']}") 
    
  18. modified the milestones: 5.3.x, 5.5 on Jun 25, 2024
  19. modified the milestones: 5.5.0, 5.7.0 on Jun 12, 2025
  20. auvipy commented on Jun 27, 2026

    @auvipy
    Member

    we got a proposed fix for this, feel free to check and try it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions