Repository navigation
Description
Checklist
- I have verified that the issue exists against the
mainbranch of Celery. - This has already been asked to the [discussions forum](https://github.com/celery/celery/discussions) first.
- I have read the relevant section in the
[contribution guide](https://docs.celeryq.dev/en/main/contributing.html#other-bugs)
on reporting bugs. - I have checked the [issues list](https://github.com/celery/celery/issues?q=is%3Aissue+label%3A%22Issue+Type%3A+Bug+Report%22+-label%3A%22Category%3A+Documentation%22)
for similar or identical bug reports. - I have checked the [pull requests list](https://github.com/celery/celery/pulls?q=is%3Apr+label%3A%22PR+Type%3A+Bugfix%22+-label%3A%22Category%3A+Documentation%22)
for existing proposed fixes. - I have checked the [commit log](https://github.com/celery/celery/commits/main)
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](https://docs.celeryq.dev/projects/pytest-celery/en/latest/userguide/celery-bug-report.html) and added the reproduction script below.
Mandatory Debugging Information
- I have included the output of
celery -A proj reportin 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
mainbranch of Celery. - I have included the contents of
pip freezein 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 sOn 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
Gambit-Checkmate commentedon Aug 22, 2026
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?
auvipy commentedon Aug 22, 2026
no we already got a open pr for this