Repository navigation
fix(canvas): give a chord's link/link_error options to its body only - #10778
Conversation
_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
left a comment
There was a problem hiding this comment.
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 In short: |
MehrazRumman
left a comment
There was a problem hiding this comment.
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
- "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 droppedThe 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.
- 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.
Thanks a lot for testing this so thoroughly, including the worker path!
|
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.
auvipy
left a comment
There was a problem hiding this comment.
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.
Added three integration tests in e3833bd (
All three fail on main with |
|
@ChristianMurphy would you mind reviewing this? |
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
|
I tested this with a solo worker on Redis and in eager mode, comparing it with main. The fix works. On main, passing I'd change two things before merging. 1. With a group as the body, chord(header, group(x.s(), y.s())).apply_async(link=cb.s())This runs In 2. Please document the option form in Right now it's only explained in a private method's docstring, and it differs from 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. |
|
I re-tested e7a5789 against its merge base with main, with a solo worker on Redis.
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, 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 Without that branch, a chord body whose own body is a group gets the link written into the caller's group. After |
_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.
Thanks for re-testing on a real worker!
Fixed in d7ee792. |
MehrazRumman
left a comment
There was a problem hiding this comment.
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.
|
Re-tested b6bb6ec on a worker. Please make
When the group runs, its option is merged into each task and replaces the task's own errbacks. On a header failure, That also makes The change I used in
|
|
@subham-hq check the last two review please |
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.
|
@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? |
Thanks for comparing the four placements, that made the choice easy. Done in 397bcb5, the last row of your table:
On a solo worker with Redis, with a group body where one task has its own errback: when a header task fails, The Three cases are unchanged: |
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, |
|
we do not need changelog entries. instead put all the change in the docs section. the maintainers will reformat the changelog before release |
Description
A chord given
linkorlink_erroras an option (rather than throughchord.link()) raisesTypeErrorinstead of attaching the callback to its body.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.run()stores the chain's callback in the chord's options, and the chord travels in the first message'schainfield. Afteradd(1, 1)succeeds, the worker applies the chord. That raises theTypeError, soaddis stored asFAILUREeven though it returned2. The chord header is never published,.get()raises theTypeError, and the callback never runs.task_always_eagertakes 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, andchord.link()andchord.link_error()already link to the body. It then passes the same options toheader.apply_async(). Before that call it removes onlychain,chordandtask_id, solinkandlink_errorreach the header group, which always rejects them (group.apply_async).Fix
In
_chord.run(), removelinkandlink_errorfrom the options and add each callback to the body withappend_to_list_option(), the helperSignature.link()uses. The header no longer receives them. Two properties follow:chain(a, chord(h, body.link(x))).apply_async(link=y)runs bothxandyonce. A plaindict.updatewould have droppedx.chord.apply_async(link=cb)also clones the body withlink=cb, so the callback reachesrun()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 thetask_allow_error_cb_on_chord_headerbehavior.Before / after (in-process worker,
memory://broker,cache+memory://backend)chord(...).apply_async(link=cb)TypeErroron the client6,cbcalled oncechord(...).apply_async(link_error=eb)with a failing bodyTypeErroron the clientebcalled oncechain(add, chord(h, body)).apply_async(link=cb)addstored asFAILURE; chord never runs34,cbcalled once.link(own)TypeErrorownandcbeach called oncechain(add, chord(...)).on_error(eb)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 nolink.All 3 new tests fail on
mainwith theTypeErrorabove and pass with this change. The fullt/unitsuite passes except for failures that also occur on unmodifiedmainin my environment (root user, no broker). flake8, isort and codespell are clean on the changed files.Added an entry under Unreleased in
Changelog.rst.