Repository: celery/celery

Issue:
task with an errback throws `AttributeError` when replaced with a chord, when `task_allow_error_cb_on_chord_header` is set
<!--
Please fill this template entirely and do not erase parts of it.
We reserve the right to close without a response
bug reports which are incomplete.
-->
# Checklist
<!--
To check an item on the list replace [ ] with [x].
-->
- [x] I have verified that the issue exists against the `main` branch of Celery.
- [ ] This has already been asked to the [discussions forum](https://github.com/celery/celery/discussions) first.
- [x] I have read the relevant section in the
  [contribution guide](https://docs.celeryq.dev/en/main/contributing.html#other-bugs)
  on reporting bugs.
- [x] I have checked the [issues list](https://github.com/celery/celery/issues?q=is%3Aissue+label%3A%22Issue+Type%3A+Bug+Report%22+-label%3A%22Category%3A+Documentation%22)
  for similar or identical bug reports.
- [x] I have checked the [pull requests list](https://github.com/celery/celery/pulls?q=is%3Apr+label%3A%22PR+Type%3A+Bugfix%22+-label%3A%22Category%3A+Documentation%22)
  for existing proposed fixes.
- [x] I have checked the [commit log](https://github.com/celery/celery/commits/main)
  to find out if the bug was already fixed in the main branch.
- [x] I have included all related issues and possible duplicate issues
  in this issue (If there are none, check this box anyway).

## Mandatory Debugging Information

- [x] 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).
- [x] I have verified that the issue exists against the `main` branch of Celery.
- [x] I have included the contents of ``pip freeze`` in the issue.
- [x] I have included all the versions of all the external dependencies required
  to reproduce this bug.

## Optional Debugging Information
<!--
Try some of the below if you think they are relevant.
It will help us figure out the scope of the bug and how many users it affects.
-->
- [ ] 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.
- [x] 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.
- [x] I have tried reproducing the issue after downgrading
  and/or upgrading Celery and its dependencies.

## Related Issues and Possible Duplicates
<!--
Please make sure to search and mention any related issues
or possible duplicates to this issue as requested by the checklist above.

This may or may not include issues in other repositories that the Celery project
maintains or other repositories that are dependencies of Celery.

If you don't know how to mention issues, please refer to Github's documentation
on the subject: https://help.github.com/en/articles/autolinked-references-and-urls#issues-and-pull-requests
-->

#### Related Issues

https://github.com/celery/celery/issues/8456

#### Possible Duplicates

- None

## Environment & Settings
<!-- Include the contents of celery --version below -->
**Celery version**: 5.3.6 (emerald-rush)
<!-- Include the output of celery -A proj report below -->
<details>
<summary><b><code>celery report</code> Output:</b></summary>
<p>

```
software -> celery:5.3.6 (emerald-rush) kombu:5.3.4 py:3.8.10
            billiard:4.2.0 redis:5.0.1
platform -> system:Darwin arch:64bit
            kernel version:23.1.0 imp:CPython
loader   -> celery.loaders.app.AppLoader
settings -> transport:redis results:redis://0.0.0.0:6479/

broker_url: 'redis://0.0.0.0:6479//'
result_backend: 'redis://0.0.0.0:6479/'
deprecated_settings: None
task_allow_error_cb_on_chord_header: True
```

</p>
</details>

# Steps to Reproduce

## Required Dependencies
<!-- Please fill the required dependencies to reproduce this issue -->
- **Minimal Python Version**: N/A or Unknown
- **Minimal Celery Version**: 5.3.1
- **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
<!-- Please fill the contents of pip freeze below -->
<details>
<summary><b><code>pip freeze</code> Output:</b></summary>
<p>

```
aiohttp==3.8.5
aiosignal==1.3.1
amqp==5.2.0
asgiref==3.6.0
astroid==2.15.2
async-timeout==4.0.3
attrs==22.2.0
autopep8==1.5.5
backports.zoneinfo==0.2.1
bandit==1.7.5
billiard==4.2.0
black==23.7.0
boto3-stubs==1.19.12.post1
botocore-stubs==1.29.107
celery==5.3.6
celery-stubs==0.1.3
certifi==2022.12.7
cfgv==3.3.1
charset-normalizer==3.1.0
click==8.1.7
click-didyoumean==0.3.0
click-plugins==1.1.1
click-repl==0.3.0
coreapi==2.3.3
coreschema==0.0.4
data-science-types==0.2.23
dill==0.3.6
distlib==0.3.6
Django==4.2
django-filter-stubs==0.1.3
django-stubs==1.15.0
django-stubs-ext==0.8.0
djangorestframework==3.14.0
djangorestframework-stubs==1.9.1
drf-yasg==1.20.3
factory-boy==3.2.1
Faker==18.3.4
filelock==3.10.7
flake8==3.8.4
frozenlist==1.4.0
fuzzywuzzy-stubs==0.0.1
gitdb==4.0.10
GitPython==3.1.31
graphene-stubs==0.15
identify==2.5.22
idna==3.4
inflection==0.5.1
isort==5.12.0
itypes==1.2.0
jedi==0.17.2
Jinja2==3.1.2
kombu==5.3.4
lazy-object-proxy==1.9.0
markdown-it-py==2.2.0
MarkupSafe==2.1.2
mccabe==0.6.1
mdurl==0.1.2
multidict==6.0.4
mypy==1.1.1
mypy-extensions==1.0.0
nodeenv==1.7.0
packaging==23.0
parso==0.7.1
pathspec==0.11.1
pbr==5.11.1
pip-licenses==3.5.5
platformdirs==3.2.0
pluggy==1.0.0
pre-commit==2.7.1
prompt-toolkit==3.0.41
PTable==0.9.2
pycodestyle==2.6.0
pydocstyle==6.3.0
pyflakes==2.2.0
Pygments==2.14.0
pylint==2.17.2
python-dateutil==2.8.2
python-jsonrpc-server==0.4.0
python-language-server==0.36.2
pytoolconfig==1.2.5
pytz==2023.3
PyYAML==6.0
ratelimit-stubs==2.2.1
redis==5.0.1
regex==2019.11.1
requests==2.28.2
rich==13.3.3
rope==1.7.0
ruamel.yaml==0.17.21
ruamel.yaml.clib==0.2.7
six==1.16.0
smmap==5.0.0
snowballstemmer==2.2.0
sqlparse==0.4.3
stevedore==5.0.0
toml==0.10.2
tomli==2.0.1
tomlkit==0.11.7
types-awscrt==0.16.13.post1
types-beautifulsoup4==4.10.20
types-docutils==0.19.1.7
types-pytz==2023.3.0.0
types-PyYAML==5.4.12
types-requests==2.25.12
types-setuptools==67.3.0.2
types-six==0.1.9
typing_extensions==4.5.0
tzdata==2023.3
ujson==5.7.0
uritemplate==4.1.1
urllib3==1.26.15
vine==5.1.0
virtualenv==20.21.0
wcwidth==0.2.12
wrapt==1.15.0
yapf==0.32.0
yarl==1.8.2
```

</p>
</details>

### Other Dependencies
<!--
Please provide system dependencies, configuration files
and other dependency information if applicable
-->
<details>
<p>
N/A
</p>
</details>

## Minimally Reproducible Test Case
<!--
Please provide a reproducible test case.
Refer to the Reporting Bugs section in our contribution guide.

We prefer submitting test cases in the form of a PR to our integration test suite.
If you can provide one, please mention the PR number below.
If not, please attach the most minimal code example required to reproduce the issue below.
If the test case is too large, please include a link to a gist or a repository below.
-->

<details>
<p>

```python
from celery import Celery, chain, chord, signature, group

app = Celery(
    "celery_bug", backend="redis://0.0.0.0:6479/", broker="redis://0.0.0.0:6479/"
)

app.conf.task_allow_error_cb_on_chord_header = True


@app.task(bind=True)
def orig_task(self, arg):
    chord_headers = group([chord_header.s(arg=f"arg{i}") for i in range(5)])
    replacement_chord = chord(chord_headers, chord_body.s())
    return self.replace(replacement_chord)


@app.task
def chord_header(arg):
    return f"header: {arg}"


@app.task
def chord_body(arg):
    return f"body: {arg}]"


@app.task
def handle_error(*args, **kwargs):
    print(f"handle error called with args {args} kwargs {kwargs}")


def main():
    print(f"hello world")
    res = orig_task.apply_async(args=["spam"], link_error=handle_error.s())
    print(f"RESULT: {res.get()}")


if __name__ == "__main__":
    main()

```

</p>
</details>

# Expected Behavior
<!-- Describe in detail what you expect to happen -->
I would expect the `orig_task` to be replaced with `replacement_chord` and give expected output.

This is the expected output that I do see if `task_allow_error_cb_on_chord_header` is `False`, or if the `orig_task` is called without the `link_error=` callback:  

```
$ poetry run python celery_bug.py 
hello world
RESULT: body: ['header: arg0', 'header: arg1', 'header: arg2', 'header: arg3', 'header: arg4']]
```
# Actual Behavior
<!--
Describe in detail what actually happened.
Please include a backtrace and surround it with triple backticks (```).
In addition, include the Celery daemon logs, the broker logs,
the result backend logs and system logs below if they will help us debug
the issue.
-->
Instead, I get this `AttributeError`:
```
$ poetry run python celery_bug.py 
hello world
Traceback (most recent call last):
  File "celery_bug.py", line 39, in <module>
    main()
  File "celery_bug.py", line 35, in main
    print(f"RESULT: {res.get()}")
  File "/Users/robertgalloway/Library/Caches/pypoetry/virtualenvs/rpg-play-aJQQ1jqR-py3.8/lib/python3.8/site-packages/celery/result.py", line 251, in get
    return self.backend.wait_for_pending(
  File "/Users/robertgalloway/Library/Caches/pypoetry/virtualenvs/rpg-play-aJQQ1jqR-py3.8/lib/python3.8/site-packages/celery/backends/asynchronous.py", line 223, in wait_for_pending
    return result.maybe_throw(callback=callback, propagate=propagate)
  File "/Users/robertgalloway/Library/Caches/pypoetry/virtualenvs/rpg-play-aJQQ1jqR-py3.8/lib/python3.8/site-packages/celery/result.py", line 365, in maybe_throw
    self.throw(value, self._to_remote_traceback(tb))
  File "/Users/robertgalloway/Library/Caches/pypoetry/virtualenvs/rpg-play-aJQQ1jqR-py3.8/lib/python3.8/site-packages/celery/result.py", line 358, in throw
    self.on_ready.throw(*args, **kwargs)
  File "/Users/robertgalloway/Library/Caches/pypoetry/virtualenvs/rpg-play-aJQQ1jqR-py3.8/lib/python3.8/site-packages/vine/promises.py", line 235, in throw
    reraise(type(exc), exc, tb)
  File "/Users/robertgalloway/Library/Caches/pypoetry/virtualenvs/rpg-play-aJQQ1jqR-py3.8/lib/python3.8/site-packages/vine/utils.py", line 27, in reraise
    raise value
AttributeError: 'dict' object has no attribute 'clone'
```

I suspect the error is related to this code in the `_chord.link_error()` method:
```python
        if self.app.conf.task_allow_error_cb_on_chord_header:
            for task in maybe_list(self.tasks) or []:
                task.link_error(errback.clone(immutable=True))
```



Discussion:
previous related fix  https://github.com/celery/celery/pull/8463 

Resolving patch:
diff --git a/celery/canvas.py b/celery/canvas.py
--- a/celery/canvas.py
+++ b/celery/canvas.py
@@ -2271,6 +2271,8 @@ def link_error(self, errback):
             ``False`` (the current default), then the error callback will only be
             applied to the body.
         """
+        errback = maybe_signature(errback)
+
         if self.app.conf.task_allow_error_cb_on_chord_header:
             for task in maybe_list(self.tasks) or []:
                 task.link_error(errback.clone(immutable=True))


Test patch:
diff --git a/t/unit/tasks/test_canvas.py b/t/unit/tasks/test_canvas.py
--- a/t/unit/tasks/test_canvas.py
+++ b/t/unit/tasks/test_canvas.py
@@ -1688,6 +1688,14 @@ def test_flag_allow_error_cb_on_chord_header_various_header_types(self):
             errback = c.link_error(sig)
             assert errback == sig
 
+    @pytest.mark.usefixtures('depends_on_current_app')
+    def test_flag_allow_error_cb_on_chord_header_with_dict_callback(self):
+        self.app.conf.task_allow_error_cb_on_chord_header = True
+        c = chord(group(signature('th1'), signature('th2')), signature('tbody'))
+        errback_dict = dict(signature('tcb'))
+        errback = c.link_error(errback_dict)
+        assert errback == errback_dict
+
     def test_chord__or__group_of_single_task(self):
         """ Test chaining a chord to a group of a single task. """
         c = chord([signature('header')], signature('body'))