Repository navigation
Conversation
LimitedSet documents maxlen as enforced at all times, but assigning a smaller value kept the excess items in place until enough new items were added to trigger eviction one by one. Make maxlen a property that purges down to the new limit on assignment and keeps the maxlen >= minlen invariant for direct assignments too.
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Address the unresolved purge behavior and Windows test-skip review comments.
Review effort: Lite
Findings: 1
Open (1)
What changed in this PR
Updates LimitedSet.maxlen to validate changes and immediately enforce reduced limits.
Changes:
- Adds validated
maxlenproperty handling. - Adds regression tests for shrinking, growth, validation, and pickling.
| File | Summary |
|---|---|
t/unit/utils/test_collections.py |
Adds coverage for updated LimitedSet behavior. |
celery/utils/collections.py |
Implements validated, immediate maxlen enforcement. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| assert cm['bar'] == 3 | ||
|
|
||
|
|
||
| class test_LimitedSet_maxlen_shrink: |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #10796 +/- ##
==========================================
+ Coverage 89.98% 90.23% +0.24%
==========================================
Files 153 153
Lines 20937 21069 +132
Branches 2516 2544 +28
==========================================
+ Hits 18841 19011 +170
+ Misses 1796 1751 -45
- Partials 300 307 +7
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
MehrazRumman
left a comment
There was a problem hiding this comment.
Thanks for the PR. I checked it out and ran it against main.
What works
| Case | main | this PR |
|---|---|---|
5 items, then s.maxlen = 2 |
len(s) stays 5 until the next add() |
len(s) == 2 immediately, oldest items evicted |
s.maxlen = 1 with minlen=2 |
accepted | ValueError |
pickle, copy, deepcopy, repr |
work | work, unchanged |
The unit tests for LimitedSet and for the worker state that uses it (revoked, successful_requests) pass unchanged.
Scope of the problem
- Nothing in Celery assigns
maxlenafter construction;celery/worker/state.pybuilds both sets once. This is API hardening, which is fine, but the description should not read as a live bug. - On
mainthe excess is removed at the very nextadd(), becauseadd()callspurge()once the set is atmaxlen. The test comment says the items stay "until several more items were added"; it is one add.
Suggestions
- The setter calls
purge()on every assignment, not only when shrinking.purge()also runs time-based expiry, so growing the limit removes expired items as a side effect: withexpires=10and 4 expired items,s.maxlen = 50leaves the set empty (4 items onmain). The comment says "shrinking maxlen must evict", so I would purge only when the set is over the new limit:
if value and len(self._data) > value:
self.purge()-
getattr(self, 'minlen', 0)andgetattr(self, '_data', None)are only needed because__init__goes through the setter before the other attributes exist. Settingself._maxlendirectly in__init__(it is validated a few lines later anyway) removes both guards and keeps the setter simple. -
Behaviour changes worth a line in the description:
s.maxlen = 0(no limit) on a set withminlen > 0now raises. The constructor already rejects that combination, so this is consistent, but it was accepted on assignment before.s.maxlen = Noneis now stored as0.minlenis still not validated on assignment:s.minlen = 9withmaxlen=5is accepted. Fine to leave out of scope, but then only one of the two is guarded.
-
Tests:
- Please move the new tests into the existing
test_LimitedSetclass instead of a new class aftertest_ChainMap. That class carries@t.skip.if_win32, and keeping them together avoids deciding the Windows question separately. import pickleinsidetest_pickle_roundtrip_uses_maxlenis redundant; the module already imports it.- A test for growing
maxlenon a set with expired items would pin down whichever behaviour you choose in point 1.
- Please move the new tests into the existing
None of these is a blocker. With points 1 and 4 addressed this looks good to me.
- purge() on assignment only when the set is over the new limit: growing maxlen must not purge, since purge() also drops time-expired items (with expires set, growing used to empty the set as a side effect). - __init__ sets _maxlen directly, which removes both getattr guards from the setter (it no longer runs during construction). - the new tests move into test_LimitedSet (carries @t.skip.if_win32), the redundant local pickle import is gone, and a new test pins that growing maxlen keeps expired items in place. - the shrink test comment now says 'until the next add()', matching the actual main-branch behaviour. Signed-off-by: simpleqt <[email protected]>

Summary
LimitedSetstoresmaxlenas a plain attribute. The class docstring promises "maxlenis enforced at all times", but assigning a smallermaxlenafter construction leaves the set over the limit until the nextadd()(eachadd()triggers at most one eviction viapurge()). Nothing in Celery assignsmaxlenafter construction today (celery/worker/state.pybuilds both sets once), so this is API hardening: it makes the property setter uphold the documented invariant, with tests to pin it.Fix
maxlenbecomes a property whose setter validates againstminlenand evicts down to the new limit immediately — but only when the set is actually over the new limit, becausepurge()also drops time-expired items and growing the limit must not have that side effect:__init__sets_maxlendirectly, so the setter no longer runs during construction and needs no attribute guards.Behaviour changes worth noting
s.maxlen = 0(no limit) on a set withminlen > 0now raisesValueError— the constructor already rejects that combination, so assignment is now consistent with it.s.maxlen = Noneis stored as0(same as in the constructor).minlenremains unvalidated on assignment (out of scope; onlymaxlenis guarded, consistent with the constructor's validation order).Test plan
t/unit/utils/test_collections.py, inside the existingtest_LimitedSetclass (carries@t.skip.if_win32):test_shrinking_maxlen_evicts_oldest_immediately: shrink evicts the oldest items at once (onmainthe excess would survive until the nextadd()).test_growing_maxlen_is_untouched: growing the limit neither evicts nor reorders.test_growing_maxlen_keeps_expired_items: with expired items present, growingmaxlenmust not purge them away (pins the conditional-purge behaviour).test_shrink_below_minlen_raises: assignment belowminlenraises.test_pickle_roundtrip_uses_maxlen: pickling preservesmaxlen.All
LimitedSettests (94) and the worker state tests that use it (revoked,successful_requests, 30) pass; flake8 (repo config) clean.