Repository navigation
Sweep terminated sessions from the node-local cache - #7259
Merged
aponcedeleonch merged 1 commit intoNov 24, 2026
Merged
Conversation
A session terminated on another replica is deleted from shared storage, but its MultiSession stays in the caching replica's node-local ValidatingCache: checkSession only runs on a cache hit, which never comes once the client is done with the session, so the backend connections and per-session state leak until LRU eviction. Add a background sweep to the session Manager that periodically validates cached sessions against storage and evicts the gone/terminated ones, closing their backend connections via the existing onEvict path. The sweep reads via LoadMetadata so it does not refresh live sessions' Redis TTL. Add ValidatingCache.Sweep (the predicate runs outside the cache lock so it may do I/O) and FactoryConfig.SweepInterval (0 uses the 1m default; negative disables). Fixes stacklok#6932 Co-Authored-By: Claude Code <[email protected]>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #7259 +/- ##
==========================================
+ Coverage 81.81% 81.82% +0.01%
==========================================
Files 914 914
Lines 92619 92661 +42
==========================================
+ Hits 75773 75823 +50
+ Misses 16841 16833 -8
Partials 5 5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
aponcedeleonch
approved these changes
Nov 24, 2026
aponcedeleonch
left a comment
Member
There was a problem hiding this comment.
nice, thanks for the contribution!
3 of 10 tasks
aponcedeleonch
added a commit
that referenced
this pull request
Nov 24, 2026
* Stop resync liveness check refreshing session TTL The list_changed/health resync opened with a GetMultiSession liveness check. That goes through the node-local cache: a hit runs checkSession, whose storage.Load refreshes the session TTL (GETEX on Redis, the last-access time in memory), and a miss restores the session and opens backend connections. The resync fires for every registered session on each backend health change and on each backend list_changed, so sessions abandoned without a DELETE were kept alive for as long as backends flapped. The expiry sweep from #7259 then never reclaimed them, which contributed to OOM kills from abandoned sessions. Check liveness with LookupOwner instead. It reads metadata only, never restores, and reports absent or terminated sessions as ErrSessionNotFound, which still prunes the health-resync registration. A storage failure now skips the run without pruning, since it says nothing about whether the session is alive. Co-Authored-By: Claude Opus 5.5 <[email protected]> * Deduplicate concurrent capability cache misses A vMCP embedder was OOMKilled on a 512Mi pod. Part of the cause was bursts of concurrent requests from one caller: every request that missed the capability cache ran its own full backend sweep, so up to 24 sweeps ran at once, each allocating tens of MiB. Expired entries were also only checked lazily on read and never removed, so a key that was never requested again kept a full tool catalogue copy in the LRU. Concurrent misses for the same key now share one sweep through singleflight. Each caller waits on its own context, so a cancelled request returns promptly. If the shared sweep ends because the leader's context was cancelled, waiters with a live context sweep on their own context. That covers a sweep that still succeeds with a partial view, since the default aggregator skips backends it could not reach; such a view is neither cached nor shared, so waiters do not get "unknown tool" for the missing backends. An invalidation generation keeps a sweep that overlaps InvalidateAll from repopulating the cache with a pre-invalidation view, and keeps later callers from joining that sweep, which the list_changed resync depends on. A successful sweep also prunes expired entries. A panic in the wrapped aggregator is recovered and returned as an error, because singleflight's DoChan otherwise re-raises it on a goroutine where it crashes the process. Co-Authored-By: Claude Opus 5.5 <[email protected]> * Tie session sweep to health-resync cleanup Sessions that expire or are terminated on another replica kept their health-resync registration until the next health or list_changed trigger, so cleanup depended on backend activity. The session cache sweep now reports each session it evicts through a new WithSweptSessionHook option, and Serve uses it to drop the registration at the same time. The sweep also ran on context.Background() with no per-read timeout, and cleanup could not stop a sweep in flight. It now runs on a cancellable context from New. Each storage read is bounded by restoreStorageTimeout, and cleanup cancels the sweep and waits for the loop to exit. Also log the stack when the capability cache recovers a panic, since the returned error only carries the panic value. Co-Authored-By: Claude Opus 5.5 <[email protected]> * Test cleanup reporting a sweep that outlives ctx Covers the stopSweep path where the cleanup ctx ends before the sweep loop exits, which Codecov flagged as uncovered. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
This was referenced Nov 24, 2026
Merged
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
A session terminated on another replica is deleted from shared storage, but
its
MultiSessionstays in the caching replica's node-localValidatingCache:checkSessiononly runs on a cache hit, which never comes once the client isdone with the session, so the backend connections and per-session state leak
until LRU eviction.
Managerthat periodically validatescached sessions against storage and evicts the gone/terminated ones, closing
their backend connections via the existing
onEvictpath.LoadMetadata, which does not refresh the Redis TTL, sosweeping a live session does not extend its lifetime.
ValidatingCache.Sweep(predicate runs outside the cache lock so it maydo I/O) and
FactoryConfig.SweepInterval(0uses the 1m default; negativedisables).
Fixes #6932
Type of change
Test plan
task test) —go test ./pkg/cache/ ./pkg/vmcp/server/sessionmanager/(new
TestValidatingCache_Sweep_EvictsMatchingEntries,TestValidatingCache_Sweep_NoMatch_NoEviction, andTestSessionManager_SweepExpiredSessionsall pass).Does this introduce a user-facing change?
Yes — sessions terminated on another replica are now reclaimed from the local
cache within
SweepInterval(default 1 minute) instead of lingering until LRUeviction.
FactoryConfig.SweepIntervalis a new, optional config field.Special notes for reviewers
LoadMetadata(non-TTL-refreshing), not thecache-hit
checkSession(which refreshes TTL viaLoad); sweeping must notkeep idle live sessions alive.
SweepIntervaldefault (1m) and the disable sentinel (negative) are initialchoices; the cadence trade-off (Redis read cost vs. leak bound) is worth a
look — the issue reporter may have a preference.
🤖 Generated with Claude Code