Skip to content

fix: skip stale heap entries in LimitedSet to prevent premature eviction of refreshed items - #10507

Closed
waterWang wants to merge 1 commit into
celery:mainfrom
waterWang:fix/limitedset-stale-heap-entries-10506
Closed

waterWang wants to merge 1 commit into
celery:mainfrom
waterWang:fix/limitedset-stale-heap-entries-10506

Conversation

@waterWang

Copy link
Copy Markdown
Contributor

Description

Fixes #10506

When an item is refreshed (added again) to a LimitedSet, the old heap entry remains in _heap while _data[item] is replaced with the newer entry. The stale entry could cause the current entry to be removed prematurely during purge().

Root cause

purge()'s expiry branch called self.pop(), which blindly removes the oldest item from _data — even when the heap entry it popped was stale (the item had been refreshed since that entry was pushed).

Fix

  1. purge() expiry branch: directly inspect self._heap[0] with identity comparison to distinguish stale heap entries from current ones. Only delete from _data when the entry is both expired and current.

  2. pop(): use the same identity check to skip stale heap entries instead of blindly removing the item from _data.

Testing

Added test_refresh_does_not_evict_item_until_new_expiry — a regression test that reproduces the exact scenario from the issue.

All 52 tests in test_collections.py pass.

… eviction of refreshed items

When an item is refreshed (added again) to a LimitedSet, the old heap
entry remains in _heap while _data[item] is replaced with the newer
entry.  The stale entry could cause the current entry to be removed
prematurely during purge().

Fix by having purge()'s expiry branch use identity comparison
(current is entry) to distinguish stale heap entries from current
ones, and by having pop() skip stale entries via the same check.

Includes regression test test_refresh_does_not_evict_item_until_new_expiry.

Closes celery#10506
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.

LimitedSet can prematurely evict refreshed items due to stale heap entries

2 participants