Skip to content

LimitedSet can prematurely evict refreshed items due to stale heap entries #10506

Open

Description

@Namraa310806

Checklist

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.

Optional Debugging Information

  • I have tried reproducing the issue on more than one Python version
    and/or implementation.
  • I have tried reproducing the issue on more than one message broker and/or
    result backend.
  • I have tried reproducing the issue on more than one version of the message
    broker and/or result backend.
  • I have tried reproducing the issue on more than one operating system.
  • I have tried reproducing the issue on more than one workers pool.
  • I have tried reproducing the issue with autoscaling, retries,
    ETA/Countdown & rate limits disabled.
  • I have tried reproducing the issue after downgrading
    and/or upgrading Celery and its dependencies.

Related Issues and Possible Duplicates

Related Issues

  • None found.

Possible Duplicates

  • None found.

Environment & Settings

Celery version: main (reproduced from the current main branch)

celery report Output:

PASTE THE OUTPUT OF:
celery -A proj report

Steps to Reproduce

Required Dependencies

  • Minimal Python Version: Python version used for reproduction; exact minimum version not yet determined.
  • Minimal Celery Version: main (exact minimum affected release not yet determined).
  • Minimal Kombu Version: Not relevant to the isolated reproduction.
  • Minimal Broker Version: Not relevant to the isolated reproduction.
  • Minimal Result Backend Version: Not relevant to the isolated reproduction.
  • Minimal OS and/or Kernel Version: Not determined.
  • Minimal Broker Client Version: Not relevant to the isolated reproduction.
  • Minimal Result Backend Client Version: Not relevant to the isolated reproduction.

Python Packages

pip freeze Output:

PASTE THE OUTPUT OF:
pip freeze

Other Dependencies

Details

The reproduction does not require a broker or result backend. It exercises `celery.utils.collections.LimitedSet` directly.

Minimally Reproducible Test Case

The issue can be reproduced directly with LimitedSet by controlling its
clock and refreshing an existing item before its original expiration.

from celery.utils.collections import LimitedSet


class Clock:
    def __init__(self, value=0):
        self.value = value

    def __call__(self):
        return self.value


clock = Clock()
s = LimitedSet(expires=10, clock=clock)

# Add an item at t=0.
s.add("task-id")

# Refresh the same item at t=5.
clock.value = 5
s.add("task-id")

# The current entry should now expire at t=15.
assert "task-id" in s

# The old heap entry expires at t=10.
clock.value = 10
s.purge()

# BUG: the stale heap entry causes the current entry to be removed.
assert "task-id" in s

On the vulnerable implementation, the final assertion fails.

The regression test also reproduces the issue against the original implementation
and passes after the fix.

Expected Behavior

When an item is added to LimitedSet again, its expiration should be refreshed.

If an old heap entry for the same item later expires, that stale entry should not
cause the current, refreshed entry to be removed.

For example, if an item is added at t=0 with expires=10 and refreshed at
t=5, it should remain present until approximately t=15.

Actual Behavior

LimitedSet keeps heap entries lazily when an existing item is refreshed.
The old heap entry remains in _heap, while _data[item] is replaced with the
newer entry.

When purge() encounters the old expired heap entry, pop() removes the item
from _data based only on the item key. It does not verify that the heap entry
being removed is still the current entry stored in _data.

As a result, an old expired heap entry can delete a newer, refreshed entry.

For example:

t=0   add("task-id")
      heap = [(0, "task-id")]

t=5   add("task-id")
      heap = [(0, "task-id"), (5, "task-id")]
      current _data entry = (5, "task-id")

t=10  old entry expires
      purge()/pop() processes (0, "task-id")
      "task-id" is removed from _data

      BUG: the refreshed entry should remain valid until t=15

This is particularly relevant because LimitedSet is used by Celery's worker state for revoked task IDs. Re-adding or synchronizing an existing revoked task ID can refresh its expiration, but a stale heap entry can subsequently cause that ID to be removed before the refreshed expiration time.

The practical consequence is that a revoked task ID can stop being tracked as revoked earlier than its configured expiration time.

I reproduced this behavior against the original implementation, added regression tests that fail on the vulnerable implementation, and verified that the tests pass with the fix.

The existing test suite also passes with the fix.

Activity

  1. Gambit-Checkmate commented on Aug 22, 2026

    @Gambit-Checkmate

    Hi, I’d like to look into the LimitedSet stale-heap-entry behavior. I can write a small regression test that refreshes an item before eviction, then keep the implementation change limited to heap cleanup. Is this still open for a community contribution?

  2. auvipy commented on Aug 22, 2026

    @auvipy
    Member

    no we already got a open pr for this

  3. modified the milestones: 5.7.0, 5.8.0 on Sep 2, 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

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions