Skip to content

fix(canvas): give a chord's link/link_error options to its body only - #10778

Merged
auvipy merged 18 commits into
celery:mainfrom
subham-hq:fix/chord-link-option-crash
Oct 26, 2026
Merged

auvipy merged 18 commits into
celery:mainfrom
subham-hq:fix/chord-link-option-crash

Conversation

@subham-hq

@subham-hq subham-hq commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Description

A chord given link or link_error as an option (rather than through chord.link()) raises TypeError instead of attaching the callback to its body.

chord([add.s(1, 1), add.s(2, 2)], tsum.s()).apply_async(link=cb.s())
# TypeError: Cannot add link to group: use a chord

chord([add.s(1, 1), add.s(2, 2)], tsum.s()).apply_async(link_error=eb.s())
# TypeError: Cannot add link to group: do that on individual tasks

The same bug also breaks a chain whose last step is a chord, when the chain has a callback. That failure happens on the worker:

chain(add.s(1, 1), chord([add.s(10), add.s(20)], tsum.s())).apply_async(link=cb.s())

_chain.run() stores the chain's callback in the chord's options, and the chord travels in the first message's chain field. After add(1, 1) succeeds, the worker applies the chord. That raises the TypeError, so add is stored as FAILURE even though it returned 2. The chord header is never published, .get() raises the TypeError, and the callback never runs. task_always_eager takes a different path (chord.apply()), so eager test suites do not catch it.

Cause

_chord.run() copies the merged options onto the body. That part is correct: the result of a chord is the result of its body, and chord.link() and chord.link_error() already link to the body. It then passes the same options to header.apply_async(). Before that call it removes only chain, chord and task_id, so link and link_error reach the header group, which always rejects them (group.apply_async).

Fix

In _chord.run(), remove link and link_error from the options and add each callback to the body with append_to_list_option(), the helper Signature.link() uses. The header no longer receives them. Two properties follow:

  • Callbacks already on the body are kept. For example, chain(a, chord(h, body.link(x))).apply_async(link=y) runs both x and y once. A plain dict.update would have dropped x.
  • Callbacks are never duplicated. chord.apply_async(link=cb) also clones the body with link=cb, so the callback reaches run() twice, and the second append is skipped.

Errbacks attached through chord.link_error(), which is also how chains attach them (prepare_steps), are unchanged, including the task_allow_error_cb_on_chord_header behavior.

Before / after (in-process worker, memory:// broker, cache+memory:// backend)

Scenario Before After
chord(...).apply_async(link=cb) TypeError on the client result 6, cb called once
chord(...).apply_async(link_error=eb) with a failing body TypeError on the client eb called once
chain(add, chord(h, body)).apply_async(link=cb) add stored as FAILURE; chord never runs result 34, cb called once
same, with the body's own .link(own) TypeError own and cb each called once
chain(add, chord(...)).on_error(eb) works works (unchanged)

Tests

  • test_chord::test_link_option_goes_to_body_not_header[link|link_error]: the option is on the body, and no header task gets it.
  • test_chord::test_chain_link_to_chord_step_keeps_body_callbacks: a chain ending in a chord is JSON round-tripped and applied the way the worker continues a chain. The body keeps its own callback and gains the chain's, and the header gets no link.

All 3 new tests fail on main with the TypeError above and pass with this change. The full t/unit suite passes except for failures that also occur on unmodified main in my environment (root user, no broker). flake8, isort and codespell are clean on the changed files.

Added an entry under Unreleased in Changelog.rst.

subham-hq and others added 2 commits October 3, 2026 17:22
_chord.run() copied link and link_error onto the body, which is correct
because the result of a chord is the result of its body. It then also
passed them to header.apply_async(). A group always rejects these
options, so the chord raised:

    TypeError: Cannot add link to group: use a chord

This affected chord.apply_async(link=...) and link_error=. It also
affected any chain whose last step is a chord when the chain has a
callback: _chain.run() stores the callback in the chord's options. The
first task's message carries the chord in its 'chain' field. When that
task succeeds, the worker applies the chord and raises the TypeError.
The task is marked FAILURE even though it succeeded, the header is
never published and the callback never runs. Eager mode does not take
this path, so tests with task_always_eager did not catch it.

Drop link/link_error from the header options and append them to the
body with append_to_list_option(), as chord.link() does. Callbacks
already on the body are kept and duplicates are skipped.

@auvipy auvipy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which issue you are trying to fix with this pr, can you point to this?

@subham-hq

Copy link
Copy Markdown
Contributor Author

which issue you are trying to fix with this pr, can you point to this?

Thanks for the review! There was no existing issue for this. I found it while reading _chord.run() and confirmed it on a worker, so I've opened #10781 with a minimal reproduction and added "Fixes #10781" to the description.

In short: chord(...).apply_async(link=cb) raises TypeError: Cannot add link to group: use a chord. A chain that ends in a chord and has a callback fails on the worker instead: the task before the chord is stored as FAILURE, and the chord never runs.

@auvipy auvipy added this to the 5.7.x milestone Oct 4, 2026

@MehrazRumman MehrazRumman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked this out and ran it against main, including through an in-process worker with the memory broker.

The fix works

Case main this PR
chord(...).apply_async(link=cb) TypeError: Cannot add link to group body runs, callback runs with the body result
chord(...).apply_async(link_error=eb) TypeError errback on the body, nothing on the header
chain(add, chord).apply_async(link=cb) with a real worker first task marked FAILURE, chord never runs chord completes, callback runs
Same chain, body already has its own callback nothing runs both callbacks run

The three new tests fail on main and pass here. With the branch merged onto current main, the canvas, chord, builtins and stamping unit tests pass.

Needs a rebase

The branch conflicts with main in Changelog.rst: 5.7.0b1 was released and the "Unreleased" section this PR adds to is gone. celery/canvas.py and the tests merge cleanly.

Two behaviours that differ from the description

  1. "Existing body callbacks are preserved" holds for the chain path and for chord.set(link=...), but not for a direct call:
   body = tsum.s()
   body.link(cb2.s())
   chord([add.s(1, 1), add.s(2, 2)], body).apply_async(link=cb.s())
   # body link is [cb]; cb2 is dropped

The cause is in apply_async, not in the new code: body.clone(**self._body_options(options)) replaces the body's link before run() appends to it. A plain task also replaces (sig.link(cb2); sig.apply_async(link=cb) sends only cb), so replacing may be acceptable. But the two paths now behave differently, and chord.link(cb) keeps both. Please either make the direct path append as well, or state the difference and add a test that pins it.

  1. With task_allow_error_cb_on_chord_header=True, chord.link_error(eb) adds the errback to every header task and to the body. chord.apply_async(link_error=eb) with this PR adds it to the body only. The comment says "as with chord.link() and chord.link_error()", which is not accurate for that setting. Reusing the same logic or adjusting the comment would both be fine.

Summary

The core change is correct and fixes #10781. I would merge after the rebase, with point 1 either fixed or documented and tested.

…link=

chord.apply_async() cloned the body with the link / link_error options
it was given, which replaced the body's own callbacks before run()
appended the chord's ones. chord(h, body.link(cb2)).apply_async(link=cb)
called only cb, while chord.link(cb) and the chain path call both.

Take link and link_error out of the options before cloning. run() still
gets them, so a link given to apply_async() replaces one set with
chord.set(link=...), as for any signature, and appends it to the body's
own callbacks. The eager path does not go through run(): add the
chord's callbacks to the body there too, so that it runs the same
callbacks as a worker, chord.set(link=...) included.

The option form of link_error is added to the body only, also with
task_allow_error_cb_on_chord_header enabled, where chord.link_error()
also links the header tasks. Say so in the helper's docstring instead
of claiming it matches chord.link_error(), and pin it with a test.
@subham-hq

Copy link
Copy Markdown
Contributor Author

I checked this out and ran it against main, including through an in-process worker with the memory broker.

The fix works

Case main this PR
chord(...).apply_async(link=cb) TypeError: Cannot add link to group body runs, callback runs with the body result
chord(...).apply_async(link_error=eb) TypeError errback on the body, nothing on the header
chain(add, chord).apply_async(link=cb) with a real worker first task marked FAILURE, chord never runs chord completes, callback runs
Same chain, body already has its own callback nothing runs both callbacks run
The three new tests fail on main and pass here. With the branch merged onto current main, the canvas, chord, builtins and stamping unit tests pass.

Needs a rebase

The branch conflicts with main in Changelog.rst: 5.7.0b1 was released and the "Unreleased" section this PR adds to is gone. celery/canvas.py and the tests merge cleanly.

Two behaviours that differ from the description

  1. "Existing body callbacks are preserved" holds for the chain path and for chord.set(link=...), but not for a direct call:
   body = tsum.s()
   body.link(cb2.s())
   chord([add.s(1, 1), add.s(2, 2)], body).apply_async(link=cb.s())
   # body link is [cb]; cb2 is dropped

The cause is in apply_async, not in the new code: body.clone(**self._body_options(options)) replaces the body's link before run() appends to it. A plain task also replaces (sig.link(cb2); sig.apply_async(link=cb) sends only cb), so replacing may be acceptable. But the two paths now behave differently, and chord.link(cb) keeps both. Please either make the direct path append as well, or state the difference and add a test that pins it.

  1. With task_allow_error_cb_on_chord_header=True, chord.link_error(eb) adds the errback to every header task and to the body. chord.apply_async(link_error=eb) with this PR adds it to the body only. The comment says "as with chord.link() and chord.link_error()", which is not accurate for that setting. Reusing the same logic or adjusting the comment would both be fine.

Summary

The core change is correct and fixes #10781. I would merge after the rebase, with point 1 either fixed or documented and tested.

Thanks a lot for testing this so thoroughly, including the worker path!

  1. Good catch, fixed in b102cf5. chord.apply_async() now takes link / link_error out of the options before cloning the body, so they are appended to the body's own callbacks (as chord.link() does) instead of replacing them. That holds on a worker and in eager mode. A link passed to apply_async() still replaces one set with chord.set(link=...), as for any signature. test_link_option_keeps_body_callbacks, test_link_option_replaces_link_set_on_chord and test_link_set_on_chord_goes_to_body pin this, each for both paths.

  2. I adjusted the comment. The option form adds link_error to the body only, also with task_allow_error_cb_on_chord_header=True. Reusing chord.link_error() here would mean refactoring it, and fix(canvas): link errbacks to nested chords in a frozen chord header #10784 changes that same method, so I kept this PR away from it. The helper's docstring now states the difference, and test_link_option_goes_to_body_not_header is parametrized over the setting.

  3. Merged main in 5206604. The 5.7.0b1 release removed the Unreleased section, so I re-created it above the release notes for this entry, as Fix escaped percent signs in node formatting #10663 did after 5.7.0a1. I merged instead of rebasing because a maintainer commit is already on the branch.

@subham-hq

Copy link
Copy Markdown
Contributor Author

@auvipy I've linked #10781 in the description and addressed MehrazRumman's review. Could you take another look when you have time?

The changelog is written at release time, and 5.7.0rc1 rewrote
Changelog.rst, so the entry only causes conflicts. Leave it out of this
change, as asked in review.
Comment thread celery/canvas.py Resolved

@auvipy auvipy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also it might be a good idea to have additional integration tests as well

Run the cases from celery#10781 on a real worker:
chord.apply_async(link=...) with a body that has its own callback, a
chain ending in a chord with a callback, and apply_async(link_error=...)
with a failing body that has its own errback. All three raise TypeError
on main.

Also pin that link=None / link_error=None keep the body's own callbacks,
which main replaced with None.
@subham-hq

Copy link
Copy Markdown
Contributor Author

also it might be a good idea to have additional integration tests as well

Added three integration tests in e3833bd (t/integration/test_canvas.py::test_chord):

All three fail on main with TypeError: Cannot add link to group and pass on this branch (redis broker and backend). The rest of t/integration/test_canvas.py passes too.

@auvipy

auvipy commented Oct 24, 2026

Copy link
Copy Markdown
Member

@ChristianMurphy would you mind reviewing this?

@codecov

codecov Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.06%. Comparing base (54f5a21) to head (4a1f431).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #10778      +/-   ##
==========================================
+ Coverage   91.04%   91.06%   +0.02%     
==========================================
  Files         153      153              
  Lines       21586    21638      +52     
  Branches     2661     2677      +16     
==========================================
+ Hits        19653    19705      +52     
  Misses       1624     1624              
  Partials      309      309              
Flag Coverage Δ
unittests 91.04% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@ChristianMurphy

Copy link
Copy Markdown
Contributor

I tested this with a solo worker on Redis and in eager mode, comparing it with main.

The fix works. On main, passing link= or link_error= to chord.apply_async() raises TypeError: Cannot add link to group. On this branch, the callback runs once after the body, and the body's own callbacks still run. I saw the same result for a chain that ends in a chord, a chord used as the body, and links set by chord.set(link=...). The new integration tests fail on main and pass here.

I'd change two things before merging.

1. With a group as the body, link= runs the callback once per task.

chord(header, group(x.s(), y.s())).apply_async(link=cb.s())

This runs cb twice, once with each body task's result. chord.link(cb.s()) on the same chord runs it once. The canvas docs say chord callbacks run "just once upon completion of the body", so I think the link= form should run it once too.

In _add_body_callbacks, calling body.link(callback) and body.link_error(errback) instead of append_to_list_option makes the two forms match. I tried it locally. The group case then runs cb once, nothing else changes, and the canvas and chord tests pass.

2. Please document the option form in canvas.rst.

Right now it's only explained in a private method's docstring, and it differs from on_error() in one case. With task_allow_error_cb_on_chord_header on and a failing header task, on_error(eb) calls eb twice: once for the header task and once for the body. link_error=eb calls it once. The "Chords" section says that setting makes header tasks call the errback, so readers will expect it here too. Something like this in that section:

You can also pass ``link`` and ``link_error`` to ``chord.apply_async()``.
They are added to the body, after its own callbacks. The
:setting:`task_allow_error_cb_on_chord_header` setting doesn't apply to
them, so an errback passed this way isn't added to the header tasks.

@auvipy
auvipy requested a review from Nusnus October 24, 2026 16:33
@auvipy auvipy removed their assignment Oct 24, 2026
@ChristianMurphy

Copy link
Copy Markdown
Contributor

I re-tested e7a5789 against its merge base with main, with a solo worker on Redis.

link= works now. With a group body, chord(header, group(x.s(), y.s())).apply_async(link=cb.s()) runs cb once with no arguments, the same as chord.link(). Applying the same chord twice with a different link= each time runs each callback once, so the caller's chord isn't changed.

The docs paragraph I suggested no longer matches for group bodies.

It says both options "are added to the body, after its own callbacks". With a group body, link= goes on the first task only, as with chord.link(). And link_error= replaces the errbacks of the body's tasks. When a body task with its own errback fails, only the link_error= errback runs: the log shows opt:err:ValueError('boom') and no own entry, where chord.on_error() runs both. Something like this would describe it:

You can also pass ``link`` and ``link_error`` to ``chord.apply_async()``.
``link`` is linked to the body the same way ``chord.link()`` links it.
``link_error`` is added to the body's options. When the body is a group,
a group task that fails calls this errback instead of its own errbacks,
and a failed header task calls it once for each task in the group. The
:setting:`task_allow_error_cb_on_chord_header` setting doesn't apply to
``link_error``, so an errback passed this way isn't added to the header
tasks.

The unit tests still pass with the chord branch of _clone_with_members removed.

Without that branch, a chord body whose own body is a group gets the link written into the caller's group. After _add_body_callbacks, the caller's x.body.body.tasks[0].options['link'] is [cb()]. A case in test_link_option_on_chord_body_goes_to_its_body with a group as the inner body would catch it.

_clone_with_members() also cloned the header of a chord. That was only
needed while link_error went through chord.link_error(), which links the
header tasks; link only reaches the body of a chord, so drop it.

Cover a group as the body of a chord body in
test_link_option_on_chord_body_goes_to_its_body: without the chord
branch, the callback was linked to the caller's group.

Describe the option form as it behaves with a group body: link goes on
its first task, as with chord.link(), and link_error replaces the
errbacks of the tasks when one fails and is called once per task when
the header fails.
@subham-hq

Copy link
Copy Markdown
Contributor Author

I re-tested e7a5789 against its merge base with main, with a solo worker on Redis.

link= works now. With a group body, chord(header, group(x.s(), y.s())).apply_async(link=cb.s()) runs cb once with no arguments, the same as chord.link(). Applying the same chord twice with a different link= each time runs each callback once, so the caller's chord isn't changed.

The docs paragraph I suggested no longer matches for group bodies.

It says both options "are added to the body, after its own callbacks". With a group body, link= goes on the first task only, as with chord.link(). And link_error= replaces the errbacks of the body's tasks. When a body task with its own errback fails, only the link_error= errback runs: the log shows opt:err:ValueError('boom') and no own entry, where chord.on_error() runs both. Something like this would describe it:

You can also pass ``link`` and ``link_error`` to ``chord.apply_async()``.
``link`` is linked to the body the same way ``chord.link()`` links it.
``link_error`` is added to the body's options. When the body is a group,
a group task that fails calls this errback instead of its own errbacks,
and a failed header task calls it once for each task in the group. The
:setting:`task_allow_error_cb_on_chord_header` setting doesn't apply to
``link_error``, so an errback passed this way isn't added to the header
tasks.

The unit tests still pass with the chord branch of _clone_with_members removed.

Without that branch, a chord body whose own body is a group gets the link written into the caller's group. After _add_body_callbacks, the caller's x.body.body.tasks[0].options['link'] is [cb()]. A case in test_link_option_on_chord_body_goes_to_its_body with a group as the inner body would catch it.

Thanks for re-testing on a real worker!

  • Docs: you're right, "added to the body, after its own callbacks" was wrong for group bodies. I've replaced it with your paragraph, inside the versionchanged:: 5.7 block auvipy asked for, after checking each sentence on a solo worker: with a group body, link= goes on the first task only, a failing body task calls the link_error= errback instead of its own, a header failure calls it once per body task, and the counts are the same with task_allow_error_cb_on_chord_header on.
  • Tests: test_link_option_on_chord_body_goes_to_its_body now also covers a group as the inner body, compared with chord.link(), and it fails without the chord branch of _clone_with_members. While checking that, I found that the chord header part of that function no longer did anything, since link_error doesn't go through chord.link_error() any more, so I removed it. Each remaining branch of the helper now has a unit test that fails without it.

Fixed in d7ee792.

@auvipy
auvipy self-requested a review October 24, 2026 18:05
Comment thread t/integration/test_canvas.py Resolved
Comment thread celery/canvas.py Resolved

@MehrazRumman MehrazRumman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I re-checked b6bb6ec. The canvas, chord and stamping unit tests pass (850 passed). I also compared it with main using an in-process worker (memory broker, cache+memory:// backend).

link= looks good now. The body keeps its own callbacks, the caller's signature isn't changed, and a group body is linked once, the same as with chord.link().

link_error= with a group body drops the errbacks of the group's tasks

failing = boom.s()
failing.link_error(eb.si(name='own'))
c = chord([add.si(1, 1)], group(failing, tsum.s()))

c.link_error(eb.si(name='chord')); c.apply_async()  # errbacks run: chord, own
c.apply_async(link_error=eb.si(name='chord'))       # errbacks run: chord

_add_body_callbacks() puts the errback in the group's own link_error option. When the body fires, group._freeze_gid() merges the group's options into each task's options, and Signature._merge() replaces each task's link_error instead of adding to it. So own never runs.

The docs paragraph does say this ("a group task that fails calls this errback instead of its own errbacks"), so it's intended. But the PR's goal is to keep callbacks already on the body. link= keeps them, and so does chord.link_error(), so with a group body link_error= is the only path that loses them. A user moving from chord.link_error(eb) to apply_async(link_error=eb) would silently lose the errbacks on the group's tasks.

I think it would be better for the group's tasks to keep their own errbacks. For example, the errback could also be appended to each task with append_to_list_option(), and the group-level option could be kept only for the header-failure path in on_chord_part_return, without reaching the tasks. If replacing them is the intended behaviour, please add a test that pins it. Right now test_link_error_option_stays_on_group_or_chord_body only checks body.options, and no test covers a group task that has its own errback and fails.

Everything else looks fine to me.

@ChristianMurphy

Copy link
Copy Markdown
Contributor

Re-tested b6bb6ec on a worker. link= with a group body runs once, and the docs paragraph matches what the worker does.

Please make link_error= keep the group tasks' own errbacks and still run when a header task fails. I tried four placements, with a group body where one task has its own errback, own:

Where the errback goes Header task fails Body task fails
In the group's option (this PR) chord twice chord
On each task, as chord.link_error() does none chord, own
On each task and in the group's option, as MehrazRumman suggested chord twice chord
On each task, with _handle_group_chord_error() also calling the tasks' errbacks chord twice chord, own

When the group runs, its option is merged into each task and replaces the task's own errbacks. On a header failure, _handle_group_chord_error() (celery/backends/base.py:518) reads only the group's option. To get the last row, link the errback to the group's tasks in _add_body_callbacks, and have _handle_group_chord_error() call each task's errbacks too.

That also makes chord.link_error() run on a header failure with a group body, which it doesn't today, so it needs a changelog line. Six _handle_group_chord_error tests in t/unit/backends/test_base.py pass a Mock as the group and would need updating.

The change I used in celery/backends/base.py
tasks = list(getattr(group_callback, 'tasks', []) or [])
for index, result in enumerate(frozen_group.results):
    try:
        task = tasks[index] if index < len(tasks) else None
        task_errbacks = task.options.get("link_error", []) if task is not None else []
        fake_request = _create_fake_task_request(
            task_id=result.id,
            errbacks=list(group_callback.options.get("link_error", [])) + list(task_errbacks),
            ...

@auvipy

auvipy commented Oct 26, 2026

Copy link
Copy Markdown
Member

@subham-hq check the last two review please

auvipy and others added 2 commits October 26, 2026 11:43
link_error= on a chord with a group body was added to the group's own
link_error option. When the group runs, its options are merged into
each task's and replace the task's own link_error, so a body task that
failed called the chord's errback but not its own. chord.link_error()
keeps them: it links the errback to each task.

Link it to each task the same way. A chord in the group gets it on its
body only, as a chord body does, so it doesn't reach that chord's
header or raise the header deprecation warning. If the group has its
own link_error, even an empty one, it goes there instead, with the
group's own errbacks: when the group runs, that option replaces the
errbacks of its tasks and is passed on to a chord in it, so linking it
to that chord's body as well would call it twice.

When the header fails, the chord's error handling only called the
group's own errbacks, so an errback linked to the tasks did not run,
with link_error= now or with chord.link_error() before.
_handle_group_chord_error() now calls, for each result of the body,
the errbacks of its task and those of the group, each once. A chord in
the group has the result of its body, so its errbacks are read from
there, and a link_error option holding one signature is read as one
errback.
Comment thread docs/userguide/canvas.rst Resolved
@auvipy

auvipy commented Oct 26, 2026

Copy link
Copy Markdown
Member

@subham-hq can you please check if this PR #10907 is fixing part of your pr or having duplicate effort or partial duplicate effort there?

@subham-hq

subham-hq commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Re-tested b6bb6ec on a worker. link= with a group body runs once, and the docs paragraph matches what the worker does.

Please make link_error= keep the group tasks' own errbacks and still run when a header task fails. I tried four placements, with a group body where one task has its own errback, own:

Where the errback goes Header task fails Body task fails
In the group's option (this PR) chord twice chord
On each task, as chord.link_error() does none chord, own
On each task and in the group's option, as MehrazRumman suggested chord twice chord
On each task, with _handle_group_chord_error() also calling the tasks' errbacks chord twice chord, own
When the group runs, its option is merged into each task and replaces the task's own errbacks. On a header failure, _handle_group_chord_error() (celery/backends/base.py:518) reads only the group's option. To get the last row, link the errback to the group's tasks in _add_body_callbacks, and have _handle_group_chord_error() call each task's errbacks too.

That also makes chord.link_error() run on a header failure with a group body, which it doesn't today, so it needs a changelog line. Six _handle_group_chord_error tests in t/unit/backends/test_base.py pass a Mock as the group and would need updating.

The change I used in celery/backends/base.py

Thanks for comparing the four placements, that made the choice easy. Done in 397bcb5, the last row of your table:

  • _add_body_callbacks() links link_error to each task of a group body, as group.link_error() does, so the tasks keep their own errbacks. A chord that is a task of the group gets it on its body only, never on its header, so the task_allow_error_cb_on_chord_header warning isn't raised. If the group has its own link_error, the errback is added there instead, since that's where the tasks get their errbacks from when the group runs (and a chord in the group passes them on to its body, so linking it there as well would call it twice). Other bodies still get it in their own options.
  • _handle_group_chord_error() now calls, for each body result, the errbacks of its task and those of the group, each errback once. Results are matched to tasks by the ids that freezing gave them; a chord in the group has the result of its body, so its errbacks are read from there. A link_error holding a single signature is read as one errback rather than iterated as a dict.

On a solo worker with Redis, with a group body where one task has its own errback: when a header task fails, chord runs once per body task, plus own (each body task is marked failed, the same way a task body's own errbacks already run in that case); when a body task fails, chord and own run. chord.link_error() with a group body now runs on a header failure too. test_errbacks_of_group_body_tasks_run_on_header_failure[option|method] and test_errbacks_of_chord_in_group_body_run_on_header_failure[option|method] cover both forms; test_errback_set_as_one_signature_runs_on_header_failure, test_link_error_option_with_link_error_of_group_body and test_link_error_option_runs_once_for_chord_in_group_body_with_link_error cover the single-signature and group-level link_error cases.

The _handle_group_chord_error unit tests now give the mock group real task signatures, and new ones cover the lookup, chords in the group and single-signature errbacks. The behaviour change is described in the versionchanged:: 5.7 note in canvas.rst. I left Changelog.rst alone, since PRs have been asked to leave it to release time (e.g. #10837), but I can add a line if you'd prefer.

Three cases are unchanged: chord.link_error() with a chord as the body still isn't called when the outer header fails, because it links the inner body and chord_error_from_stack reads the inner chord's own options; for a chain in a group body, a header failure calls the errbacks linked to the chain, not those of its steps; and in eager mode a chord in a group body ignores the options the group passes it (chord.apply() doesn't read them), so when the group has its own link_error, an errback passed as link_error= doesn't reach that chord's body, the same as the group's own errbacks before this PR. I can look at those separately.

@subham-hq

Copy link
Copy Markdown
Contributor Author

I re-checked b6bb6ec. The canvas, chord and stamping unit tests pass (850 passed). I also compared it with main using an in-process worker (memory broker, cache+memory:// backend).

link= looks good now. The body keeps its own callbacks, the caller's signature isn't changed, and a group body is linked once, the same as with chord.link().

link_error= with a group body drops the errbacks of the group's tasks

failing = boom.s()
failing.link_error(eb.si(name='own'))
c = chord([add.si(1, 1)], group(failing, tsum.s()))

c.link_error(eb.si(name='chord')); c.apply_async()  # errbacks run: chord, own
c.apply_async(link_error=eb.si(name='chord'))       # errbacks run: chord

_add_body_callbacks() puts the errback in the group's own link_error option. When the body fires, group._freeze_gid() merges the group's options into each task's options, and Signature._merge() replaces each task's link_error instead of adding to it. So own never runs.

The docs paragraph does say this ("a group task that fails calls this errback instead of its own errbacks"), so it's intended. But the PR's goal is to keep callbacks already on the body. link= keeps them, and so does chord.link_error(), so with a group body link_error= is the only path that loses them. A user moving from chord.link_error(eb) to apply_async(link_error=eb) would silently lose the errbacks on the group's tasks.

I think it would be better for the group's tasks to keep their own errbacks. For example, the errback could also be appended to each task with append_to_list_option(), and the group-level option could be kept only for the header-failure path in on_chord_part_return, without reaching the tasks. If replacing them is the intended behaviour, please add a test that pins it. Right now test_link_error_option_stays_on_group_or_chord_body only checks body.options, and no test covers a group task that has its own errback and fails.

Everything else looks fine to me.

Thanks for digging into this! You're right, losing the tasks' own errbacks went against the point of the PR. Fixed in 397bcb5: with a group body, link_error= is now linked to each task, the same way chord.link_error() does it, so the tasks keep their own errbacks, and your example now runs both chord and own. test_link_error_option_on_group_body_is_like_chord_link_error pins it against chord.link_error(), with a task that has its own errback, and test_link_error_option_keeps_errbacks_of_group_body_tasks checks it on a worker.

@subham-hq

Copy link
Copy Markdown
Contributor Author

@auvipy I've addressed both reviews in 397bcb5. Could you take another look?

@auvipy

auvipy commented Oct 26, 2026

Copy link
Copy Markdown
Member

we do not need changelog entries. instead put all the change in the docs section. the maintainers will reformat the changelog before release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants