Skip to content

fix: release paged KV block pins on prompt-cache evict/decline - #170

Merged
inureyes merged 1 commit into
mainfrom
feat/122-paged-pin-release
Jun 7, 2026
Merged

inureyes merged 1 commit into
mainfrom
feat/122-paged-pin-release

Conversation

@inureyes

@inureyes inureyes commented Jun 7, 2026

Copy link
Copy Markdown
Member

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/stats block 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's Drop only warns — releasing its pins needs a CachePool handle, 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_paged itself (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 that detach_paged left in place when it removed the sequence from active without a release_sequence. The discard path now releases both, driving an unshared block to refcount 0 so it returns to the free list (verified live=1, detached=2, released → 0). A set parked in the store is the sole owner of its blocks — sharing only happens through adopt_paged, which consumes the set — so the double release is safe; the retained_blocks.take() guard keeps it idempotent.

prompt_cache/store.rs — queue declined/evicted paged pins

The store can't release pool pins (no CachePool handle), so it stashes evicted/declined paged sets on a pending_paged_releases queue (held as the Send/Sync DetachedKvSetHolder so Inner stays Send + Sync). remove_entry (the chokepoint for LRU/TTL/drained/replacement) and the three insert decline paths stash the set via take_detached. New public drain_pending_paged_releases() / has_pending_paged_releases(). Dense sets free on drop and are never queued.

batch/scheduler.rs — drain + release

New drain_store_paged_releases() drains the store's queue and returns each set's pins via CachePool::release_detached_paged. Called at the start of try_adopt_cached_prefix (covers prior lookups' TTL/drained sweeps) and the end of donate_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 via CachePool.
  • …::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 --check clean; core cache::paged_detach (22) + full core regression + server::batch (179) + server::prompt_cache (134) green.

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.
@inureyes inureyes added the type:bug Bug fixes, error corrections, or issue resolutions label Jun 7, 2026
@inureyes
inureyes merged commit efe33c1 into main Jun 7, 2026
4 checks passed
@inureyes
inureyes deleted the feat/122-paged-pin-release branch June 7, 2026 10:16
@inureyes inureyes added this to the 0.2 milestone Jun 21, 2026
@inureyes inureyes self-assigned this Aug 31, 2026
@inureyes inureyes added the status:done Completed label Sep 6, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant