Skip to content

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

Open

Description

@MehrazRumman

Checklist

  • I have verified that the issue exists against the main branch of Celery.
  • This has already been asked to the discussions forum first.
  • I have read the relevant section in the
    contribution guide
    on reporting bugs.
  • I have checked the issues list
    for similar or identical bug reports.
  • I have checked the pull requests list
    for existing proposed fixes.
  • I have checked the commit log
    to find out if the bug was already fixed in the main branch.
  • I have included all related issues and possible duplicate issues
    in this issue (If there are none, check this box anyway).
  • I have tried to reproduce the issue with pytest-celery and added the reproduction script below.

Mandatory Debugging Information

  • I have included the output of celery -A proj report in the issue.
    (if you are not able to do this, then at least specify the Celery
    version affected).
  • I have verified that the issue exists against the main branch of Celery.
  • I have included the contents of pip freeze in the issue.
  • I have included all the versions of all the external dependencies required
    to reproduce this bug.

Related Issues and Possible Duplicates

Related Issues

Possible Duplicates

  • None

Environment & Settings

Celery version: main at 9c2f238 (5.7.0)

Steps to Reproduce

Required Dependencies

  • Minimal Python Version: N/A or Unknown
  • Minimal Celery Version: N/A or Unknown
  • Minimal Kombu Version: N/A or Unknown
  • Minimal Broker Version: N/A or Unknown
  • Minimal Result Backend Version: memcached 1.6
  • Minimal OS and/or Kernel Version: N/A or Unknown
  • Minimal Broker Client Version: N/A or Unknown
  • Minimal Result Backend Client Version: pymemcache 4.0.0

I ran it with Python 3.14 and the memory broker.

Minimally Reproducible Test Case

$ docker run -d -p 11211:11211 memcached:1.6
from datetime import timedelta

from celery import Celery
from celery.contrib.testing.worker import start_worker
from celery.exceptions import TimeoutError

for days in (30, 31):
    app = Celery('repro', broker='memory://',
                 backend='cache+memcached://127.0.0.1:11211/',
                 set_as_current=False)
    app.conf.result_expires = timedelta(days=days)

    @app.task(shared=False)
    def add(x, y):
        return x + y

    with start_worker(app, pool='solo', perform_ping_check=False):
        result = add.delay(2, 2)
        try:
            print(days, 'days:', result.get(timeout=5))
        except TimeoutError:
            print(days, 'days: get() timed out, state', result.state)

Expected Behavior

result.get() returns 4 for both, and the result is kept for 31 days.

Actual Behavior

30 days: 4
31 days: get() timed out, state PENDING

The task ran and stored SUCCESS, but the result was gone at once. Nothing is logged.

The memcached protocol reads an expiry over 30 days (2,592,000 seconds) as an absolute Unix time, so 31 days in seconds (2,678,400) is a moment in January 1970, and memcached expires the key straight away. CacheBackend.set() passes result_expires as seconds (self.client.set(key, value, self.expires or 0)), and so does expire() (touch) and the chord counter's set() with result_chord_expires.

The result_expires docs don't mention the limit. The note in the result_chord_expires docs and the startup warning from #10830 only cover result_chord_expires, and the warning only fires when that setting is set, not when it falls back to a long result_expires.

Sending an expiry over 30 days as the Unix time it ends, int(time.time()) + seconds, fixes it. With that change, memcached reports a TTL of 31 days for the 31-day result, and of 400 days for a 400-day one.

Activity

  1. added this to the 5.7.x milestone on Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions