fix(serve-mcp): session TTL eviction, SSE emitter leak, subscription race, tool name sanitization#66
Merged
Merged
Conversation
…race, tool name sanitization
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.
McpServer— sessions idle for longer thansessionIdleTimeout(default 1 hour, configurable viaBuilder.sessionIdleTimeout(Duration)) are evicted automatically, breaking the permanent-503 deadlock that occurred oncemaxSessionswas hit after the cap-without-TTL fix in fix(serve-mcp): harden MCP surface against DoS, info-leak, and missing DELETE #53. Every JSON-RPC dispatch and SSE connection establishment toucheslastAccessed; the reaper sweeps atmin(idleTimeout / 2, 5 min)using a daemon virtual thread.SessionState.emitteris now anAtomicReference<SseEmitter>; a second GET on the same session atomically swaps in the new emitter viagetAndSetand callsclose()on the old one (unblocking itsawaitClose()virtual thread), whilecompareAndSetinfinallyprevents the exiting old thread from nulling out a newer reconnect's emitter.handleResourcesSubscribeis nowsynchronizedon the subscriptions set, preventing two concurrent subscribes near the cap from both passing and slightly overshooting it.sanitizeMessage()in the "Unknown tool: X" JSON-RPC error, matching the path used everywhere else error text is constructed.McpServerhas no built-in authentication and must be wrapped (e.g. withserve auth'sAuthMiddleware) before exposure to untrusted clients.shouldEvictIdleSessionAfterTimeout(configures a 200ms idle timeout, fills the session cap, waits for the reaper to sweep, asserts the slot is freed) andshouldCloseOldSseEmitterOnReconnect(opens an SSE stream, reconnects with the same session ID, asserts the first stream'sdonefuture completes — proving the emitter was closed rather than leaked).