Skip to content

fix(redis): add result_chord_expires for chord keys - #10827

Merged
auvipy merged 7 commits into
celery:mainfrom
ChristianMurphy:fix/redis-chord-expires
Oct 13, 2026
Merged

auvipy merged 7 commits into
celery:mainfrom
ChristianMurphy:fix/redis-chord-expires

Conversation

@ChristianMurphy

Copy link
Copy Markdown
Contributor

Description

Fixes #4207.

On the Redis backend, a chord's size, results and counter keys expire after result_expires, measured from the chord's start or from the last header task to finish. If a header task takes longer than that, the size key is gone, so the chord's body never runs and nothing is logged.

This adds result_chord_expires for those keys and for the header that a chord with nested groups saves. Unset, it falls back to result_expires, as today. 0 means the keys don't expire while the chord is open.

The results of the header tasks themselves still use result_expires. The saved header is now written like the other chord keys, so with result_backend_always_retry it no longer goes through GroupResult.save()'s retry loop.

Tests cover each write and the fallback. An integration test checks the TTLs against Redis and that the saved header still restores.

@codecov

codecov Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.41%. Comparing base (0c87f96) to head (8322e5a).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #10827   +/-   ##
=======================================
  Coverage   90.40%   90.41%           
=======================================
  Files         153      153           
  Lines       21079    21093   +14     
  Branches     2544     2547    +3     
=======================================
+ Hits        19057    19071   +14     
  Misses       1710     1710           
  Partials      312      312           
Flag Coverage Δ
unittests 90.39% <100.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.

@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. Since there is no Redis server here, I used a real in-process worker with the Redis result backend backed by fakeredis, which honours TTLs. The chord had three 1.5 s header tasks and the worker was solo.

The fix works

Setting main this PR
result_expires=1 only body never runs body never runs (unchanged)
result_expires=1, result_chord_expires=60 body never runs (option unknown) body runs
result_expires=1, result_chord_expires=0 body never runs body runs

The default stays the same, so long chords still need the new setting. The docs for chords and result_expires should point to it, since that is where people hitting #4207 will look. The new unit tests fail on main and pass here, and the rest of test_redis.py and test_base.py passes.

Problem: a GroupResult the user saved can get a shorter TTL

on_chord_part_return() now also runs EXPIRE on the group key (celery-taskset-meta-<gid>) with chord_expires. That key is not only the saved header of a chord with nested groups: it is also where GroupResult.save() stores a header the user saves to restore later, which is a common pattern (res = chord(...).apply_async(); res.parent.save()).

With result_expires=3600 and result_chord_expires=60:

main this PR
TTL of the saved GroupResult after the chord finishes about 3600 s 59 s

So GroupResult.restore() starts returning None after a minute instead of an hour. Never shortening that key below result_expires avoids it:

            expires = self.chord_expires
            if expires:
                pipeline = pipeline \
                    .expire(jkey, expires) \
                    .expire(tkey, expires) \
                    .expire(skey, expires)
                if self.expires:
                    # The group key may also be a GroupResult the user saved
                    # with result_expires: never shorten it below that.
                    pipeline = pipeline.expire(
                        self.get_key_for_group(gid), max(expires, self.expires))

With this, the saved GroupResult keeps about 3600 s, a nested header still gets result_chord_expires when that is longer, and the slow chord above still completes. test_on_chord_part_return_uses_chord_expires would need the group key's expected TTL updated.

Smaller points

  1. _set_chord_key() calls client.set() directly instead of RedisBackend.set(). That skips the _MAX_STR_VALUE_SIZE check, so a nested header too large for Redis no longer raises BackendStoreError('value too large for Redis backend'). It also skips the publish(), which is harmless because nothing subscribes to group keys. Keeping the size check in _set_chord_key() would keep the old error.
  2. prepare_expires(..., type=int) truncates, so result_chord_expires=0.5 becomes 0, which means "never expires". Rounding up, or rejecting values between 0 and 1, would avoid that surprise.
  3. With result_chord_expires=0, the .j, .t and .s keys have no TTL until the chord completes. If a chord is abandoned (for example a lost header message), they stay forever. The docs say "never expires while the chord is open"; adding "including chords that never complete" would make the trade-off clear.
  4. #10830 (cache backend) contains this commit as its first commit. Merging this one first and rebasing #10830 onto it would keep the reviews separate.

@ChristianMurphy

Copy link
Copy Markdown
Contributor Author

The group key refresh could shorten a GroupResult saved with res.parent.save(), as you found. The refresh now never gives that key less than result_expires, and it leaves the key alone when result_expires is 0 or unset. In your example the saved header keeps its hour. _set_chord_key checks _MAX_STR_VALUE_SIZE again too, so an oversized nested header raises BackendStoreError as before.

On the smaller points:

  • 0.5 becoming 0 matches result_expires, since both go through prepare_expires(type=int). I'd rather keep the two consistent here. Rounding up for every expiry setting could be its own PR
  • The docs now say that with 0, a chord that never completes keeps its keys
  • The result_expires docs and the chord notes in the canvas guide now point to the new setting
  • Agreed on the order. Once this merges, I'll rebase fix(cache): keep chords alive past result_expires #10830 on main

The changes are in ed7c68a

@auvipy

auvipy commented Oct 13, 2026

Copy link
Copy Markdown
Member

@jheld do you have any objection merging this pr?

@jheld

jheld commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

@jheld do you have any objection merging this pr?

@auvipy nope it looks good!

@auvipy
auvipy merged commit 77529b9 into celery:main Oct 13, 2026
0 of 2 checks passed
@ChristianMurphy
ChristianMurphy deleted the fix/redis-chord-expires branch October 13, 2026 16:48
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.

Chord body will not be called if result_expires is too low

4 participants