Skip to content

fix(vmcp): reclaim session cache entry on Terminate - #6787

Closed
suantea wants to merge 4 commits into
stacklok:mainfrom
suantea:fix/session-terminate-reclaims-cache
Closed

suantea wants to merge 4 commits into
stacklok:mainfrom
suantea:fix/session-terminate-reclaims-cache

Conversation

@suantea

@suantea suantea commented Oct 10, 2026

Copy link
Copy Markdown

Fixes #6762.

Problem

Manager.Terminate Phase 2 (full MultiSession) deleted the storage record but left the node-local ValidatingCache entry in place. Backend connections stayed open until a subsequent GetMultiSession triggered lazy eviction (checkSession → ErrExpired → onEvict → Close).

Under sustained create/terminate churn the cache never shrank, accumulating ~2.3 MiB per terminated session until the pod was OOM-killed. The default CacheCapacity=1000 is not configurable, so this was unbounded.

Fix

  1. pkg/cache/validating_cache.go: add ValidatingCache.Remove(key) with the same lock discipline as RemoveMatching — remove under mu, run the user onEvict off the lock via drainEvictions.
  2. pkg/vmcp/server/sessionmanager/session_manager.go: call sm.sessions.Remove(sessionID) in Terminate's Phase 2 branch right after the storage delete, so onEvict closes the backend connections immediately instead of on the next request.

The storage delete happens first, so the existing loadSession SET XX guard still rejects in-flight restores; other replicas still self-heal via checkSession on their next Get.

The pre-existing narrow window (an Update that lands between the delete and a concurrent Get miss insertion) is unchanged by this patch.

Tests

  • TestRemove (cache): present → true + one onEvict + entry gone; absent → false + no eviction + cache unchanged; surviving entry still served from cache.
  • Existing session_manager_test.go terminate paths now assert sm.sessions.Len()==0 immediately after Terminate (previously relied on a subsequent Get).
  • go test -race ./pkg/cache/ ./pkg/vmcp/server/sessionmanager/ -count=1 passes.

CleanupAll and CleanupStaleLocks released the flock before removing the
lock path, leaving the same unlock/unlink race that ReleaseTrackedLock
avoids: a waiter could flock the about-to-be-removed inode, then have the
pathname yanked out from under it while another process locks a fresh
replacement inode — two processes holding "the" lock.

Reorder both to remove the path first, so any subsequent open necessarily
gets a new inode and AcquireTrackedLock's identity check can detect the
stale one. CleanupStaleLocks deliberately keeps TryLock (non-blocking, on
an already-stale-by-age file) instead of routing through the retrying
acquire path.

Fixes: stacklok#6713
Add the Pi coding agent (pi.dev) to the supported MCP clients for
thv client register/setup and the desktop app's Manage Clients dialog.

Pi reads servers from a standard mcpServers JSON file at
~/.pi/agent/mcp.json, so it fits the existing client model with no new
config logic. stdio and streamable-http transports are supported; SSE
is intentionally omitted because Pi rejects "type": "sse".

Fixes stacklok#6735
--oidc-insecure-allow-http, --thv-ca-bundle, and --jwks-auth-token-file
were accepted but discarded: createOIDCConfig never set
InsecureAllowHTTP, CACertPath, or AuthTokenFile on the
TokenValidatorConfig. Wire them through from runFlags so the CLI
matches the runner config builder.
MultiSession Terminate deleted the storage record but left the node-local
cache entry in place; backend connections stayed open until a subsequent
Get triggered lazy eviction. Under sustained create/terminate churn the
cache never shrank, accumulating ~2.3MiB per terminated session until the
pod OOM-killed.

Add ValidatingCache.Remove (same lock discipline as RemoveMatching: remove
under mu, run onEvict off the lock) and call it from Terminate's Phase 2
branch right after the storage delete, so onEvict closes the backend
connections immediately instead of on the next request.

fixes stacklok#6762
@hellouz818

Copy link
Copy Markdown
Contributor

@suantea I had already volunteered to implement this fix and requested assignment in #6762 before this PR was opened.

This is a problem we are currently experiencing in production. I was waiting for assignment in accordance with the [contribution guide], and planned to coordinate the scope with the maintainers, implement the fix, and validate it against our actual workload.

Since I had already expressed my intent to work on this, I would have appreciated coordinating before implementation. I also noticed that the current diff includes unrelated lockfile, Pi client, and OIDC flag changes alongside the #6762 fix. These should be separated to keep the PR focused.

@alex-feel, since you suggested coordinating this with #5860, could you help clarify the next step for that coordination? I'd like to align with the maintainers on how to proceed, and I'm still happy to take on the implementation and validation as originally proposed.

@JAORMX

JAORMX commented Oct 12, 2026

Copy link
Copy Markdown
Collaborator

@suantea, we’ve assigned #6762 to @hellouz818 following their earlier request. Coordination details are here: #6762 (comment).

Could you two agree whether to continue together on this PR or carry the focused fix into a replacement? Either way, let’s preserve credit for the existing work and keep the unrelated changes separate.

@hellouz818

Copy link
Copy Markdown
Contributor

@JAORMX, thanks for assigning #6762 to me and clarifying the scope.

@suantea, thanks for taking the time to put together a patch. I had already implemented the fix locally and was waiting for assignment before opening a PR, in accordance with the contribution guide.

I'd like to open a focused PR with my existing implementation and regression tests, and validate the fix against the production workload where we observed this issue. I'll keep the scope limited to explicit termination, as agreed.

Would you both be comfortable with proceeding this way? Thanks for helping coordinate this.

@jhrozek

jhrozek commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Hi @suantea, I'm closing this PR along with the rest of this batch. Nothing personal, it's just not how we're set up to take contributions.

Our contributing guide asks you to comment on an issue and wait for someone on the team to assign it to you before you start. We do that so two people don't end up fixing the same thing, and so we can agree on the approach first. A few of these PRs landed on issues that already had someone assigned, and I can't review 20+ PRs across unrelated parts of the codebase from one person anyway.

If you'd like to contribute, please read CONTRIBUTING.md, pick one issue, and ask to be assigned. A good-first-issue is a fine place to start. Happy to take a proper look at that one.

@jhrozek jhrozek closed this Oct 12, 2026
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.

vMCP: Terminate() retains backend connections in the node-local cache until eviction

4 participants