Commit 86f5317
authored
fix(transport): single Server instance causes response routing to wrong clients (#141)
* fix(transport): use per-session Server instances to prevent response routing to wrong clients
A single global Server instance caused `_transport` to be overwritten on each
new connection, routing responses to the most recently connected client instead
of the requesting one. This resulted in clients hanging indefinitely in
multi-session deployments.
Introduces SessionManager that creates isolated Server instances per session
with automatic idle timeout cleanup (30min default), disconnect-based cleanup
for SSE, and broadcast notifications across all active sessions.
Also fixes ERR_HTTP_HEADERS_SENT by checking res.headersSent before error responses.
Closes #138
* fix(transport): await session creation before handling requests
- Pre-generate session ID and await createSession before
handleWithContext to prevent race condition in StreamableHTTP
- Remove redundant handlersInitialized if-else in SessionManager
- Wrap sendToolsListChangedNotification in try/catch to prevent
error propagation to callers
- Add tests for onsessioninitialized/closed callbacks, OAuth
session association, graceful shutdown, SSE disconnect cleanup,
and headersSent guards
* style(session-manager): condense doc comment
* fix(session): add error handling, race guard, and duplicate session check
- Wrap SSE createSession calls in try/catch with proper error response
- Add schemaModeDetected flag to prevent setDetectedSchemaMode race condition
- Add duplicate sessionId guard in createSession (warns and closes existing)
- Add tests for all new behaviors
* refactor(server): remove unreachable SSE-only and StreamableHTTP-only transport cases
determineTransportMode() only returns "stdio" or "dual", making the
standalone "sse" and "streamable-http" switch cases dead code that
cannot be tested. The "dual" mode already provides both transports.
* fix(session): move duplicate sessionId check before resource allocation
Move the duplicate session guard to the beginning of createSession(),
before creating a new Server instance and registering handlers.
This avoids wasteful resource allocation when a duplicate is detected.1 parent f90e120 commit 86f5317
File tree
4 files changed
+1071
-287
lines changed- src
- tests/unit
4 files changed
+1071
-287
lines changed
0 commit comments