Repository navigation
fix: release paged KV block pins on prompt-cache evict/decline - #170
Merged
Merged
Conversation
Sub-step (a) of #122. Paged prompt-cache entries the store evicts (LRU / TTL / drained-shell / idempotent replacement) or declines (oversized / disabled / prefix-too-short) dropped their pool block pins instead of returning them: a DetachedPagedCacheSet's Drop only warns, and releasing its pins needs a CachePool handle the store does not hold. The store now stashes evicted/declined paged sets on a pending-release queue (held as the Send/Sync DetachedKvSetHolder so Inner stays Send + Sync), which the scheduler drains and returns to the pool via release_detached_paged at the start of try_adopt_cached_prefix and the end of donate_finished_sequence_cache. Also fixes a pre-existing leak in release_detached_paged itself: it released only the detach pin, leaving the origin sequence's allocation (which detach_paged left in place after removing the sequence from active without a release_sequence) dangling at refcount 1. The discard path now releases both refs so an unshared block returns to the pool's free list at refcount 0; the adopt path is unaffected (it transfers the allocation to the new sequence). A parked set is the sole owner of its blocks — sharing only happens through adopt_paged, which consumes the set — so the double release is safe. Adds five tests covering the discard refcount-0 invariant, eviction and oversized-decline queueing plus pool reclaim, dense entries never queueing, and block recyclability after release.
be-free-snowboy
pushed a commit
to freestylejs/snowbrain-mlxcel
that referenced
this pull request
Oct 27, 2026
…p#170) Sub-step (a) of lablup#122. Paged prompt-cache entries the store evicts (LRU / TTL / drained-shell / idempotent replacement) or declines (oversized / disabled / prefix-too-short) dropped their pool block pins instead of returning them: a DetachedPagedCacheSet's Drop only warns, and releasing its pins needs a CachePool handle the store does not hold. The store now stashes evicted/declined paged sets on a pending-release queue (held as the Send/Sync DetachedKvSetHolder so Inner stays Send + Sync), which the scheduler drains and returns to the pool via release_detached_paged at the start of try_adopt_cached_prefix and the end of donate_finished_sequence_cache. Also fixes a pre-existing leak in release_detached_paged itself: it released only the detach pin, leaving the origin sequence's allocation (which detach_paged left in place after removing the sequence from active without a release_sequence) dangling at refcount 1. The discard path now releases both refs so an unshared block returns to the pool's free list at refcount 0; the adopt path is unaffected (it transfers the allocation to the new sequence). A parked set is the sole owner of its blocks — sharing only happens through adopt_paged, which consumes the set — so the double release is safe. Adds five tests covering the discard refcount-0 invariant, eviction and oversized-decline queueing plus pool reclaim, dense entries never queueing, and block recyclability after release.
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
Sub-step (a) of #122 (block-budget admission, eviction, preemption). Part of #122 — the budget/admission/eviction headline is sub-step (b); the
GET /v1/cache/statsblock fields are sub-step (c).Fixes the deferred pin-release leak from #121 sub-step b: paged prompt-cache entries the store evicts (LRU / TTL / drained-shell / idempotent replacement) or declines (oversized / disabled / prefix-too-short) dropped their pool block pins instead of returning them. A
DetachedPagedCacheSet'sDroponly warns — releasing its pins needs aCachePoolhandle, which the store does not hold — so those blocks leaked and never returned to the pool's free list.While wiring this I found and fixed a pre-existing leak in
CachePool::release_detached_pageditself (the function the existing adopt-skip path already used): it released only the detach pin, leaving the origin sequence's allocation dangling at refcount 1. The adopt path is fine (it transfers the allocation to the new sequence); only the discard path leaked.Changes
cache/paged_detach.rs—release_detached_paged(core fix)A detached set owns two pool refs per block: the detach pin (
retained_blocks) and the origin allocation thatdetach_pagedleft in place when it removed the sequence fromactivewithout arelease_sequence. The discard path now releases both, driving an unshared block to refcount 0 so it returns to the free list (verifiedlive=1, detached=2, released → 0). A set parked in the store is the sole owner of its blocks — sharing only happens throughadopt_paged, which consumes the set — so the double release is safe; theretained_blocks.take()guard keeps it idempotent.prompt_cache/store.rs— queue declined/evicted paged pinsThe store can't release pool pins (no
CachePoolhandle), so it stashes evicted/declined paged sets on apending_paged_releasesqueue (held as the Send/SyncDetachedKvSetHoldersoInnerstaysSend + Sync).remove_entry(the chokepoint for LRU/TTL/drained/replacement) and the threeinsertdecline paths stash the set viatake_detached. New publicdrain_pending_paged_releases()/has_pending_paged_releases(). Dense sets free on drop and are never queued.batch/scheduler.rs— drain + releaseNew
drain_store_paged_releases()drains the store's queue and returns each set's pins viaCachePool::release_detached_paged. Called at the start oftry_adopt_cached_prefix(covers prior lookups' TTL/drained sweeps) and the end ofdonate_finished_sequence_cache(covers this insert's eviction/decline), so during serving pins are reclaimed within one store op.Tests
cache::paged_detach::release_detached_paged_returns_blocks_to_free_list(core) — detach → refcount 2 → release → refcount 0, and the freed block is recycled by the next allocation.scheduler_prompt_cache_tests::release_detached_paged_reclaims_blocks_to_pool— the discard refcount-0 invariant viaCachePool.…::evicted_paged_entry_queues_pins_then_pool_reclaims— LRU eviction queues the set; drain + release reclaims its blocks.…::oversized_paged_entry_decline_queues_pins_then_pool_reclaims— oversized decline queues + reclaims.…::dense_eviction_does_not_queue_paged_releases— dense evictions queue nothing.Validation (orchestrator-run, M1 Ultra, metal+accelerate)
Cold
cargo clippy --tests(both crates) clean;cargo fmt --checkclean; corecache::paged_detach(22) + full core regression +server::batch(179) +server::prompt_cache(134) green.