Skip to content

chord on_error callback does not work as documented #3709

Closed

Description

@NickStefan

Checklist

Steps to reproduce

file1.py

from celery import chord, group
from app.graph.tasks import on_chord_error, add, xsum

c = (group(add.s(i, i) for i in range(1)) | xsum.s().on_error(on_chord_error.s())).delay()

tasks.py

@shared_task
def add(x, y):
    raise Exception("bob")

@shared_task
def xsum(numbers):
    return sum(numbers)

@shared_task
def on_chord_error(request, exc, traceback):
    print('calling this random error method!!!!')
    print('Task {0!r} raised error: {1!r}'.format(request.id, exc))

Expected behavior

That the on_chord_error callback would be invoked and would print out "calling this random error method"

Actual behavior

Whether I define an on_error callback or not (e.g. c = (group(add.s(i, i) for i in range(1)) | xsum.s().on_error(on_chord_error.s())).delay() versus c = (group(add.s(i, i) for i in range(1)) | xsum.s()).delay() , i get this same output:

output

django      | [2016-12-20 23:32:14,688: INFO/MainProcess] Received task: app.graph.tasks.add[c5eeb13a-21f6-4f15-ac22-eab0784d2806]
django      | [2016-12-20 23:32:16,068: ERROR/PoolWorker-2] Task app.graph.tasks.add[c5eeb13a-21f6-4f15-ac22-eab0784d2806] raised unexpected: Exception('bob',)
django      | Traceback (most recent call last):
django      |   File "/usr/local/lib/python3.5/site-packages/celery/app/trace.py", line 367, in trace_task
django      |     R = retval = fun(*args, **kwargs)
django      |   File "/usr/local/lib/python3.5/site-packages/celery/app/trace.py", line 622, in __protected_call__
django      |     return self.run(*args, **kwargs)
django      |   File "/usr/src/app/graph/tasks.py", line 44, in add
django      |     raise Exception("bob")
django      | Exception: bob

It never calls the on_error handler.

normal circumstances work:

If I rewrite add from

@shared_task
def add(x, y):
    raise Exception("bob")

to this:

@shared_task
def add(x, y):
    return x + y

then everything works. So I believe one of three things is going on:

  • the documentation regarding group / chord error handling is incorrect
  • chord / group error handling does not work
  • i'm misinterpreting how error handling is supposed to work in celery (i have scoured the internet for good examples, but i cannot find any good examples of people propagating errors from a group of subprocesses. I would think this would be a very common thing!)

What is the celery equivalent of something like this from Node.js land?

var async = require('async');
async.auto({
     thing1: function(){},
     thing2: function(){}
}, function(err, results) {
    if (err) {
         // do stuff!
   }
   // do normal stuff post batch of stuff
})

Activity

  1. NickStefan commented on Dec 21, 2016

    @NickStefan
    Author

    I am able to trigger the on_chord_error callback if I change add
    from this

    @shared_task(bind=True)
    def add(self, x, y):
        raise Exception('bob')
    

    to this

    @shared_task(bind=True)
    def add(self, x, y):
        self.update_state(state="FAILURE")
    

    However, it doesnt call the on error callback with the correct arguments, AND it still calls the xsum method.

    I still think this issue either points to missing documentation or a bug

  2. NickStefan commented on Dec 21, 2016

    @NickStefan
    Author

    I found the issue! This is definitely related #3574

  3. NickStefan commented on Dec 21, 2016

    @NickStefan
    Author

    #3597 also

  4. NickStefan commented on Dec 21, 2016

    @NickStefan
    Author

    the code from here #3323 seems to be the cause

  5. NickStefan commented on Dec 21, 2016

    @NickStefan
    Author

    callback and callback error doesn't work with one task (callback never called):

            callback = xsum.s().on_error(do_on_error.s())
            tasks = [ add.s(4, 4) ]
            c = chord(tasks)(callback)
    

    callback and callback error does work with two tasks

            callback = xsum.s().on_error(do_on_error.s())
            tasks = [ add.s(4, 4), add.s(4, 5) ]
            c = chord(tasks)(callback)
    
  6. br1anjtuck commented on Jan 30, 2017

    @br1anjtuck

    Any new progress on this issue? I ran into the same issue that @NickStefan experienced, and unless I am misinterpreting the documentation, it does seem like the callback should be called for proper chord error handling

  7. NickStefan commented on Jan 30, 2017

    @NickStefan
    Author

    @br1anjtuck we just made some helpers that always add a blank task when using chords (to ensure there are always at least 2 tasks)

    @shared_task(bind=True)
    def celery_bug_fix(self, *args, **kwargs):
        '''
        celery chords only correctly handle errors with at least 2 tasks, 
        so we always append a celery_bug_fix task
        https://github.com/celery/celery/issues/3709
        '''
        pass
    
    
    
  8. br1anjtuck commented on Jan 31, 2017

    @br1anjtuck

    Ok. So with the added helper task above, are you getting the user defined callback (your "do_on_error" task) to occur or are you getting a simple Chod Callback? (i.e. something like below):

    Chord callback for '599a6689-3737-4e92-b774-9c4b56885fea' raised: ChordError(u"Dependency ec44e5c6-1396-44d9-acf7-86bdffaf0a10 raised RuntimeError('BLAH',)",)
    
    Traceback (most recent call last):
      File "/usr/lib/python2.7/site-packages/celery/backends/redis.py", line 281, in on_chord_part_return
        callback.delay([unpack(tup, decode) for tup in resl])
      File "/usr/lib/python2.7/site-packages/celery/backends/redis.py", line 234, in _unpack_chord_result
        raise ChordError('Dependency {0} raised {1!r}'.format(tid, retval))
    ChordError: Dependency ec44e5c6-1396-44d9-acf7-86bdffaf0a10 raised RuntimeError('BLAH',)",)
    

    I am only getting the above chord callback error and I am not getting my defined callback error "on_chord_error" when I define the SAME tasks that you have from your first cell of this post, and run a group of to tasks chained to the "xsum" callback (exactly like you did in the second part of the cell before I first posted) :

    @shared_task
    def on_chord_error(request, exc, traceback):
        print('\nTask {0!r} raised error: {1!r}\n'.format(request.id, exc))
    

    I am supposed to getting this "on_chord_error" callback error, right? Or should it only be the Chord callback?

  9. NickStefan commented on Jan 31, 2017

    @NickStefan
    Author

    yes, i am getting the user defined callback when I always make sure to add a task (to ensure theres always at least two)

           callback = xsum.s().on_error(do_on_error.s())
            tasks = [ add.s(4, 4), celery_bug_fix.s() ]
            c = chord(tasks)(callback)
    
  10. br1anjtuck commented on Jan 31, 2017

    @br1anjtuck

    Hmm..interesting. I don't know why, but with the same code, I am only getting the Chord callback and am not getting the user-defined callback. The only time I get the user-defined callback error is when I raise an exception in the "xsum" task rather than in the "add" task.

  11. br1anjtuck commented on Jan 31, 2017

    @br1anjtuck

    Are you doing the below instead of raising an exception in your task?:

    @shared_task(bind=True)
    def add(self, x, y):
        self.update_state(state="FAILURE")
    
  12. NickStefan commented on Feb 1, 2017

    @NickStefan
    Author

    Just doing regular exceptions. It was definitely tricky to get working, but yes an exception in add task is triggering the error callback

  13. tlong-dev commented on May 12, 2017

    @tlong-dev

    Yeah, I'm experiencing this as well. Seems to also be reported in #3317

  14. rholloway commented on May 30, 2017

    @rholloway

    @br1anjtuck did you ever figure out a solution to your problem? I seem to have the same situation as you with 4.0.2. My custom callback is called if the exception occurs in the chord callback (xsum), but if it occurs in individual task (add) it's never called.

    I've tried sticking on_error onto each task in the header, onto the chord callback, onto the entire chord, everywhere. No luck.

  15. chrisbrantley commented on Jun 20, 2017

    @chrisbrantley

    I'm having the same problem. The on_error is simply never called. It doesn't seem to matter how many tasks I have in the group. Very frustrating.

  16. 63 remaining items

  17. Miraliu commented on Oct 5, 2023

    @Miraliu

    Ok after some checks the documentation seems correct for unbound tasks, but not for bound tasks.

    1 -------------- The code handles 2 incompatible argument schemes, it can either pass request, exc, traceback or request_id to callbacks. It may be worth mentionning in the documentation?

    The following declarations are compatible with on_error(on_chord_error.s()):

    • def on_chord_error(request, exc, traceback)
    • def on_chord_error(request_id).

    The following declarations are compatible with: on_error(on_chord_error.s(123)), :

    • def on_chord_error(request, exc, traceback, custom).
    • def on_chord_error(request_id, custom):

    2 -------------- If I use bind=True :

    • def on_chord_error(self, request, exc, traceback) is KO (TypeError: on_chord_error() missing 2 required positional arguments: 'exc' and 'traceback')
    • def on_chord_error(self, request_id, custom) is OK

    Same thing happens if I add a custom argument. I am just learning Celery so it can be a mistake, but 2nd point looks like a bug.

    3 -------------- For some reason if I process a group with more than 2 items, chord never finishes (with celery 5.1.2, rabbitMQ 3.9.9, Mongo 5.0.4) (I use amqp://...... rabbitmq broker and mongodb://... result backend) There is an open ticket (#4701) but the logs are not the same and there is no warning in documentation showing that chords implementation is not compatible yet with mongodb result backend.

    Typical log:

    [2022-01-24 12:12:51,666: INFO/MainProcess] celery@worker1 ready.
    [2022-01-24 12:12:53,762: INFO/MainProcess] Task celery.chord_unlock[e2d97e9e-1747-429d-aa01-65724ba6bfcd] received
    [2022-01-24 12:12:53,762: INFO/MainProcess] Task test_celery.add[fb83685b-38b9-4baa-8006-4758fc4e2d64] received
    [2022-01-24 12:12:53,763: INFO/MainProcess] Task test_celery.add[db1cde84-1431-4ea2-9786-7de0f1b81d6c] received
    [2022-01-24 12:12:53,786: INFO/ForkPoolWorker-16] Task test_celery.add[db1cde84-1431-4ea2-9786-7de0f1b81d6c] succeeded in 0.02320910000707954s: 2
    [2022-01-24 12:12:53,786: INFO/ForkPoolWorker-15] Task test_celery.add[fb83685b-38b9-4baa-8006-4758fc4e2d64] succeeded in 0.0232757999910973s: 0
    [2022-01-24 12:12:55,685: INFO/ForkPoolWorker-15] Task celery.chord_unlock[e2d97e9e-1747-429d-aa01-65724ba6bfcd] retry: Retry in 1.0s
    [2022-01-24 12:12:55,686: INFO/MainProcess] Task celery.chord_unlock[e2d97e9e-1747-429d-aa01-65724ba6bfcd] received
    [2022-01-24 12:12:57,676: INFO/ForkPoolWorker-15] Task celery.chord_unlock[e2d97e9e-1747-429d-aa01-65724ba6bfcd] retry: Retry in 1.0s
    [2022-01-24 12:12:57,677: INFO/MainProcess] Task celery.chord_unlock[e2d97e9e-1747-429d-aa01-65724ba6bfcd] received
    [2022-01-24 12:12:59,679: INFO/ForkPoolWorker-15] Task celery.chord_unlock[e2d97e9e-1747-429d-aa01-65724ba6bfcd] retry: Retry in 1.0s
    [2022-01-24 12:12:59,680: INFO/MainProcess] Task celery.chord_unlock[e2d97e9e-1747-429d-aa01-65724ba6bfcd] received
    [2022-01-24 12:13:01,657: INFO/ForkPoolWorker-15] Task celery.chord_unlock[e2d97e9e-1747-429d-aa01-65724ba6bfcd] retry: Retry in 1.0s
    [2022-01-24 12:13:01,658: INFO/MainProcess] Task celery.chord_unlock[e2d97e9e-1747-429d-aa01-65724ba6bfcd] received
    [2022-01-24 12:13:03,682: INFO/ForkPoolWorker-15] Task celery.chord_unlock[e2d97e9e-1747-429d-aa01-65724ba6bfcd] retry: Retry in 1.0s
    [2022-01-24 12:13:03,683: INFO/MainProcess] Task celery.chord_unlock[e2d97e9e-1747-429d-aa01-65724ba6bfcd] received
    [2022-01-24 12:13:05,684: INFO/ForkPoolWorker-15] Task celery.chord_unlock[e2d97e9e-1747-429d-aa01-65724ba6bfcd] retry: Retry in 1.0s
    [2022-01-24 12:13:05,685: INFO/MainProcess] Task celery.chord_unlock[e2d97e9e-1747-429d-aa01-65724ba6bfcd] received
    ... (infinite loop)
    

    celery==5.3.1
    def on_chord_error(request_id, custom) no longer works for us...the callback is not invoked.
    def on_chord_error(request, exc, traceback, custom) works.

  18. added this to the 5.3.x milestone on Oct 22, 2023
  19. modified the milestones: 5.3.x, 5.4 on Nov 13, 2023
  20. removed this from v5.3on Nov 23, 2023
  21. modified the milestones: 5.4, 5.5 on Jun 25, 2024
  22. removed this from v5.4on Jun 10, 2025
  23. modified the milestones: 5.5.0, 5.7.0 on Jun 10, 2025
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