fix: use real LRU eviction in DirCache - #2097
Draft
aryansk wants to merge 1 commit into
Draft
Conversation
The lru_cache-based eviction in DirCache never removed entries from _cache: lru_cache calls its wrapped function only on cache misses, so old keys were silently evicted from the recency index without popping _cached listings. Reads of evicted keys then deleted them lazily, and __iter__ (which filters through __getitem__) destroyed the whole cache. Track recency with an OrderedDict instead, evict the oldest entry on insert beyond max_paths, refresh order on access, and keep __delitem__ and __iter__ from corrupting state. Co-authored-by: CommandCodeBot <noreply@commandcode.ai>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2095
Problem
DirCacheuses afunctools.lru_cacheas an eviction helper, butlru_cachecalls its wrapped function only on cache misses — never when an old key is evicted from its recency index. As a result:max_pathsnever removes old entries from_cache(unbounded growth)._cachelazily and raisesKeyError.__iter__filters through__getitem__, so iterating destroys the entire cache and returns[].Change
Track recency with an
OrderedDictinstead:_cacheand_times) when overmax_paths;__delitem__andclear()keep the recency index consistent.Testing
Added
fsspec/tests/test_dircache.pycovering eviction, iteration stability, recency refresh, delete, and expiry. With the fix:160 passedin the cache test modules; the remaining failures/errors are pre-existing missing-optional-dependency (aiohttp) import issues.Notes
max_pathsis currently undocumented in the class docstring (only in the__init__docstring); I left that unchanged to keep the diff focused.