fix(lru_cache): gc() must walk full list, not stop at tail#2
Merged
jjacke13 merged 1 commit intojjacke13:mainfrom Apr 26, 2026
Merged
Conversation
`LruCache::gc` walked from the list tail (oldest by insertion order)
and broke on the first entry whose `created_at` was within the TTL.
That assumed tail order tracked `created_at` order — but it doesn't,
because `get()` promotes accessed entries to the front via splice
WITHOUT touching `created_at`.
Repro:
put("old", t=1000) list: [old]
put("fresh", t=5000) list: [fresh, old]
get("old") list: [old, fresh] // fresh is now at tail
gc(now=4000, ttl=2000)
-> walks back, sees fresh (created_at=5000), breaks
-> "old" (age 3000, expired) is leaked forever
Fix: walk every entry. The list is small enough (mutables /
immutables / connection caches) that this is fine, and it removes
the order assumption that `get()` already broke.
This LruCache backs the DHT's user-facing `mutables_` /
`immutables_` storage caches in `rpc_handlers.cpp`, so a missed
eviction can return values past their declared TTL.
Adds a regression test (`LruCache.GcWalksFullListAfterGetPromotion`)
that fails on the old `gc` and passes on the new one. All 9 LruCache
tests pass.
Made-with: Cursor
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.
Summary
LruCache::gc(ininclude/hyperdht/lru_cache.hpp) walks the list from the tail ("oldest") and breaks on the first entry whose `created_at` is within the TTL. That works only if tail order tracks `created_at` order — but it doesn't, because `get()` promotes accessed entries to the front via splice without touching `created_at`.After any `get()`, list order = LRU access order, while expiry is still keyed on `created_at`. The two diverge, and the early `break` then leaks expired entries indefinitely.
Repro
This
LruCachebacks the DHT's user-facing `mutables_` / `immutables_` storage caches in `rpc_handlers.cpp`, so a missed eviction can return key/value records past their declared TTL.Fix
Walk every entry. The caches in question are bounded (default 1024 entries) and `gc` is invoked on a low-frequency timer, so the constant-factor cost is negligible — and it removes the chronological-order assumption that `get()` already broke.
I considered two alternatives:
Test plan
Made with Cursor