Skip to content

fix: use real LRU eviction in DirCache - #2097

Draft
aryansk wants to merge 1 commit into
fsspec:masterfrom
aryansk:fix/dircache-lru-eviction
Draft

fix: use real LRU eviction in DirCache#2097
aryansk wants to merge 1 commit into
fsspec:masterfrom
aryansk:fix/dircache-lru-eviction

Conversation

@aryansk

@aryansk aryansk commented Aug 11, 2026

Copy link
Copy Markdown

Fixes #2095

Problem

DirCache uses a functools.lru_cache as an eviction helper, but lru_cache calls its wrapped function only on cache misses — never when an old key is evicted from its recency index. As a result:

  1. Adding entries beyond max_paths never removes old entries from _cache (unbounded growth).
  2. Reading an evicted key pops it from _cache lazily and raises KeyError.
  3. __iter__ filters through __getitem__, so iterating destroys the entire cache and returns [].

Change

Track recency with an OrderedDict instead:

  • on insert, evict the least-recently-used entry (from both _cache and _times) when over max_paths;
  • on access, refresh the recency order without popping the listing;
  • __delitem__ and clear() keep the recency index consistent.

Testing

Added fsspec/tests/test_dircache.py covering eviction, iteration stability, recency refresh, delete, and expiry. With the fix: 160 passed in the cache test modules; the remaining failures/errors are pre-existing missing-optional-dependency (aiohttp) import issues.

Notes

  • max_paths is currently undocumented in the class docstring (only in the __init__ docstring); I left that unchanged to keep the diff focused.

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>
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.

[Bug] DirCache LRU implementation fails to evict items on write and wipes cache during iteration

1 participant