Skip to content

Send memcached an expiry over 30 days as an absolute time - #11120

Open
MehrazRumman wants to merge 5 commits into
celery:mainfrom
MehrazRumman:fix/memcached-expires-over-30-days
Open

MehrazRumman wants to merge 5 commits into
celery:mainfrom
MehrazRumman:fix/memcached-expires-over-30-days

Conversation

@MehrazRumman

@MehrazRumman MehrazRumman commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Note: Before submitting this pull request, please review our contributing
guidelines
& [Anti AI spamming Guidelines] (#10932). Before opening an AI based PR make sure you asked
in the discussion forum or in the related open issues first.

Description

Fixes #11119

Memcached reads an expiry over 30 days as an absolute Unix time. The cache backend passed result_expires and result_chord_expires as seconds, so with result_expires=timedelta(days=31) every result expired as soon as it was stored, and AsyncResult.get() never returned. Nothing was logged.

  • _memcached_expires() sends an expiry over 30 days as the Unix time it ends, int(time.time()) + seconds. It's used in CacheBackend.set(), expire() (touch) and for the chord counter in _apply_chord_incr(). Shorter expiries, and 0 for no expiry, are sent as before.
  • The startup warning from fix(cache): keep chords alive past result_expires #10830 about result_chord_expires over 30 days, and the docs note saying to keep it at 30 days or less, are removed, since such keys now last as long as configured. The logger that only that warning used goes with it.

Only celery/backends/cache.py changes, and the in-memory DummyClient ignores expiries as before.

#11072, which is open, caps its new chord keys at 30 days on memcached with min(..., MAX_RELATIVE_EXPIRES), and its docs say "at most 30 days". The two PRs work together as they are. Once this one is in, that cap could go, with _memcached_expires() used instead.

Tests

Against memcached 1.6 in Docker, with pymemcache 4.0.0 and Python 3.14. The task test uses the memory broker and an in-process solo worker. The TTL is what mg <key> t reports right after the result is stored:

result_expires main: task get() this PR: task get() this PR: memcached TTL
1 day 4 4 86,400 s
30 days 4 4 2,592,000 s
31 days times out, PENDING 4 2,678,401 s
400 days not run not run 34,560,001 s

New unit tests in t/unit/backends/test_cache.py check the expiry that set(), expire() and the chord counter send. The three cases over 30 days fail on main (checked with only import time added to main's cache.py, so the patch target exists) and pass here. Each of these changes makes one of them fail: > changed to >=, and the old expiry restored in set(), expire() or the chord counter.

pytest t/unit/backends/test_cache.py t/unit/backends/test_base.py: all pass, apart from test_store_result_parent_id[msgpack], which fails the same way on main because msgpack isn't installed here. pre-commit passes on the changed files.

Not run: the integration and smoke suites. The smoke suite's memcached tests use 1 day or less, which this doesn't change.

Memcached reads an expiry over 30 days as an absolute Unix time. The
cache backend passed result_expires and result_chord_expires to it as
seconds, so with result_expires=timedelta(days=31) every result expired
as soon as it was stored, and AsyncResult.get() never returned.

Send an expiry over 30 days as the Unix time it ends, in set(), in
expire() and for the chord counter. Shorter expiries and 0 are sent as
before.
The cache backend warned at startup that memcached would expire chord
keys at once when result_chord_expires was over 30 days, and the docs
said to keep it at 30 days or less. Since the previous commit sends such
an expiry as an absolute time, the keys live as long as configured, so
remove the warning and the note.
@codecov

codecov Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.26%. Comparing base (971b0ef) to head (d05d4b4).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #11120   +/-   ##
=======================================
  Coverage   91.26%   91.26%           
=======================================
  Files         153      153           
  Lines       21788    21790    +2     
  Branches     2702     2703    +1     
=======================================
+ Hits        19885    19887    +2     
  Misses       1603     1603           
  Partials      300      300           
Flag Coverage Δ
unittests 91.24% <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.

@auvipy
auvipy self-requested a review November 19, 2026 03:38
@auvipy auvipy added this to the 5.7.x milestone Nov 19, 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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cache backend: result_expires over 30 days makes memcached expire every result at once

2 participants