Repository navigation
Description
I created a test case to reproduce the problem I suffer from in more complex workflow. I expect link_error of the chord callback to be executed every time an error occurs in chord header. In provided example chord header is a group, which consists of one chain. However, in my implementation group can contain multiple chains and/or tasks.
My celery version is 3.1.23.
test.py:
from celery import Celery, chain, group, chord
app = Celery('test', backend='redis', broker='redis://localhost')
@app.task
def task1():
print("Task 1 executed")
@app.task
def error_task():
raise Exception("Mocked exception")
@app.task
def callback_task():
print("Callback executed")
@app.task
def link_error_task(*args, **kwargs):
print("Link error executed")
@app.task
def link_task():
print("Link executed")
def main():
c1 = chain(error_task.si(), task1.si())
head = group([c1])
callback = callback_task.si().set(link=[link_task], link_error=[link_error_task.s()])
chord(head, callback).delay()
I expect following things to happen:
-
Celery worker starts executing tasks in chain c1.
-
After reaching exception in error_task celery stops executing chain c1.
-
The error from chord header is propagated to the chord callback. As a result, callback execution will be marked as failed and finally link_error of the callback will be executed.
To execute this code I run following commands:
-
Terminal 1:
$ celery -A test worker --app=test:app --loglevel=info -
Terminal 2:
$ python Python 2.7.12 (default, Jun 29 2016, 14:05:02) [GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import test >>> test.main()
The celery output will be following:
[2016-07-14 21:39:36,186: INFO/MainProcess] Received task: test.error_task[b9e7d006-43f9-4e9d-8391-ada74fb3bc4e]
[2016-07-14 21:39:36,193: ERROR/MainProcess] Task test.error_task[b9e7d006-43f9-4e9d-8391-ada74fb3bc4e] raised unexpected: Exception('Mocked exception',)
Traceback (most recent call last):
File "/private/tmp/venv/lib/python2.7/site-packages/celery/app/trace.py", line 240, in trace_task
R = retval = fun(*args, **kwargs)
File "/private/tmp/venv/lib/python2.7/site-packages/celery/app/trace.py", line 438, in __protected_call__
return self.run(*args, **kwargs)
File "/private/tmp/test.py", line 11, in error_task
raise Exception("Mocked exception")
Exception: Mocked exception
As we can see link_error was not executed. However, if we remove last task in c1, the program will work as expected:
def main():
c1 = chain(error_task.si()) # last task removed
head = group([c1])
callback = callback_task.si().set(link=[link_task], link_error=[link_error_task.s()])
chord(head, callback).delay()
The output will be following:
[2016-07-14 21:42:47,177: INFO/MainProcess] Received task: test.error_task[96d5cb6c-b059-452a-b8cf-73e54f495976]
[2016-07-14 21:42:47,185: ERROR/Worker-4] Chord 'b3f57200-a52f-426f-9814-202576ae3538' raised: "Dependency 96d5cb6c-b059-452a-b8cf-73e54f495976 raised Exception('Mocked exception',)"
Traceback (most recent call last):
File "/private/tmp/venv/lib/python2.7/site-packages/celery/backends/base.py", line 580, in on_chord_part_return
ret = j(timeout=3.0, propagate=propagate)
File "/private/tmp/venv/lib/python2.7/site-packages/celery/result.py", line 691, in join_native
raise value
Exception: Mocked exception
[2016-07-14 21:42:47,207: INFO/MainProcess] Received task: test.link_error_task[c3c4408b-17fc-4efa-b502-c7cdf3862240]
[2016-07-14 21:42:47,209: WARNING/Worker-3] Link error executed
[2016-07-14 21:42:47,210: ERROR/MainProcess] Task test.error_task[96d5cb6c-b059-452a-b8cf-73e54f495976] raised unexpected: Exception('Mocked exception',)
Traceback (most recent call last):
File "/private/tmp/venv/lib/python2.7/site-packages/celery/app/trace.py", line 240, in trace_task
R = retval = fun(*args, **kwargs)
File "/private/tmp/venv/lib/python2.7/site-packages/celery/app/trace.py", line 438, in __protected_call__
return self.run(*args, **kwargs)
File "/private/tmp/test.py", line 11, in error_task
raise Exception("Mocked exception")
Exception: Mocked exception
[2016-07-14 21:42:47,214: INFO/MainProcess] Task test.link_error_task[c3c4408b-17fc-4efa-b502-c7cdf3862240] succeeded in 0.00591249897843s: None
To conclude, the problem seems to be with the chain c1. If there is at least one task after the task, which triggers an error, the link_error of the callback will not be executed.
Activity
ask commentedon Jul 19, 2016
Please try using the Celery development version (
pip install -U --pre celery), I think most of these are fixed thereAmbientLighter commentedon Jul 20, 2016
Hi, I can confirm this issue with latest Celery release (v4.0.0rc3)
Case 1
When I run this code without modifications, it fails with kombu.exceptions.EncodeError
Error:
Case 2
When I use subtask for callback link and link_error, it fails with initial exception, link_error is not called:
Code:
Error:
Case 3
And finally, when I skip task1.si(), it fails with ChordError and then Celery worker dies. It's strange, but when I try to restart Celery worker, it dies again and again. And the only way to restart worker is to purge queue.
Code:
Error:
rholloway commentedon May 30, 2017
I seem to be having similar issue on 4.0.2. If I look at the header task request object I can see link_error mentions the chord error callback, but it's never executed no matter what I try.
Wesley-Li commentedon Jul 10, 2017
I hit the same problem, is this solved?
AmbientLighter commentedon Jul 10, 2017
Nope, it's still actual.
Wesley-Li commentedon Jul 10, 2017
Chord/link_error is really terrible. Sometimes not run..., sometimes always run...sometimes run but still raises exception...
AmbientLighter commentedon Jul 10, 2017
@Wesley-Li Yeah, that's true. You may find more info on related issue #3709, but I don't use chord until this feature is fixed.
xeor commentedon Nov 13, 2017
@AmbientLighter This error is really annoying! Did you find a viable workaround not using chords? Or are you missing some functionality till this is solved?
AmbientLighter commentedon Nov 13, 2017
I haven't found any viable workaround yet, I'm not using chords at all now :(
AmbientLighter commentedon Nov 13, 2017
@xeor You may vote up for this issue so that it becomes more visible for the core developers.
xeor commentedon Nov 13, 2017
I have subscribed to a bunch of related issues.. Is there a separate place to vote for then? Can't find any.
I'm looking into a cheap workaround to this. Looks like a group don't have this problem. So just using them, and a side-task that checks and reports on the group-result might work till it is fixed.. Very ugly tho. Not even sure if that's going to work as I expect :(
xeor commentedon Nov 13, 2017
naah... That didn't do it. Groups in a chain won't execute in order, and using
linkon a group raises an error telling me to usechords. Commenting out the part that tells me to use chords only results in the link not being executed at all.Executing a new task inside the first group using
.join()blocks, so not something I want it to do (same as with.get()).So, basically, using time-limits (and probably other hooks that raises an exception), is broken if you use chords. I guess there will be no timelimits on my API for a while then... (don't tell them) :p
AmbientLighter commentedon Nov 13, 2017
@xeor There's an issue which has got most comments so far:
#3709
There's some workaround provided:
#3709 (comment)
I haven't personally looked into it yet, but it may be useful too.
auvipy commentedon Dec 19, 2017
lets try the master and report issues on celery/kombu#817