Skip to content

Release per-session session state on forget - #551

Open
HankGrimm wants to merge 1 commit into
stacklok:mainfrom
HankGrimm:fix/release-session-state-on-forget
Open

HankGrimm wants to merge 1 commit into
stacklok:mainfrom
HankGrimm:fix/release-session-state-on-forget

Conversation

@HankGrimm

Copy link
Copy Markdown

Summary

forgetSessionLocked removes a session's registry entry and closes its notify channel, but leaves every per-session reference on the clientSession intact. When the clientSession stays reachable through some other path — ToolHive's list_changed sink captures the ClientSession and lives inside the session's MultiSession backend connectors — those references keep the per-session go-sdk server (and its converted tool schemas) and the tools/resources/prompts overlays alive long after the session is deleted.

This contributes to stacklok/toolhive#6932, where a per-session tool set (~several MB) is retained under initialize→DELETE churn.

What changed

Add clientSession.release, which clears boundServer, goSession, owner, the capability/protocol bookkeeping (initializedOn, negotiated), and the tools/resources/resourceTemplates/prompts/sdkToolNames overlays. forgetSessionLocked now calls release() before closing the notify channel, so forgetting a session eagerly drops the per-session state it owns.

release is idempotent and safe to run concurrently with the setters: after release, SetSessionTools (and friends) still record their overlay but no longer reconcile it onto a live server (boundServer/owner are nil), which is the correct behavior for a session that is already gone.

Type of change

  • Bug fix

Test plan

  • Added TestClientSessionRelease, which populates every cleared field and asserts release nils them all.
  • go test ./mcpcompat/server/ passes.
  • go build ./... and go vet ./mcpcompat/server/ pass.

Note: go test ./... has a pre-existing failure in permissions (TestMountDeclaration_Parse) on Windows — path-separator expectation (/ vs \), unrelated to this change.

🤖 Generated with Claude Code

forgetSessionLocked removes a session's registry entry but leaves its
per-session references intact, so a lingering reference to the clientSession
(for example the list_changed sink a backend connector holds after the
session's MultiSession is closed) keeps the per-session go-sdk server — and
its converted tool schemas — plus the tools/resources/prompts overlays alive.

Add clientSession.release, which clears boundServer, goSession, owner, the
capability/protocol bookkeeping, and the overlay maps, and call it from
forgetSessionLocked before closing the notify channel.

Co-Authored-By: Claude Code <noreply@anthropic.com>
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.

1 participant