Skip to content

fix(canvas): stamp the chord body without a visitor - #10698

Open
simpleqt wants to merge 2 commits into
celery:mainfrom
simpleqt:chord-stamp-body
Open

simpleqt wants to merge 2 commits into
celery:mainfrom
simpleqt:chord-stamp-body

Conversation

@simpleqt

Copy link
Copy Markdown

Problem

_Chord.stamp() requires a visitor for the body to receive any stamps:

c = chord(group(add.s(2, 2), add.s(4, 4)), body=mul.s(4))
c.stamp(custom="x")
# every header task: options["custom"] == "x"
# c.body.options: {}                 <-- stamps silently dropped

Stamping without a visitor is a supported public mode — Signature.stamp documents plain custom-header stamping and falls back to set(**headers) — and inside _Chord.stamp() the header tasks are stamped unconditionally. Only the final body block was additionally guarded with visitor is not None, producing a header/body asymmetry: custom headers (routing keys, trace metadata, monitoring tags) reach the header tasks but never the callback.

With a visitor the body is stamped today, so the guard only breaks the non-visitor flow.

Fix

Stamp the body whenever it exists; consult visitor.on_chord_body() only when a visitor was passed:

-        if visitor is not None and self.body is not None:
-            visitor_headers = visitor.on_chord_body(self, **headers) or {}
+        if self.body is not None:
+            visitor_headers = None
+            if visitor is not None:
+                visitor_headers = visitor.on_chord_body(self, **headers) or {}
             headers = self._stamp_headers(visitor_headers, append_stamps, **headers)
             self.body.stamp(visitor, append_stamps, **headers)

Tests

  • test_stamp_without_visitor_stamps_body — plain stamp(custom=...) must land on the header tasks and the body (red before the fix: body options empty).
  • test_stamp_with_visitor_stamps_body — guards the visitor flow (green before and after).

Red/green verified: with the fix reverted the non-visitor test fails and the visitor test passes; with the fix all 258 tests in t/unit/tasks/test_canvas.py plus test_stamping.py + test_chord.py (461 tests, 27.6k subtests) pass. pre-commit run --files (pinned hooks incl. flake8/isort/mypy) passes on both changed files.

Copilot AI lite review requested due to automatic review settings September 23, 2026 15:53

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@codecov

codecov Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 75.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 89.85%. Comparing base (f3f6e79) to head (0e923fb).
⚠️ Report is 95 commits behind head on main.

Files with missing lines Patch % Lines
celery/canvas.py 75.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #10698      +/-   ##
==========================================
- Coverage   89.85%   89.85%   -0.01%     
==========================================
  Files         153      153              
  Lines       20707    20709       +2     
  Branches     2473     2474       +1     
==========================================
+ Hits        18607    18608       +1     
  Misses       1801     1801              
- Partials      299      300       +1     
Flag Coverage Δ
unittests 89.83% <75.00%> (-0.01%) ⬇️

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.

@auvipy auvipy added this to the 5.7.0 milestone Sep 23, 2026
@auvipy
auvipy requested review from Nusnus and auvipy September 23, 2026 17:19

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

please also add smoke test for this.... and if possible integration test as well. @MehrazRumman can you also try it?

@simpleqt

This comment was marked as spam.

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

Thanks @simpleqt, and thanks for adding the smoke and integration tests. The canvas fix is correct. The main issue is the branch itself.

Blocking: the branch includes the unrelated forget() commit

This PR also contains 4eb1c57 ("fix(backends): keep forget() idempotent on elasticsearch and couchdb"), which is the whole of #10697. It touches celery/backends/couchdb.py, celery/backends/elasticsearch.py and their tests. It looks like this branch was created from the #10697 branch instead of from main. Could you rebase onto main so the PR only contains 9cd0ed1? For example: git rebase --onto origin/main 4eb1c5771 chord-stamp-body. Otherwise, merging this would also merge #10697 without its own review, and it would conflict or duplicate whichever of the two lands second.

The canvas fix

The change makes _chord.stamp() follow the same pattern as Signature.stamp(), _chain.stamp() and group.stamp(): always call _stamp_headers(), and only call the visitor hook when a visitor is given. Before this, a chord was the only canvas type whose stamping skipped part of the canvas when visitor=None.

I compared main and this branch using chord(group(add.s(1, 1), add.s(2, 2)), xsum.s()):

Case main body custom this PR body custom
c.stamp(custom='a') None 'a' (same as header)
stamp 'a', then stamp(append_stamps=True, custom='b') None ['b', 'a'] (same as header)
stamp 'a', then stamp(custom='b') None 'b' (same as header)
chord inside group(...), group stamped None 'g'
chord inside chain(...), chain stamped None 'ch'

In every case the body now ends up with exactly what the header tasks get, including stamped_headers and the append_stamps behaviour, which is what I'd expect.

Worth noting in the changelog

As the last two rows show, the change isn't limited to calling chord.stamp() directly. Any chord nested in a group or chain stamped without a visitor now gets its body stamped as well. I think that's correct and consistent, but code that currently relies on the callback not carrying those stamps (for example, routing or monitoring that treats stamped messages differently) will behave differently after upgrading. A one-line changelog entry would help.

Tests

  • test_stamp_without_visitor_stamps_body fails on main and passes here. That's the key regression test.
  • test_stamp_with_visitor_stamps_body already passes on main, since the visitor path always stamped the body. That's fine as a guard, but maybe say so in a docstring so nobody expects it to cover the fix.
  • test_stamped_chord_applies_end_to_end_without_visitor only checks that apply() still returns 12. It doesn't check that the body received the stamp at execution time. Using a bound body task that records self.request.stamps would turn it into an actual end-to-end check of the fix. Otherwise the integration test is the only runtime check.
  • It would be good to add unit tests for the two cases above that aren't covered yet: append_stamps=True (body matches header) and a chord nested in a stamped group/chain (body gets stamped).
  • Integration test: the @task_received.connect handler follows the pattern the other stamping tests in this class use, so that's fine as is.

Summary

The fix is correct and I'd approve it once the branch is rebased to remove the #10697 commit. Additional unit tests for append_stamps and nested chords, would be nice to have.

@auvipy

auvipy commented Sep 27, 2026

Copy link
Copy Markdown
Member

now this has merged conflicts so the unrelated commit must be discarded from this branch, along with additional tests and docs updates

_Chord.stamp() guarded the body stamping behind visitor is not None,
so a plain chord(header, body).stamp(custom=x) stamped every header
task but silently dropped the stamps from the body. Stamping without a
visitor is a documented, supported mode (Signature.stamp falls back to
set(**headers)), and the header/body asymmetry meant callers could not
rely on custom headers reaching the callback.

Stamp the body whenever it exists; the visitor's on_chord_body hook is
only consulted when a visitor was passed, preserving the visitor flow
covered by the existing stamping tests.

Signed-off-by: simpleqt <[email protected]>
@simpleqt

This comment was marked as spam.

@auvipy auvipy modified the milestones: 5.7.0, 5.7.x Sep 30, 2026

This branch has not been deployed

No deployments
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