Skip to content

Unexpected Behaviors for a Chain of Groups, when Groups contain failing tasks #8786

Description

@kevinjdolan

Checklist

  • I have verified that the issue exists against the main branch 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 report in 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 main branch of Celery.
  • I have included the contents of pip freeze in 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:

celery_bug_report.zip

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):
    pass

If 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

  1. kevinjdolan commented on Jan 10, 2024

    @kevinjdolan
    Author

    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:

    @app.task(bind=True)
    def replace_group(self, args):
        tasks = []
        for arg in args:
            if arg == 'fail':
                tasks.append(fail.si(arg))
            else:
                tasks.append(identity.si(arg))
        return self.replace(celery.group(tasks))

    This test-case fails:

    def test_group_chain_2_replace_fail_1(self):
        sig = celery.chain(
            tasks.replace_group.si(["a", "fail"]),
            tasks.replace_group.si(["c", "d"]),
        )
        result = sig.apply_async()
        with self.assertRaises(tasks.MockException):
            result.get(TIMEOUT)

    This test-case raises ChordError, which again, might be acceptable behavior, but is not what I'd expect:

    def test_group_chain_2_replace_fail_2(self):
        sig = celery.chain(
            tasks.replace_group.si(["a", "b"]),
            tasks.replace_group.si(["c", "fail"]),
        )
        result = sig.apply_async()
        with self.assertRaises(tasks.MockException):
            result.get(TIMEOUT)
  2. kevinjdolan commented on Jan 10, 2024

    @kevinjdolan
    Author

    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:

    # FAILS with TimeoutError
    def test_chord_chain_2_fail_1_body(self):
        sig = celery.chain(
            celery.chord(
                header=[
                    tasks.identity.si("a"),
                    tasks.identity.si("b"),
                ],
                body=tasks.fail.si("c"),
            ),
            celery.chord(
                header=[
                    tasks.identity.si("d"),
                    tasks.identity.si("e"),
                ],
                body=tasks.identity.si("f"),
            ),
        )
        result = sig.apply_async()
        with self.assertRaises(tasks.MockException):
            result.get(TIMEOUT)

    If, however, the header group fails, the test passes, but we see the task_id error in the worker logs:

    def test_chord_chain_2_fail_1_header(self):
        sig = celery.chain(
            celery.chord(
                header=[
                    tasks.identity.si("a"),
                    tasks.fail.si("b"),
                ],
                body=tasks.identity.si("c"),
            ),
            celery.chord(
                header=[
                    tasks.identity.si("d"),
                    tasks.identity.si("e"),
                ],
                body=tasks.identity.si("f"),
            ),
        )
        result = sig.apply_async()
        with self.assertRaises(tasks.MockException):
            result.get(TIMEOUT)
  3. kevinjdolan commented on Jan 10, 2024

    @kevinjdolan
    Author

    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:

    @app.task
    def check_for_failures(args):
        if 'fail' in args:
            raise MockException('fail in args')
        
        return args

    All the combinations tested seem to work with these semantics:

    def test_workaround_chain_3_fail_2(self):
        sig = celery.chain(
            celery.group(
                tasks.identity.si("a"),
                tasks.identity.si("b"),
            ),
            tasks.check_for_failures.s(),
            celery.group(
                tasks.identity.si("c"),
                tasks.identity.si("fail"),
            ),
            tasks.check_for_failures.s(),
            celery.group(
                tasks.identity.si("e"),
                tasks.identity.si("f"),
            ),
            tasks.check_for_failures.s(),
        )
        result = sig.apply_async()
        with self.assertRaises(tasks.MockException):
            result.get(TIMEOUT)

    We can even do this same thing by replacement of a sub-chain:

    @app.task(bind=True)
    def replace_group_subchain(self, args):
        tasks = [
            identity.si(arg)
            for arg
            in args
        ]
    
        sig = celery.chain(
            celery.group(tasks),
            check_for_failures.s(),
        )
        
        return self.replace(sig)

    All the combinations pass, like this one:

    def test_workaround_subchain_3_fail_2(self):
        sig = celery.chain(
            tasks.replace_group_subchain.si(["a", "b"]),
            tasks.replace_group_subchain.si(["c", "fail"]),
            tasks.replace_group_subchain.si(["e", "f"]),
        )
        result = sig.apply_async()
        with self.assertRaises(tasks.MockException):
            result.get(TIMEOUT)

    This might be more simply expressed as a chord (since I think that's what it unrolls to, or something similar):

    @app.task(bind=True)
    def replace_group_subchord(self, args):
        tasks = [
            identity.si(arg)
            for arg
            in args
        ]
    
        sig = celery.chord(
            header=tasks,
            body=check_for_failures.s(),
        )
        
        return self.replace(sig)

    And tests like this will pass:

    def test_workaround_subchord_3_fail_2(self):
        sig = celery.chain(
            tasks.replace_group_subchord.si(["a", "b"]),
            tasks.replace_group_subchord.si(["c", "fail"]),
            tasks.replace_group_subchord.si(["e", "f"]),
        )
        result = sig.apply_async()
        with self.assertRaises(tasks.MockException):
            result.get(TIMEOUT)
  4. kevinjdolan commented on Jan 10, 2024

    @kevinjdolan
    Author

    Finally, a more abstracted workaround is to create a couple dummy tasks that wrap the call to the desired task in a safe_call task that catches and returns exceptions, and chording that to a function that checks the output.

    @app.task
    def safe_call(
        task,
        args=None,
        kwargs=None,
    ):
        try:
            task_func = celery.current_app.tasks[task]
            args = args or []
            kwargs = kwargs or {}
            result = task_func(*args, **kwargs)
            return (True, result, None)
        except Exception as e:
            traceback.print_exc()
            trace = traceback.format_exc()
            return (False, e.__class__.__name__, trace)
    
    @app.task
    def check_safe_group(
        results: list[tuple[bool, typing.Any]],
    ):
        values = []
        for [success, value, trace] in results:
            if not success:
                raise SafeGroupException(
                    f"Exception encountered in safe_group: {value}",
                )
    
            values.append(value)
        return values
    
    @app.task(bind=True)
    def safe_group(
        self: celery.Task,
        *signatures: list[dict],
    ):
        safe_call_sigs = []
        for sig in signatures:
            safe_call_sig = safe_call.si(
                sig['task'],
                args=sig.get('args'),
                kwargs=sig.get('kwargs'),
            )
            safe_call_sigs.append(safe_call_sig)
    
        chord_sig = celery.chord(
            header=safe_call_sigs,
            body=check_safe_group.s(),
        )
        return self.replace(chord_sig)
    
    class SafeGroupException(Exception):
        pass

    We can use this to pass a test like this:

    def test_safe_group_3_fail_2(self):
        sig = celery.chain(
            tasks.safe_group.si(
                tasks.identity.si("a"),
                tasks.identity.si("b"),
            ),
            tasks.safe_group.si(
                tasks.identity.si("c"),
                tasks.fail.si("d"),
            ),
            tasks.safe_group.si(
                tasks.identity.si("e"),
                tasks.identity.si("f"),
            ),
        )
        result = sig.apply_async()
        with self.assertRaises(tasks.SafeGroupException):
            result.get(TIMEOUT)

    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_body fails -- 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.

  5. Nusnus commented on Jan 16, 2024

    @Nusnus
    Member

    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:

    • Assumption 1: When a chain is frozen, IDs are assigned to the tasks within the chain, but not to the chain itself.
    • Assumption 2: In certain scenarios, a callback signature requires an ID.

    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

    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.

    💪 💪 💪

  6. Nusnus commented on Jan 17, 2024

    @Nusnus
    Member

    Fix pushed to main and will be released in Celery v5.4 @kevinjdolan

  7. kevinjdolan commented on Jan 17, 2024

    @kevinjdolan
    Author

    Wow that was fast! Excited to get rid of my workaround!

  8. kevinjdolan commented on Jan 17, 2024

    @kevinjdolan
    Author

    @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):

    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)
    

    But this test is still failing with a Timeout:

    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)
    

    For reference, here is the full test suite I used:
    celery_bug_reports_5.zip

  9. Nusnus commented on Jan 17, 2024

    @Nusnus
    Member

    @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 on b.

    2024-01-17 23:23:09 Task t.integration.tasks.identity[3bb83020-f54f-4dc3-8b7e-0a589e9c7614] succeeded in 0.022236790999997424s: ‘a’
    2024-01-17 23:23:09 [2024-01-17 21:23:09,803: ERROR/ForkPoolWorker-1] Chord '1200c366-6d64-465a-ac84-ee2cc511d581' raised: ChordError("Dependency d368025d-7cd9-45b6-8407-bb27cbb62dd5 raised ExpectedException('Task expected to fail', 'b')")
    

    Calling get() on the results, as expected, does not work and fails on celery.exceptions.TimeoutError: The operation timed out.

    Did I get something wrong?

    Test:

    def test_group_chain_2_fail_1(self, celery_setup: CeleryTestSetup):
        queue = celery_setup.worker.worker_queue
        sig = chain(
            group(
                identity.si("a").set(queue=queue),
                fail.si("b").set(queue=queue),
            ),
            group(
                identity.si("c").set(queue=queue),
                identity.si("d").set(queue=queue),
            ),
        )
        result = sig.apply_async()
        with pytest.raises(ExpectedException):
            result.get(timeout=RESULT_TIMEOUT)

    Worker:

    2024-01-17 23:23:09 Task t.integration.tasks.identity[3bb83020-f54f-4dc3-8b7e-0a589e9c7614] received
    2024-01-17 23:23:09 [2024-01-17 21:23:09,754: INFO/MainProcess] Task t.integration.tasks.identity[3bb83020-f54f-4dc3-8b7e-0a589e9c7614] received
    2024-01-17 23:23:09 Task t.integration.tasks.fail[d368025d-7cd9-45b6-8407-bb27cbb62dd5] received
    2024-01-17 23:23:09 [2024-01-17 21:23:09,760: INFO/MainProcess] Task t.integration.tasks.fail[d368025d-7cd9-45b6-8407-bb27cbb62dd5] received
    2024-01-17 23:23:09 [2024-01-17 21:23:09,780: INFO/ForkPoolWorker-8] Task t.integration.tasks.identity[3bb83020-f54f-4dc3-8b7e-0a589e9c7614] succeeded in 0.022236790999997424s: 'a'
    2024-01-17 23:23:09 Task t.integration.tasks.identity[3bb83020-f54f-4dc3-8b7e-0a589e9c7614] succeeded in 0.022236790999997424s: 'a'
    2024-01-17 23:23:09 [2024-01-17 21:23:09,803: ERROR/ForkPoolWorker-1] Chord '1200c366-6d64-465a-ac84-ee2cc511d581' raised: ChordError("Dependency d368025d-7cd9-45b6-8407-bb27cbb62dd5 raised ExpectedException('Task expected to fail', 'b')")
    2024-01-17 23:23:09 Traceback (most recent call last):
    2024-01-17 23:23:09   File "/celery/celery/app/trace.py", line 453, in trace_task
    2024-01-17 23:23:09     R = retval = fun(*args, **kwargs)
    2024-01-17 23:23:09                  ^^^^^^^^^^^^^^^^^^^^
    2024-01-17 23:23:09   File "/celery/celery/app/trace.py", line 736, in __protected_call__
    2024-01-17 23:23:09     return self.run(*args, **kwargs)
    2024-01-17 23:23:09            ^^^^^^^^^^^^^^^^^^^^^^^^^
    2024-01-17 23:23:09   File "/app/t/integration/tasks.py", line 360, in fail
    2024-01-17 23:23:09     raise ExpectedException(*args)
    2024-01-17 23:23:09 t.integration.tasks.ExpectedException: ('Task expected to fail', 'b')
    2024-01-17 23:23:09 
    2024-01-17 23:23:09 During handling of the above exception, another exception occurred:
    2024-01-17 23:23:09 
    2024-01-17 23:23:09 Traceback (most recent call last):
    2024-01-17 23:23:09   File "/celery/celery/backends/redis.py", line 528, in on_chord_part_return
    2024-01-17 23:23:09     resl = [unpack(tup, decode) for tup in resl]
    2024-01-17 23:23:09            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    2024-01-17 23:23:09   File "/celery/celery/backends/redis.py", line 528, in <listcomp>
    2024-01-17 23:23:09     resl = [unpack(tup, decode) for tup in resl]
    2024-01-17 23:23:09             ^^^^^^^^^^^^^^^^^^^
    2024-01-17 23:23:09   File "/celery/celery/backends/redis.py", line 434, in _unpack_chord_result
    2024-01-17 23:23:09     raise ChordError(f'Dependency {tid} raised {retval!r}')
    2024-01-17 23:23:09 celery.exceptions.ChordError: Dependency d368025d-7cd9-45b6-8407-bb27cbb62dd5 raised ExpectedException('Task expected to fail', 'b')
    2024-01-17 23:23:09 Chord '1200c366-6d64-465a-ac84-ee2cc511d581' raised: ChordError("Dependency d368025d-7cd9-45b6-8407-bb27cbb62dd5 raised ExpectedException('Task expected to fail', 'b')")
    2024-01-17 23:23:09 Traceback (most recent call last):
    2024-01-17 23:23:09   File "/celery/celery/app/trace.py", line 453, in trace_task
    2024-01-17 23:23:09     R = retval = fun(*args, **kwargs)
    2024-01-17 23:23:09                  ^^^^^^^^^^^^^^^^^^^^
    2024-01-17 23:23:09   File "/celery/celery/app/trace.py", line 736, in __protected_call__
    2024-01-17 23:23:09     return self.run(*args, **kwargs)
    2024-01-17 23:23:09            ^^^^^^^^^^^^^^^^^^^^^^^^^
    2024-01-17 23:23:09   File "/app/t/integration/tasks.py", line 360, in fail
    2024-01-17 23:23:09     raise ExpectedException(*args)
    2024-01-17 23:23:09 t.integration.tasks.ExpectedException: ('Task expected to fail', 'b')
    2024-01-17 23:23:09 
    2024-01-17 23:23:09 During handling of the above exception, another exception occurred:
    2024-01-17 23:23:09 
    2024-01-17 23:23:09 Traceback (most recent call last):
    2024-01-17 23:23:09   File "/celery/celery/backends/redis.py", line 528, in on_chord_part_return
    2024-01-17 23:23:09     resl = [unpack(tup, decode) for tup in resl]
    2024-01-17 23:23:09            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    2024-01-17 23:23:09   File "/celery/celery/backends/redis.py", line 528, in <listcomp>
    2024-01-17 23:23:09     resl = [unpack(tup, decode) for tup in resl]
    2024-01-17 23:23:09             ^^^^^^^^^^^^^^^^^^^
    2024-01-17 23:23:09   File "/celery/celery/backends/redis.py", line 434, in _unpack_chord_result
    2024-01-17 23:23:09     raise ChordError(f'Dependency {tid} raised {retval!r}')
    2024-01-17 23:23:09 celery.exceptions.ChordError: Dependency d368025d-7cd9-45b6-8407-bb27cbb62dd5 raised ExpectedException('Task expected to fail', 'b')
    2024-01-17 23:23:09 Task t.integration.tasks.fail[d368025d-7cd9-45b6-8407-bb27cbb62dd5] raised unexpected: ExpectedException('Task expected to fail', 'b')
    2024-01-17 23:23:09 Traceback (most recent call last):
    2024-01-17 23:23:09   File "/celery/celery/app/trace.py", line 453, in trace_task
    2024-01-17 23:23:09     R = retval = fun(*args, **kwargs)
    2024-01-17 23:23:09                  ^^^^^^^^^^^^^^^^^^^^
    2024-01-17 23:23:09   File "/celery/celery/app/trace.py", line 736, in __protected_call__
    2024-01-17 23:23:09     return self.run(*args, **kwargs)
    2024-01-17 23:23:09            ^^^^^^^^^^^^^^^^^^^^^^^^^
    2024-01-17 23:23:09   File "/app/t/integration/tasks.py", line 360, in fail
    2024-01-17 23:23:09     raise ExpectedException(*args)
    2024-01-17 23:23:09 t.integration.tasks.ExpectedException: ('Task expected to fail', 'b')
    2024-01-17 23:23:09 [2024-01-17 21:23:09,807: ERROR/ForkPoolWorker-1] Task t.integration.tasks.fail[d368025d-7cd9-45b6-8407-bb27cbb62dd5] raised unexpected: ExpectedException('Task expected to fail', 'b')
    2024-01-17 23:23:09 Traceback (most recent call last):
    2024-01-17 23:23:09   File "/celery/celery/app/trace.py", line 453, in trace_task
    2024-01-17 23:23:09     R = retval = fun(*args, **kwargs)
    2024-01-17 23:23:09                  ^^^^^^^^^^^^^^^^^^^^
    2024-01-17 23:23:09   File "/celery/celery/app/trace.py", line 736, in __protected_call__
    2024-01-17 23:23:09     return self.run(*args, **kwargs)
    2024-01-17 23:23:09            ^^^^^^^^^^^^^^^^^^^^^^^^^
    2024-01-17 23:23:09   File "/app/t/integration/tasks.py", line 360, in fail
    2024-01-17 23:23:09     raise ExpectedException(*args)
    2024-01-17 23:23:09 t.integration.tasks.ExpectedException: ('Task expected to fail', 'b')

    result.get(timeout=RESULT_TIMEOUT)

    Traceback (most recent call last):
      File "/Users/nusnus/dev/GitHub/celery/celery/backends/asynchronous.py", line 287, in _wait_for_pending
        for _ in self.drain_events_until(
      File "/Users/nusnus/dev/GitHub/celery/celery/backends/asynchronous.py", line 52, in drain_events_until
        raise socket.timeout()
    TimeoutError
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/Users/nusnus/dev/GitHub/celery/celery/result.py", line 705, in get
        return (self.join_native if self.supports_native_join else self.join)(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/Users/nusnus/dev/GitHub/celery/celery/result.py", line 827, in join_native
        for task_id, meta in self.iter_native(timeout, interval, no_ack,
      File "/Users/nusnus/dev/GitHub/celery/celery/backends/asynchronous.py", line 172, in iter_native
        for _ in self._wait_for_pending(result, no_ack=no_ack, **kwargs):
      File "/Users/nusnus/dev/GitHub/celery/celery/backends/asynchronous.py", line 293, in _wait_for_pending
        raise TimeoutError('The operation timed out.')
    celery.exceptions.TimeoutError: The operation timed out.
    Traceback (most recent call last):
      File "/Users/nusnus/dev/GitHub/celery/celery/backends/asynchronous.py", line 287, in _wait_for_pending
        for _ in self.drain_events_until(
      File "/Users/nusnus/dev/GitHub/celery/celery/backends/asynchronous.py", line 52, in drain_events_until
        raise socket.timeout()
    TimeoutError
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/Users/nusnus/dev/GitHub/celery/celery/result.py", line 705, in get
        return (self.join_native if self.supports_native_join else self.join)(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/Users/nusnus/dev/GitHub/celery/celery/result.py", line 827, in join_native
        for task_id, meta in self.iter_native(timeout, interval, no_ack,
      File "/Users/nusnus/dev/GitHub/celery/celery/backends/asynchronous.py", line 172, in iter_native
        for _ in self._wait_for_pending(result, no_ack=no_ack, **kwargs):
      File "/Users/nusnus/dev/GitHub/celery/celery/backends/asynchronous.py", line 293, in _wait_for_pending
        raise TimeoutError('The operation timed out.')
    celery.exceptions.TimeoutError: The operation timed out.
  10. Nusnus commented on Jan 17, 2024

    @Nusnus
    Member

    Wow that was fast! Excited to get rid of my workaround!

    Glad we could help!
    I just did the dirty work afterward :)

  11. kevinjdolan commented on Jan 19, 2024

    @kevinjdolan
    Author

    @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).

    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)

    This would be more consistent with the behavior when we see the second group fail (this test passes):

    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)

    This allows for composing workflows that are error-tolerant.

    Effectively, we want the semantics to be something like the following in async python:

    async def identity(arg):
        return arg
    
    async def fail(arg):
        raise MockException(arg)
    
    async def run():
        task_a = identity('a')
        task_b = fail('b')
        await asyncio.gather(task_a, task_b)
        task_c = identity('c')
        task_d = identity('d')
        await asyncio.gather(task_c, task_d)

    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 calling result.get().

    To demonstrate this, try:

    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"),
            ),
        )
        sig.link(tasks.identity.si("pass"))
        sig.link_error(tasks.identity.si("fail"))
        result = sig.apply_async()
        with self.assertRaises(tasks.MockException):
            result.get(TIMEOUT)

    In the worker logs, you will see the "fail" task is not run.

    worker_1  | [2024-01-19 22:44:00,551: INFO/MainProcess] Task tasks.identity[963f042b-3b4d-4c35-8644-d5f36a06e133] received
    worker_1  | [2024-01-19 22:44:00,553: INFO/ForkPoolWorker-2] Task tasks.identity[963f042b-3b4d-4c35-8644-d5f36a06e133] succeeded in 0.0013081219512969255s: 'a'
    worker_1  | [2024-01-19 22:44:00,555: INFO/MainProcess] Task tasks.fail[478b1fd2-c58f-4865-a18a-9df19c1c1bf5] received
    worker_1  | [2024-01-19 22:44:00,560: ERROR/ForkPoolWorker-2] Chord '87994db1-122f-46e4-98cb-ea0945a74d5b' raised: ChordError("Dependency 478b1fd2-c58f-4865-a18a-9df19c1c1bf5 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 453, 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 736, in __protected_call__
    worker_1  |     return self.run(*args, **kwargs)
    worker_1  |   File "/app/tasks.py", line 14, 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 478b1fd2-c58f-4865-a18a-9df19c1c1bf5 raised MockException('b')
    worker_1  | [2024-01-19 22:44:00,562: ERROR/ForkPoolWorker-2] Task tasks.fail[478b1fd2-c58f-4865-a18a-9df19c1c1bf5] raised unexpected: MockException('b')
    worker_1  | Traceback (most recent call last):
    worker_1  |   File "/usr/local/lib/python3.10/site-packages/celery/app/trace.py", line 453, 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 736, in __protected_call__
    worker_1  |     return self.run(*args, **kwargs)
    worker_1  |   File "/app/tasks.py", line 14, in fail
    worker_1  |     raise MockException(arg)
    worker_1  | tasks.MockException: b
    

    Compare this to a failure in the second group:

    worker_1  | [2024-01-19 22:45:14,446: INFO/MainProcess] Task tasks.identity[cbcf1c5c-0258-48bb-861b-bbf101205899] received
    worker_1  | [2024-01-19 22:45:14,447: INFO/ForkPoolWorker-2] Task tasks.identity[cbcf1c5c-0258-48bb-861b-bbf101205899] succeeded in 0.0010110409930348396s: 'a'
    worker_1  | [2024-01-19 22:45:14,451: INFO/MainProcess] Task tasks.identity[106161f7-add0-4c78-9331-aae98479baa2] received
    worker_1  | [2024-01-19 22:45:14,454: INFO/MainProcess] Task tasks.identity[de7ca18a-bba6-4b17-b823-7e432300bdee] received
    worker_1  | [2024-01-19 22:45:14,455: INFO/MainProcess] Task tasks.fail[195db9e3-4636-4d68-b07c-5f3ed7ae2982] received
    worker_1  | [2024-01-19 22:45:14,456: INFO/MainProcess] Task tasks.identity[2dda8655-9e94-47a3-a510-908916135d1e] received
    worker_1  | [2024-01-19 22:45:14,457: INFO/ForkPoolWorker-3] Task tasks.identity[de7ca18a-bba6-4b17-b823-7e432300bdee] succeeded in 0.0019437901210039854s: 'c'
    worker_1  | [2024-01-19 22:45:14,458: INFO/ForkPoolWorker-2] Task tasks.identity[106161f7-add0-4c78-9331-aae98479baa2] succeeded in 0.006650320021435618s: 'b'
    worker_1  | [2024-01-19 22:45:14,464: INFO/ForkPoolWorker-4] Task tasks.identity[2dda8655-9e94-47a3-a510-908916135d1e] succeeded in 0.0062230711337178946s: 'pass'
    worker_1  | [2024-01-19 22:45:14,473: INFO/MainProcess] Task tasks.identity[d1f40269-e9bc-4aab-b5bd-f1521f0a616d] received
    worker_1  | [2024-01-19 22:45:14,473: ERROR/ForkPoolWorker-1] Task tasks.fail[195db9e3-4636-4d68-b07c-5f3ed7ae2982] raised unexpected: MockException('d')
    worker_1  | Traceback (most recent call last):
    worker_1  |   File "/usr/local/lib/python3.10/site-packages/celery/app/trace.py", line 453, 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 736, in __protected_call__
    worker_1  |     return self.run(*args, **kwargs)
    worker_1  |   File "/app/tasks.py", line 14, in fail
    worker_1  |     raise MockException(arg)
    worker_1  | tasks.MockException: d
    worker_1  | [2024-01-19 22:45:14,474: INFO/ForkPoolWorker-2] Task tasks.identity[d1f40269-e9bc-4aab-b5bd-f1521f0a616d] succeeded in 0.0008630580268800259s: 'fail'

    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.

  12. kevinjdolan commented on Feb 2, 2024

    @kevinjdolan
    Author

    @Nusnus this issue is still present -- should I create a separate bug report?

  13. Nusnus commented on Feb 3, 2024

    @Nusnus
    Member

    @Nusnus this issue is still present -- should I create a separate bug report?

    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!

  14. reopened this on Feb 3, 2024
  15. FluxZA commented on Apr 13, 2024

    @FluxZA

    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.

  16. auvipy commented on Jun 24, 2025

    @auvipy
    Member

    Hey, most probably this might fix this #9774

  17. auvipy commented on Jun 26, 2025

    @auvipy
    Member

    @dfm88 may be this is also fixed by your PR?

  18. dfm88 commented on Jun 26, 2025

    @dfm88
    Contributor

    @dfm88 may be this is also fixed by your PR?

    mm not completely, in fact I also experienced timeouts when both the header and the body of a chord are groups. 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"),

    @flaky
    @pytest.mark.parametrize(
    "input_body",
    [
    (lambda: add.si(9, 7)),
    (
    lambda: chain(
    add.si(9, 7),
    add.si(5, 7),
    )
    ),
    pytest.param(
    (
    lambda: group(
    [
    add.si(9, 7),
    add.si(5, 7),
    ]
    )
    ),
    marks=pytest.mark.skip(reason="Task times out"),
    ),
    (
    lambda: chord(
    group(
    [
    add.si(1, 1),
    add.si(2, 2),
    ]
    ),
    add.si(10, 10),
    )
    ),
    ],
    ids=[
    "body is a single_task",
    "body is a chain",
    "body is a group",
    "body is a chord",
    ],
    )
    def test_chord_error_propagation_with_different_body_types(
    self, manager, caplog, input_body
    ) -> None:

  19. added a commit that references this issue on Jul 1, 2025
  20. auvipy commented on Jul 1, 2025

    @auvipy
    Member

    we got another pr to fix this

  21. added a commit that references this issue on Jul 1, 2025
  22. added a commit that references this issue on Jul 2, 2025
  23. added this to the 5.5.4 milestone on Jul 2, 2025
  24. modified the milestones: 5.5.4, 5.6.0 on Jul 13, 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