Repository navigation
Description
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).
Mandatory Debugging Information
- I have included the output of
celery -A proj reportin the issue.
(if you are not able to do this, then at least specify the Celery
version affected). - 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
- None
Possible Duplicates
- None
Environment & Settings
Celery version: 5.3.6 (emerald-rush)
celery report Output:
software -> celery:5.3.6 (emerald-rush) kombu:5.3.4 py:3.10.13
billiard:4.2.0 py-amqp:5.2.0
platform -> system:Linux arch:64bit, ELF
kernel version:5.10.104-linuxkit imp:CPython
loader -> celery.loaders.app.AppLoader
settings -> transport:amqp results:redis://redis:6379/0
broker_url: 'amqp://rabbit:********@rabbit:5672//'
result_backend: 'redis://redis:6379/0'
deprecated_settings: None
Steps to Reproduce
Required Dependencies
- Minimal Python Version: N/A or Unknown
- Minimal Celery Version: N/A or Unknown
- Minimal Kombu Version: N/A or Unknown
- Minimal Broker Version: N/A or Unknown
- Minimal Result Backend Version: N/A or Unknown
- 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:
amqp==5.2.0
async-timeout==4.0.3
billiard==4.2.0
celery==5.3.6
click==8.1.7
click-didyoumean==0.3.0
click-plugins==1.1.1
click-repl==0.3.0
kombu==5.3.4
prompt-toolkit==3.0.43
python-dateutil==2.8.2
redis==5.0.1
six==1.16.0
tzdata==2023.4
vine==5.1.0
wcwidth==0.2.13
Other Dependencies
Details
N/A
Minimally Reproducible Test Case
Apologies for not including test cases as a PR, because I need to find a workaround quickly, but wanted to make sure you had enough to reproduce the problems I am observing, so I have included a zip of files that can be run via docker-compose to reproduce the problem, via Python unittest:
Basic gist of the problem is that I have identified several cases where the behavior of a chain of groups is not what I would anticipate when those groups may fail. I've tried to include test cases that demonstrate both functioning and failing combinations, as I do not know enough about the celery internals to speculate as to a root cause.
See the test suite for all combinations, but some highlights below:
Consider the following tasks.py:
@app.task
def fail(arg):
raise MockException(arg)
@app.task
def identity(arg):
return arg
class MockException(Exception):
passIf we have a chain of two groups, and the first group fails, the exception is not raised in the result:
def test_group_chain_2_fail_1(self):
sig = celery.chain(
celery.group(
tasks.identity.si("a"),
tasks.fail.si("b"),
),
celery.group(
tasks.identity.si("c"),
tasks.identity.si("d"),
),
)
result = sig.apply_async()
with self.assertRaises(tasks.MockException):
result.get(TIMEOUT)
However, if it is the second group that fails, the exception is raised as we would expect:
def test_group_chain_2_fail_2(self):
sig = celery.chain(
celery.group(
tasks.identity.si("a"),
tasks.identity.si("b"),
),
celery.group(
tasks.identity.si("c"),
tasks.fail.si("d"),
),
)
result = sig.apply_async()
with self.assertRaises(tasks.MockException):
result.get(TIMEOUT)Bizarrely, if we pad the end of the chain with a normal task, then the exception is raised even if it is in the first group!
def test_group_chain_2_padded_fail_1(self):
sig = celery.chain(
celery.group(
tasks.identity.si("a"),
tasks.fail.si("b"),
),
celery.group(
tasks.identity.si("c"),
tasks.identity.si("d"),
),
tasks.identity.si("end"),
)
result = sig.apply_async()
with self.assertRaises(tasks.MockException):
result.get(TIMEOUT)This may indicate a potential leak somewhere in the code. If we pad the end of the chain, as above, and the second group fails, we do get an exception on result.get(), but it is a ChordError and not the MockException. This is potentially acceptable behavior, but it is not consistent with the behavior observed in test_group_chain_2_fail_1.
def test_group_chain_2_padded_fail_2(self):
sig = celery.chain(
celery.group(
tasks.identity.si("a"),
tasks.identity.si("b"),
),
celery.group(
tasks.identity.si("c"),
tasks.fail.si("d"),
),
tasks.identity.si("end"),
)
result = sig.apply_async()
with self.assertRaises(tasks.MockException):
result.get(TIMEOUT)Things get weirder from here on out.
For example, if we have a set of 3 groups, and the middle group fails, then we observe the same problem with the result.get() hanging, but if we look at the worker logs, we see that the tasks from the last group are still executed, which is unexpected.
def test_group_chain_3_fail_2(self):
sig = celery.chain(
celery.group(
tasks.identity.si("a"),
tasks.identity.si("b"),
),
celery.group(
tasks.identity.si("c"),
tasks.fail.si("d"),
),
celery.group(
tasks.identity.si("e"),
tasks.identity.si("f"),
),
)
result = sig.apply_async()
with self.assertRaises(tasks.MockException):
result.get(TIMEOUT)Worker output:
worker_1 | [2024-01-10 18:39:54,553: INFO/MainProcess] Task tasks.identity[0874a4a4-357c-490c-baa3-8f8d95604a03] received
worker_1 | [2024-01-10 18:39:54,555: INFO/ForkPoolWorker-2] Task tasks.identity[0874a4a4-357c-490c-baa3-8f8d95604a03] succeeded in 0.0019019600003957748s: 'a'
worker_1 | [2024-01-10 18:39:54,557: INFO/MainProcess] Task tasks.identity[ca97bc53-a545-405e-9f21-9fee4bf03e7c] received
worker_1 | [2024-01-10 18:39:54,560: INFO/MainProcess] Task tasks.identity[741123d0-f69f-4baf-b9d6-dd469fb34fc3] received
worker_1 | [2024-01-10 18:39:54,561: INFO/ForkPoolWorker-2] Task tasks.identity[ca97bc53-a545-405e-9f21-9fee4bf03e7c] succeeded in 0.0036852339981123805s: 'b'
worker_1 | [2024-01-10 18:39:54,561: INFO/MainProcess] Task tasks.fail[5918481a-08da-48b2-a9a9-b231a5677b79] received
worker_1 | [2024-01-10 18:39:54,562: INFO/MainProcess] Task tasks.identity[97a987cd-d0b5-42d7-91c8-2c85a07ce428] received
worker_1 | [2024-01-10 18:39:54,563: INFO/ForkPoolWorker-3] Task tasks.identity[741123d0-f69f-4baf-b9d6-dd469fb34fc3] succeeded in 0.0031626600539311767s: 'c'
worker_1 | [2024-01-10 18:39:54,564: INFO/ForkPoolWorker-1] Task tasks.identity[97a987cd-d0b5-42d7-91c8-2c85a07ce428] succeeded in 0.0010612240293994546s: 'e'
worker_1 | [2024-01-10 18:39:54,564: ERROR/ForkPoolWorker-2] Task tasks.fail[5918481a-08da-48b2-a9a9-b231a5677b79] raised unexpected: MockException('d')
worker_1 | Traceback (most recent call last):
worker_1 | File "/usr/local/lib/python3.9/site-packages/celery/app/trace.py", line 477, in trace_task
worker_1 | R = retval = fun(*args, **kwargs)
worker_1 | File "/usr/local/lib/python3.9/site-packages/celery/app/trace.py", line 760, in __protected_call__
worker_1 | return self.run(*args, **kwargs)
worker_1 | File "/app/tasks.py", line 11, in fail
worker_1 | raise MockException(arg)
worker_1 | tasks.MockException: d
worker_1 | [2024-01-10 18:39:54,565: INFO/MainProcess] Task tasks.identity[4c8e4298-6102-44f4-9eab-38d3820e1465] received
worker_1 | [2024-01-10 18:39:54,566: INFO/ForkPoolWorker-2] Task tasks.identity[4c8e4298-6102-44f4-9eab-38d3820e1465] succeeded in 0.0006063029868528247s: 'f'
If we pad the end of the chain with a normal task, then we observe the correct behavior in the test, but the worker logs show us that the later tasks are still executed. This behavior is not observed when the first or last groups fail.
def test_group_chain_3_padded_fail_2(self):
sig = celery.chain(
celery.group(
tasks.identity.si("a"),
tasks.identity.si("b"),
),
celery.group(
tasks.identity.si("c"),
tasks.fail.si("d"),
),
celery.group(
tasks.identity.si("e"),
tasks.identity.si("f"),
),
tasks.identity.si("end"),
)
result = sig.apply_async()
with self.assertRaises(tasks.MockException):
result.get(TIMEOUT)Worker output:
worker_1 | [2024-01-10 18:52:33,266: INFO/MainProcess] Task tasks.identity[e9ffe2a9-8506-40c9-9a7a-ce91b1f82ec6] received
worker_1 | [2024-01-10 18:52:33,267: INFO/MainProcess] Task tasks.identity[facbec2d-45a0-42b6-b36f-1c1b192988f0] received
worker_1 | [2024-01-10 18:52:33,269: INFO/ForkPoolWorker-3] Task tasks.identity[facbec2d-45a0-42b6-b36f-1c1b192988f0] succeeded in 0.001182273030281067s: 'b'
worker_1 | [2024-01-10 18:52:33,271: INFO/MainProcess] Task tasks.identity[67b62231-6c65-4c28-b238-cefa2186887c] received
worker_1 | [2024-01-10 18:52:33,271: INFO/ForkPoolWorker-2] Task tasks.identity[e9ffe2a9-8506-40c9-9a7a-ce91b1f82ec6] succeeded in 0.0038490999722853303s: 'a'
worker_1 | [2024-01-10 18:52:33,272: INFO/MainProcess] Task tasks.fail[93a6fff7-d554-4abb-b0d4-9f16fa38b38e] received
worker_1 | [2024-01-10 18:52:33,274: INFO/MainProcess] Task tasks.identity[18f21760-50f0-48f4-86d0-c54d542b874c] received
worker_1 | [2024-01-10 18:52:33,274: ERROR/ForkPoolWorker-2] Task tasks.fail[93a6fff7-d554-4abb-b0d4-9f16fa38b38e] raised unexpected: MockException('d')
worker_1 | Traceback (most recent call last):
worker_1 | File "/usr/local/lib/python3.9/site-packages/celery/app/trace.py", line 477, in trace_task
worker_1 | R = retval = fun(*args, **kwargs)
worker_1 | File "/usr/local/lib/python3.9/site-packages/celery/app/trace.py", line 760, in __protected_call__
worker_1 | return self.run(*args, **kwargs)
worker_1 | File "/app/tasks.py", line 11, in fail
worker_1 | raise MockException(arg)
worker_1 | tasks.MockException: d
worker_1 | [2024-01-10 18:52:33,276: INFO/ForkPoolWorker-3] Task tasks.identity[67b62231-6c65-4c28-b238-cefa2186887c] succeeded in 0.005195130011998117s: 'c'
worker_1 | [2024-01-10 18:52:33,276: INFO/ForkPoolWorker-1] Task tasks.identity[18f21760-50f0-48f4-86d0-c54d542b874c] succeeded in 0.001606971025466919s: 'e'
worker_1 | [2024-01-10 18:52:33,277: INFO/MainProcess] Task tasks.identity[fc5dc89a-bac8-4b6b-8d4b-014f04e98b1d] received
worker_1 | [2024-01-10 18:52:33,282: INFO/ForkPoolWorker-2] Task tasks.identity[fc5dc89a-bac8-4b6b-8d4b-014f04e98b1d] succeeded in 0.003287828993052244s: 'f'
worker_1 | [2024-01-10 18:52:33,282: INFO/MainProcess] Task tasks.identity[3320285d-87d9-4982-b002-d7f16d6aa3f5] received
worker_1 | [2024-01-10 18:52:33,283: INFO/ForkPoolWorker-2] Task tasks.identity[3320285d-87d9-4982-b002-d7f16d6aa3f5] succeeded in 0.0005700839683413506s: 'end'
Finally, in a very confusing test case, if we have a failing group, followed by two normal tasks, the worker logs show an unusual exception, which may also be a potential thread to pull in finding a solution.
def test_demo_task_id_error_1(self):
sig = celery.chain(
tasks.identity.si("start"),
celery.group(
tasks.identity.si("a"),
tasks.fail.si("b"),
),
tasks.identity.si("break"),
tasks.identity.si("end"),
)
result = sig.apply_async()
with self.assertRaises(tasks.MockException):
result.get(TIMEOUT)Worker output:
worker_1 | [2024-01-10 19:04:29,013: INFO/MainProcess] Task tasks.identity[ee5a94d6-7ab0-4fa3-ac2a-35a00eff4793] received
worker_1 | [2024-01-10 19:04:29,032: INFO/MainProcess] Task tasks.identity[20312be4-79df-4a14-a371-5586bd2ba693] received
worker_1 | [2024-01-10 19:04:29,035: INFO/MainProcess] Task tasks.fail[730aa78b-e363-4032-a907-0a717c2f2d60] received
worker_1 | [2024-01-10 19:04:29,036: INFO/ForkPoolWorker-2] Task tasks.identity[ee5a94d6-7ab0-4fa3-ac2a-35a00eff4793] succeeded in 0.0208328349981457s: 'start'
worker_1 | [2024-01-10 19:04:29,041: INFO/ForkPoolWorker-3] Task tasks.identity[20312be4-79df-4a14-a371-5586bd2ba693] succeeded in 0.007592667010612786s: 'a'
worker_1 | [2024-01-10 19:04:29,047: ERROR/ForkPoolWorker-1] Chord '126b50b3-d85e-41b1-a168-041b43bf27ec' raised: ChordError("Dependency 730aa78b-e363-4032-a907-0a717c2f2d60 raised MockException('b')")
worker_1 | Traceback (most recent call last):
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/app/trace.py", line 477, in trace_task
worker_1 | R = retval = fun(*args, **kwargs)
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/app/trace.py", line 760, in __protected_call__
worker_1 | return self.run(*args, **kwargs)
worker_1 | File "/app/tasks.py", line 11, in fail
worker_1 | raise MockException(arg)
worker_1 | tasks.MockException: b
worker_1 |
worker_1 | During handling of the above exception, another exception occurred:
worker_1 |
worker_1 | Traceback (most recent call last):
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/backends/redis.py", line 528, in on_chord_part_return
worker_1 | resl = [unpack(tup, decode) for tup in resl]
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/backends/redis.py", line 528, in <listcomp>
worker_1 | resl = [unpack(tup, decode) for tup in resl]
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/backends/redis.py", line 434, in _unpack_chord_result
worker_1 | raise ChordError(f'Dependency {tid} raised {retval!r}')
worker_1 | celery.exceptions.ChordError: Dependency 730aa78b-e363-4032-a907-0a717c2f2d60 raised MockException('b')
worker_1 | [2024-01-10 19:04:29,051: WARNING/ForkPoolWorker-1] /usr/local/lib/python3.10/site-packages/celery/app/trace.py:686: RuntimeWarning: Exception raised outside body: ValueError('task_id must not be empty. Got None instead.'):
worker_1 | Traceback (most recent call last):
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/app/trace.py", line 477, in trace_task
worker_1 | R = retval = fun(*args, **kwargs)
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/app/trace.py", line 760, in __protected_call__
worker_1 | return self.run(*args, **kwargs)
worker_1 | File "/app/tasks.py", line 11, in fail
worker_1 | raise MockException(arg)
worker_1 | tasks.MockException: b
worker_1 |
worker_1 | During handling of the above exception, another exception occurred:
worker_1 |
worker_1 | Traceback (most recent call last):
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/backends/redis.py", line 528, in on_chord_part_return
worker_1 | resl = [unpack(tup, decode) for tup in resl]
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/backends/redis.py", line 528, in <listcomp>
worker_1 | resl = [unpack(tup, decode) for tup in resl]
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/backends/redis.py", line 434, in _unpack_chord_result
worker_1 | raise ChordError(f'Dependency {tid} raised {retval!r}')
worker_1 | celery.exceptions.ChordError: Dependency 730aa78b-e363-4032-a907-0a717c2f2d60 raised MockException('b')
worker_1 |
worker_1 | During handling of the above exception, another exception occurred:
worker_1 |
worker_1 | Traceback (most recent call last):
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/app/trace.py", line 494, in trace_task
worker_1 | I, R, state, retval = on_error(task_request, exc)
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/app/trace.py", line 399, in on_error
worker_1 | R = I.handle_error_state(
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/app/trace.py", line 178, in handle_error_state
worker_1 | return {
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/app/trace.py", line 230, in handle_failure
worker_1 | task.backend.mark_as_failure(
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/backends/base.py", line 172, in mark_as_failure
worker_1 | self.on_chord_part_return(request, state, exc)
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/backends/redis.py", line 547, in on_chord_part_return
worker_1 | return self.chord_error_from_stack(callback, exc)
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/backends/base.py", line 304, in chord_error_from_stack
worker_1 | return backend.fail_from_current_stack(callback.id, exc=exc)
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/backends/base.py", line 311, in fail_from_current_stack
worker_1 | self.mark_as_failure(task_id, exc, exception_info.traceback)
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/backends/base.py", line 167, in mark_as_failure
worker_1 | self.store_result(task_id, exc, state,
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/backends/base.py", line 526, in store_result
worker_1 | self._store_result(task_id, result, state, traceback,
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/backends/base.py", line 973, in _store_result
worker_1 | current_meta = self._get_task_meta_for(task_id)
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/backends/base.py", line 995, in _get_task_meta_for
worker_1 | meta = self.get(self.get_key_for_task(task_id))
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/backends/base.py", line 869, in get_key_for_task
worker_1 | raise ValueError(f'task_id must not be empty. Got {task_id} instead.')
worker_1 | ValueError: task_id must not be empty. Got None instead.
worker_1 |
worker_1 | warn(RuntimeWarning(
worker_1 |
worker_1 | [2024-01-10 19:04:29,053: ERROR/ForkPoolWorker-1] Task tasks.fail[730aa78b-e363-4032-a907-0a717c2f2d60] raised unexpected: ValueError('task_id must not be empty. Got None instead.')
worker_1 | Traceback (most recent call last):
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/app/trace.py", line 477, in trace_task
worker_1 | R = retval = fun(*args, **kwargs)
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/app/trace.py", line 760, in __protected_call__
worker_1 | return self.run(*args, **kwargs)
worker_1 | File "/app/tasks.py", line 11, in fail
worker_1 | raise MockException(arg)
worker_1 | tasks.MockException: b
worker_1 |
worker_1 | During handling of the above exception, another exception occurred:
worker_1 |
worker_1 | Traceback (most recent call last):
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/backends/redis.py", line 528, in on_chord_part_return
worker_1 | resl = [unpack(tup, decode) for tup in resl]
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/backends/redis.py", line 528, in <listcomp>
worker_1 | resl = [unpack(tup, decode) for tup in resl]
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/backends/redis.py", line 434, in _unpack_chord_result
worker_1 | raise ChordError(f'Dependency {tid} raised {retval!r}')
worker_1 | celery.exceptions.ChordError: Dependency 730aa78b-e363-4032-a907-0a717c2f2d60 raised MockException('b')
worker_1 |
worker_1 | During handling of the above exception, another exception occurred:
worker_1 |
worker_1 | Traceback (most recent call last):
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/app/trace.py", line 494, in trace_task
worker_1 | I, R, state, retval = on_error(task_request, exc)
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/app/trace.py", line 399, in on_error
worker_1 | R = I.handle_error_state(
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/app/trace.py", line 178, in handle_error_state
worker_1 | return {
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/app/trace.py", line 230, in handle_failure
worker_1 | task.backend.mark_as_failure(
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/backends/base.py", line 172, in mark_as_failure
worker_1 | self.on_chord_part_return(request, state, exc)
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/backends/redis.py", line 547, in on_chord_part_return
worker_1 | return self.chord_error_from_stack(callback, exc)
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/backends/base.py", line 304, in chord_error_from_stack
worker_1 | return backend.fail_from_current_stack(callback.id, exc=exc)
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/backends/base.py", line 311, in fail_from_current_stack
worker_1 | self.mark_as_failure(task_id, exc, exception_info.traceback)
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/backends/base.py", line 167, in mark_as_failure
worker_1 | self.store_result(task_id, exc, state,
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/backends/base.py", line 526, in store_result
worker_1 | self._store_result(task_id, result, state, traceback,
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/backends/base.py", line 973, in _store_result
worker_1 | current_meta = self._get_task_meta_for(task_id)
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/backends/base.py", line 995, in _get_task_meta_for
worker_1 | meta = self.get(self.get_key_for_task(task_id))
worker_1 | File "/usr/local/lib/python3.10/site-packages/celery/backends/base.py", line 869, in get_key_for_task
worker_1 | raise ValueError(f'task_id must not be empty. Got {task_id} instead.')
worker_1 | ValueError: task_id must not be empty. Got None instead.
Please see the full test suite for additional examples of both passing and failing test cases -- it is much more thorough and can be easily run to reproduce the problem with docker-compose.
Activity
kevinjdolan commentedon Jan 10, 2024
Note: included in the test cases, similar behavior is observed when you have a task that calls
self.replace()with a group (though sometimes it is slightly different), as below:This test-case fails:
This test-case raises ChordError, which again, might be acceptable behavior, but is not what I'd expect:
kevinjdolan commentedon Jan 10, 2024
Including an updates test suite here to include some test with chords (as I understand groups are converted to chords behind the scenes when unrolling a chain):
celery_bug_report_2.zip
If we have a chain of two chords, and the first chord's body fails, we see the same problem as above:
If, however, the header group fails, the test passes, but we see the task_id error in the worker logs:
kevinjdolan commentedon Jan 10, 2024
I've observed that top-level normal tasks that fail seem to consistently propagate errors, which leads to a few workarounds. Attaching updated test suite to demonstrate the workarounds:
celery_bug_report_3.zip
If we ensure that the groups ALWAYS pass (in practice, with a high-level try/catch, perhaps), then we can check the output in a subsequent normal top-level task, and raise an exception if any of the outputs look faulty.
For example, consider this new task:
All the combinations tested seem to work with these semantics:
We can even do this same thing by replacement of a sub-chain:
All the combinations pass, like this one:
This might be more simply expressed as a chord (since I think that's what it unrolls to, or something similar):
And tests like this will pass:
kevinjdolan commentedon Jan 10, 2024
Finally, a more abstracted workaround is to create a couple dummy tasks that wrap the call to the desired task in a
safe_calltask that catches and returns exceptions, and chording that to a function that checks the output.We can use this to pass a test like this:
Attached updated test suite to include this construct:
celery_bug_report_4.zip
Obviously this workaround has numerous short-comings. It does not consider the entire signature, and would not behave as expected if the tasks in the safe_group are anything other than ordinary tasks that can be run synchronously. It probably misses certain error conditions, but it works for my use-case.
I think what's most fascinating about this workaround is that
test_chord_chain_2_fail_1_bodyfails -- in which an exception is raised in the body of the chord, if the chord is included directly in the chain. However, as in these cases, if an exception is raised in the body of a chord that has been replaced into a chain, it works as desired. Whatever is functionally different about those two situations is probably the root cause of this bug.Hopefully though, y'all can figure out what's going on and I won't need it! Thanks for the great work on Celery, I've used it at almost every job I've had for the past 15 years.
Nusnus commentedon Jan 16, 2024
After collaborating on this issue, we've identified the root cause as a design conflict in the canvas feature of Celery. The issue stems from two conflicting assumptions:
The conflict arises because the chord body acts as both a chain and a callback, leading to these assumptions clashing and resulting in the unexpected exception.
You can easily replicate this design flaw by creating a chain of two tasks and freezing it. Notice how the tasks get IDs, but the chain does not.
Our solution is straightforward: we propose to allow the chain to adopt the ID of its last task. This approach directly addresses the identified design conflict while maintaining backward compatibility.
That being said, we need to make sure it does not create new conflicts.
I’ll update here as we progress.
Thank you for the report!
P.S
💪 💪 💪
Nusnus commentedon Jan 17, 2024
Fix pushed to main and will be released in Celery v5.4 @kevinjdolan
kevinjdolan commentedon Jan 17, 2024
Wow that was fast! Excited to get rid of my workaround!
kevinjdolan commentedon Jan 17, 2024
@Nusnus: FYI -- I ran my test suite on 5.4.0rc0, and while it did fix a couple of the tests, many are still failing, so I believe the core issue is still present.
For example, this one was fixed (and you included a similar test in your PR):
But this test is still failing with a Timeout:
For reference, here is the full test suite I used:
celery_bug_reports_5.zip
Nusnus commentedon Jan 17, 2024
@kevinjdolan
I’ve tried to run this second test and if I don’t miss anything, it seems to behave correctly.
The worker get’s
a, then fails onb.Calling
get()on the results, as expected, does not work and fails oncelery.exceptions.TimeoutError: The operation timed out.Did I get something wrong?
Test:
Worker:
result.get(timeout=RESULT_TIMEOUT)Nusnus commentedon Jan 17, 2024
Glad we could help!
I just did the dirty work afterward :)
kevinjdolan commentedon Jan 19, 2024
@Nusnus
Expected behavior for that snippet would be to raise the exception MockException shortly after the failing task is run -- it should bubble up. This is as opposed to the Timeout, which implies the task state was just lost, but in fact we know that it failed. (Alternatively, I'd accept a wrapper error of some type like ChordDependencyError).
This would be more consistent with the behavior when we see the second group fail (this test passes):
This allows for composing workflows that are error-tolerant.
Effectively, we want the semantics to be something like the following in async python:
In that case, awaiting run() should result in the exception from task_b bubbling up and being raised.
While my application does not actually call
result.get(), I do use error callback to clean up workflows that may fail in unexpected ways. These errbacks fail to call in the same cases where the timeout is hit when callingresult.get().To demonstrate this, try:
In the worker logs, you will see the "fail" task is not run.
Compare this to a failure in the second group:
If failures in the second group bubble up, then so should failures in the first group. The behavior should at least be consistent.
Allowing me to use an errback on the entire chain that fails if any subordinate tasks in the workflow fail allow me to do cleanups. For example, in my use-case, I am generating reports by running a chain of tasks (that may themselves be groups), and in the event anything fails in that chain, I want to mark the report as having failed.
kevinjdolan commentedon Feb 2, 2024
@Nusnus this issue is still present -- should I create a separate bug report?
Nusnus commentedon Feb 3, 2024
Yeah, I think it would be a good idea to clear out the noise.
Link to this issue, of course, and try to fill in the bug template as much as it makes sense to you.
Thank you!
FluxZA commentedon Apr 13, 2024
Hi. Thank you both for the work on these issues so far.
I have run into a similar issue and I wanted to add what I have found. Using 5.4.0rc1 a chain task now has a 'task_id' key in its options dict, however, the value can be None.
In my case, the chain is the body of a chord, and it appears that it never gets its own last_task_id to use as its own id.
@kevinjdolan Have you created the separate bug report yet? if so could you link it so I can see if my issue is covered or requires a separate report.
auvipy commentedon Jun 24, 2025
Hey, most probably this might fix this #9774
auvipy commentedon Jun 26, 2025
@dfm88 may be this is also fixed by your PR?
dfm88 commentedon Jun 26, 2025
mm not completely, in fact I also experienced timeouts when both the header and the
bodyof a chord aregroups. I had to skip those parametrized tests when the input body was a group since they were not the focus of my pr.I'll try to look into it.
Skipped tests
pytest.mark.skip(reason="Task times out"),celery/t/integration/test_canvas.py
Lines 3154 to 3197 in de104a2
fix: (celery#8786) time out when chord header fails with group body
auvipy commentedon Jul 1, 2025
we got another pr to fix this
fix: (celery#8786) PyPy mock mapping compatibility
fix: (#8786) time out when chord header fails with group body (#9788)