Repository navigation
Conversation
chord.link_error() handles chords nested in its header as a special case (celery#9580): with the default task_allow_error_cb_on_chord_header=False, it links the errback to the nested chord so that a failure in the inner chord's header reaches it. The loop iterated maybe_list(self.tasks). freeze() replaces the header list with a group, a dict subclass, which maybe_list() returns as a single item, so no nested chord was found. _chain.prepare_steps() freezes each step before linking the chain's errbacks to it, so every chain(..., chord).on_error(eb) or link_error=eb whose chord has a nested chord in its header was affected. When the inner chord's header fails, no errback is called. The chord does not complete either, as designed, so the failure goes unnoticed. Look at the tasks of the group, as stamp() and __length_hint__() do. Also run this special case only when task_allow_error_cb_on_chord_header is False: when it is True, the loop above already links every header task, nested chords included, and the special case linked the nested chord's body a second time, so the errback ran twice for one failure. chord.set_immutable() iterated self.tasks the same way and raised AttributeError ('str' object has no attribute 'set_immutable') after freeze(), because iterating the group yields its dict keys.
Also move the entry next to the related chord countdown/eta entry, so that it does not conflict with other entries added at the end.
# Conflicts: # Changelog.rst
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.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #10784 +/- ##
==========================================
+ Coverage 91.35% 91.38% +0.02%
==========================================
Files 153 153
Lines 21896 21907 +11
Branches 2722 2723 +1
==========================================
+ Hits 20004 20020 +16
+ Misses 1590 1586 -4
+ Partials 302 301 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
|
@ChristianMurphy this looks good to me. what do you think? |
|
the unit test coverage reduced to 60 % only. please also improve the unit test coverage |
|
Following up from #10783, I verified with Redis that apply_async(link_error=...) now calls the errback once when the inner chord’s header fails, and the outer body stays PENDING. |
|
@auvipy I found two regressions in follow-up testing. If the inner chord’s body fails, the errback can run twice. Passing link_error also makes generator-backed headers consume more tasks before publishing the first one. I’ll address both here in a moment. |
Forward header-failure errbacks during dispatch to preserve lazy chord headers.
ChristianMurphy
left a comment
There was a problem hiding this comment.
Needs a change before merge. I ran 0d0070277, with today's two commits, against main (c0b177ede) with a worker on Redis 7, and again on PostgreSQL 16 with RabbitMQ 4.0. The results differ by backend.
An inner chord in the header of an outer chord, the inner header fails, default settings. Errback calls:
| Redis, main | Redis, this PR | PostgreSQL, main | PostgreSQL, this PR | |
|---|---|---|---|---|
outer.on_error(eb) |
1 | 1 | 2 | 2 |
freeze(), then link_error(eb) |
0 | 1 | 1 | 2 |
chain(task, outer).on_error(eb) |
0 | 1 | 1 | 2 |
chain(task, outer).apply_async(link_error=eb) |
0 | 1 | 1 | 2 |
| Three levels of chords, in a chain | 0 | 1 | 1 | 3 |
outer.apply_async(link_error=eb) |
0 | 1 | 1 | 2 |
| The outer body | PENDING, never ends |
the same | FAILURE |
FAILURE |
On a database backend this adds a second errback call for one failure, and today's "avoid duplicate nested chord errbacks" commit does not change that. I got the same numbers at a9f36a5d1 and at 0d0070277. There the outer chord already fails when the inner one does, and its body's errback runs. Linking the errback to the inner chord as well calls it once per level. So "each failure is reported once" holds on Redis only, and it holds there because the outer chord never ends.
I think the cause is one step earlier. On Redis nothing tells the outer chord that the inner chord failed: chord_error_from_stack() stores the failure of the inner body, but the inner body is a header task of the outer chord and its part is never counted. The special case in link_error() makes up for that by linking the errback to the inner chord. With the lines below on main the outer chord ends in FAILURE on Redis as it does on PostgreSQL, and with the special case removed as well the errback runs once in every row above, on both backends. That includes outer.apply_async(link_error=eb), which I raised on #10783, without passing the errback down into the header (_chord_header_errbacks).
| With the lines below, and the special case removed | Redis | PostgreSQL |
|---|---|---|
| Errback calls, each of the six rows above | 1 | 1 |
| The outer body | FAILURE |
FAILURE |
It changes what Redis does: an outer chord that waited forever now fails. I think that is the right outcome, and it needs a maintainer's yes, @auvipy. t/unit/tasks, t/unit/backends and t/unit/app/test_builtins.py give the same results with it as on main.
The lines, in chord_error_from_stack() in celery/backends/base.py
After backend._fail_chain(callback.options.get("chain"), exc):
outer = callback.options.get("chord")
if outer:
# The body is itself a header task of another chord (a chord
# nested in a chord header): count its failure there, so the
# outer chord ends instead of waiting for it forever.
part = _create_fake_task_request(
task_id=callback_id, task_name=callback.task, chord=outer,
group=callback.options.get("group_id"),
group_index=callback.options.get("group_index"),
)
backend.on_chord_part_return(part, states.FAILURE, exc)The set_immutable() fix is right and stands on its own: it raises AttributeError after freeze() on main and works here.
Smaller things:
- With
task_allow_error_cb_on_chord_headeron, this PR removes a duplicate call forouter.on_error(eb)(3 calls to 2 on Redis, 4 to 3 on PostgreSQL). That part is an improvement on both backends. - The new unit tests check which signature holds the errback. A test that runs the chord and counts the calls would have shown the difference between backends.
test_nested_chord_header_link_errorint/integration/test_canvas.py, which today's commit extends, runs on Redis only.
What I did not run
- Any result backend other than Redis and PostgreSQL.
- The integration and smoke suites, and Python versions other than 3.13.
|
@ChristianMurphy On it, just a moment. |
Report a failed inner body as a completed outer header part and let the outer chord run its own errback. Cover the result and errback count on native and polling backends.
|
Thanks for the cross-backend repro, @ChristianMurphy. I pushed a2b2470. A failed inner chord body now counts as a failed part of the outer header. I removed the nested errback forwarding and kept the set_immutable() fix. With default settings, all six cases from your review now reach FAILURE and call the outer errback once on Redis 7 and PostgreSQL 16 with RabbitMQ 4.0. They also passed on Memcached, along with an additional case where the inner chord has a group body. @auvipy, could you confirm that changing the Redis result from PENDING to FAILURE is the behavior you want here? |
ChristianMurphy
left a comment
There was a problem hiding this comment.
Needs one more change before merge, and a merge of main. Re-tested a2b24701f against main (bffee14b2). The six cases from my last review now end FAILURE with one errback call on Redis, PostgreSQL and memcached.
An inner chord whose body is a group with a chain in it loses its errback. The chain has no chord option, only its last task does, so _return_failed_chord_part() returns without counting it.
inner = chord([add.s(1, 1), boom.s()], group(chain(body.s(), add.s(1)), body.s()))
chord([inner, add.s(2, 2)], body.s()).on_error(eb.s()).apply_async()| Redis 7.4, 3 of 3 runs | Errback calls | Outer body |
|---|---|---|
| main | 2 | PENDING |
| This PR | 0 | PENDING |
| This PR, with the change below | 1 | FAILURE |
The change I tried in _return_failed_chord_part()
from celery.canvas import _chain
# A chain in a group body keeps the enclosing chord on its last task.
while isinstance(callback, _chain) and callback.tasks and "chord" not in callback.options:
callback = maybe_signature(callback.tasks[-1], app=backend.app)It goes above options = callback.options. It has no unit test yet.
The branch conflicts with main in celery/backends/gcs.py and t/unit/backends/test_gcs.py, after #11072.
The description also still describes the first approach.
What I ran
- Celery at
a2b24701fand main atbffee14b2, with kombu main (9cd1cef8), Python 3.13, prefork with 2 processes. - The six cases, 3 of 3 runs each: Redis 7.4, PostgreSQL 16 with RabbitMQ 4.3, and memcached 1.6 with a Redis broker. Each ends FAILURE with one errback call and no result left PENDING.
- The chord above on memcached gives the same counts as on Redis. On PostgreSQL it ends FAILURE with one call.
- 31 more nested chords per backend, main against this PR. No other one is worse than main.
- The new unit tests: 12 of 12 pass, and 7 of them fail on main's source.
- Not run: DynamoDB, GCS and MongoDB, pools other than prefork, and the smoke suite.
|
also the merge conflicts |
Fixes #10783
Description
chord.link_error()handles chords nested in its header as a special case (#9580). With the defaulttask_allow_error_cb_on_chord_header=False, it links the errback to the nested chord so that a failure in the inner chord's header reaches it. The special case stops working once the chord is frozen:freeze()replaces the header list with agroup. A group is a dict subclass, somaybe_list(self.tasks)returns it as a single item andisinstance(task, chord)never matches._chain.prepare_steps()freezes each step before linking the chain's errbacks to it, so everychain(...).on_error()/link_error=with such a chord is affected. Callingfreeze()beforelink_error()on a chord directly has the same effect. When the inner header fails, no errback is called, and the chord does not complete either (as designed, seetest_nested_chord_header_link_error), so the failure goes unnoticed.Fix
getattr(self.tasks, 'tasks', self.tasks)), asstamp()and__length_hint__()already do.task_allow_error_cb_on_chord_headerisFalse, which is the configuration Bugfix: Chord with a chord in header doesn't invoke error callback on inner chord header failure (default config) #9580 targeted. When it isTrue, the first loop already links every header task, nested chords included. The special case linked the nested chord's body a second time, so the errback ran twice for one failure. Without this change, the first fix would have spread that duplicate to chains.chord.set_immutable()iteratedself.tasksthe same way and raisedAttributeError: 'str' object has no attribute 'set_immutable'afterfreeze(), because iterating the group yields its dict keys. It now uses the same unwrapping.Before / after: errbacks called when the inner chord's header fails
Output of the reproduction script in #10783 (in-process worker,
memory://broker,cache+memory://backend):task_allow_error_cb_on_chord_headerchain(add, nested chord).on_error(eb)False(default)['ChordError']→['ChordError'][]→['ChordError']True['ChordError', 'ChordError', 'ValueError']→['ChordError', 'ValueError']['ChordError', 'ValueError']→['ChordError', 'ValueError']chord.freeze(); chord.set_immutable(True)AttributeErrorAfter the change each failure is reported exactly once, and frozen and unfrozen chords produce the same errback wiring under both settings.
Tests
test_link_error_on_nested_chord[unfrozen|frozen|chain](default setting): the nested chord's body gets the errback. Thechaincase goes throughprepare_steps(), like a real chain.frozenandchainfail onmain.test_link_error_on_nested_chord_with_header_errbacks[unfrozen|frozen|chain](True): the nested chord's body gets the errback exactly once.unfrozenfails onmain(duplicate).frozenandchainpass onmainand check that behavior there is unchanged.test_set_immutable_after_freeze: fails onmainwithAttributeError.The full
t/unitsuite passes except for failures that also occur on unmodifiedmainin my environment (root user, no broker). flake8, isort and codespell are clean.Added an entry under Unreleased in
Changelog.rst.