Skip to content

fix(cache): keep chords alive past result_expires - #10830

Merged
auvipy merged 15 commits into
celery:mainfrom
ChristianMurphy:fix/memcached-chord-expires
Oct 14, 2026
Merged

auvipy merged 15 commits into
celery:mainfrom
ChristianMurphy:fix/memcached-chord-expires

Conversation

@ChristianMurphy

Copy link
Copy Markdown
Contributor

Description

Follow-up to #10827 that fixes #4207 for the memcached backend. This builds on #10827

On the cache backend, a chord's group record and its header results only live for result_expires. If the chord runs longer than that, the body never runs. With 5.7.0b1, a chord of three 3-second tasks and result_expires=1 fails every time.

This makes the cache backend use result_chord_expires too:

  • The counter and the group record get it when the chord starts.
  • Each finished header task refreshes the counter, the group record and its own result.
  • A header result never ends up with a shorter TTL than result_expires.

Redis avoids the problem by copying the results into one key. I didn't do that here, because memcached caps an item at 1 MB by default and large chords that work today would break.

If the setting isn't set, the only change is a few extra touch calls. DynamoDB and GCS share this code but don't implement expire, so they aren't affected.

@auvipy
auvipy self-requested a review October 9, 2026 14:15
@auvipy auvipy added this to the 5.7.0 milestone Oct 9, 2026
@codecov

codecov Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.49%. Comparing base (f8d5258) to head (0e9274e).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #10830      +/-   ##
==========================================
+ Coverage   90.47%   90.49%   +0.01%     
==========================================
  Files         153      153              
  Lines       21277    21294      +17     
  Branches     2590     2593       +3     
==========================================
+ Hits        19251    19270      +19     
+ Misses       1710     1709       -1     
+ Partials      316      315       -1     
Flag Coverage Δ
unittests 90.47% <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. To test it end to end without memcached, I used a real in-process worker with the cache backend and a client that expires keys the way memcached does (TTL, 0 = never, touch, incr on a missing key returning None). The chord had three 1.5 s tasks and result_expires=1.

result_chord_expires main this PR
not set body never runs body never runs (unchanged)
60 body runs
0 body runs

So the fix works, but only when the setting is set. Without it nothing changes, which is a reasonable default. The docs for chords and result_expires should point to the new setting, since that is where people hitting #4207 will look. The new cache unit tests fail on main and pass here, and the rest of test_cache.py / test_base.py passes.

1. With result_chord_expires=0, header results never expire

on_chord_part_return() refreshes each finished header result with 0 if not expires or not self.expires else max(...). With result_chord_expires=0 that is touch(key, 0), so in memcached every header result except the last one is kept forever, until it is evicted. After the chord above finished, two of the three header results had no expiry left.

With 60 the header results stay about 60 s after the chord finishes instead of result_expires (1 s here). That part is documented, but it is still longer than needed once the body has started.

The docs describe 0 as "the chord's bookkeeping never expires while the chord is open", which does not cover this. Giving the header results their normal lifetime back when the chord completes fixes both cases:

            finally:
                deps.delete()
                self.delete(key)
            if self._chord_expires is not None:
                # The chord is done: give the header results back their
                # normal lifetime instead of the chord's.
                for dep in deps.results:
                    self.expire(self.get_key_for_task(dep.id), self.expires)

With this, the body still runs for both 60 and 0, all header results end up with result_expires (1 s) after the chord finishes, and the same unit tests pass.

2. Redis: _set_chord_key() skips RedisBackend.set()

The complex-header group record used to go through header_result.save(), so through RedisBackend.set(). Now it calls client.set() directly. That drops the _MAX_STR_VALUE_SIZE check: a header too large for Redis used to raise BackendStoreError('value too large for Redis backend') and now goes straight to Redis. It also drops the publish(), which is harmless because nothing subscribes to group keys. Keeping the size check in _set_chord_key() would keep the old error.

3. Order with #10827

This branch carries the Redis commit from #10827 (2a218d9, "fix(redis): add result_chord_expires for chord keys"). If both stay open, it would help to merge #10827 first and rebase this on top, so the cache part can be reviewed on its own.

Minor

  • The integration tests check what you intend, but they only run in the cache and redis tox environments. It would be worth confirming in CI that test_chord_expires actually runs rather than skips.
  • The memcached 30-day note is important. Maybe also log a warning in the cache backend when result_chord_expires is over 30 days, since memcached then treats it as an absolute timestamp and the keys expire immediately.

@auvipy

auvipy commented Oct 11, 2026

Copy link
Copy Markdown
Member

@MehrazRumman thanks for the initial review, may be it would be better if you could review this first #10827 as this pr is the base. although some review comments are valid for this first pr already.

@ChristianMurphy

Copy link
Copy Markdown
Contributor Author

I've merged the #10827 changes into this branch and fixed the header results. When a chord finishes, every header result gets result_expires back, so with 0 or 60 they no longer outlive the chord. This runs for every chord, with or without the setting, so there's a single code path. The group record also never gets less than result_expires now, the same as on Redis.

The cache backend also logs a warning at startup when result_chord_expires is over 30 days.

About CI: the integration workflow skips the new memcached test, because it has no memcached service. The smoke suite does run memcached, so I added a test there with a chord whose tasks outlive result_expires. It fails on #10827 alone with GroupResult ... no longer exists and passes with this change.

The changes are in 01acc0e and 49dd97e.

@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 fix the merge conflicts

Comment thread celery/backends/redis.py Resolved
@auvipy
auvipy self-requested a review October 13, 2026 17:47
@ChristianMurphy

Copy link
Copy Markdown
Contributor Author

please fix the merge conflicts

Merged main in and resolved the conflicts in 73ab153, now that #10827 has landed. This PR is down to the cache backend changes.

Comment thread celery/backends/redis.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.

please fix the merge conflicts again

@ChristianMurphy

Copy link
Copy Markdown
Contributor Author

Merged main in again and resolved the conflict.

@auvipy
auvipy merged commit 349ee46 into celery:main Oct 14, 2026
485 checks passed
@ChristianMurphy
ChristianMurphy deleted the fix/memcached-chord-expires branch October 14, 2026 16:37
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

3 participants