Repository navigation
fix(files): reset cache eviction order on clear - #9518
Vidit Ostwal (Vidit-Ostwal) merged 1 commit into
Conversation
Strix Security ReviewWarning This pull request has 23 commits after the last Strix review ( ✅ No open security findings. PR summary: Fixes `UploadCache.clear()` so it also resets the cache's access-order list, keeping eviction bookkeeping consistent with a freshly built cache so a refilled cache cannot grow past `max_entries`; adds a regression test for that scenario. Review summaryThis PR is a one-line bookkeeping fix in Ruled out (3)
Review 1 on this PR · Reviewed by Strix |
Muhammad Abdullah Butt (1AbdullahButt)
left a comment
There was a problem hiding this comment.
Correct. Before this, aclear() left the old keys in _key_access_order, so the next _evict_oldest picked those stale keys first. Deleting them was a no-op and len(cache) didn't go down, so the cache grew past max_entries. The new test walks through exactly that.
I checked the other removal paths: aremove, aremove_by_file_id and aclear_expired all go through _untrack_key, and _evict_oldest removes from the list itself, so aclear was the only one that skipped the list.
Small test nit: assert len(cache) <= 2 would also pass if eviction removed too much. Checking that new-0 is gone and new-1/new-2 are still there would also pin down which entry gets evicted.
Reviewed with help from Claude Code.
UploadCache.clear() cleared the backing cache and the per-provider key sets but left the shared access-order list populated. A later set then spent its eviction step deleting stale keys that were already gone, so the cache could grow past max_entries after a clear. Clear the access-order list with the other bookkeeping and cover reuse after clear with a regression test.
01f7d39 to
6f9680e
Compare
Vidit Ostwal (Vidit-Ostwal)
left a comment
There was a problem hiding this comment.
Thanks, this is a clean fix. aclear() was the only removal path that skipped _key_access_order, and upload_cache.py:378 now resets it, so a cleared cache matches a fresh one.
Verified: the issue's repro fails on base (len 3 with max_entries=2) and passes on this branch, along with your new test and the existing clear/eviction tests.
Optional nit: asserting that new-0 is evicted and new-1/new-2 remain would make the test stricter than len(cache) <= 2.
Fixes #9513
Summary
UploadCache.clear()cleared the backing cache and the per-provider key sets, but left_key_access_orderholding the keys that had just been removed. Because the cache's size is derived from_provider_keys, a refilled cache could reachmax_entriesand then have its eviction step delete stale keys that no longer existed, freeing no room and letting the new entry push the cache past the limit.This change clears the access-order list together with the other bookkeeping, so the eviction list is as empty after
clear()as it is in a freshly constructed cache.Verification
assert 3 <= 2after clearing a two-entry cache and inserting three entries) and passes with this change.lib/crewai-files/tests/test_upload_cache.pycould not be run to completion on this Windows machine: an existing test constructs anImageFile, and libmagic crashes the interpreter here before that test finishes. The failure is unrelated to this change and occurs with the same module on main.The test uses
set_by_hash()and the in-memory backend only; no provider or network access is involved.AI assistance
I used an AI coding agent to investigate, implement, and test this change, and reviewed its results. Per CONTRIBUTING.md, please apply the
llm-generatedlabel; external contributors cannot add it with my current permissions.