@utcp/mcp: healthy cached sessions leak abort listeners via the MCP SDK's SSE reconnect loop (graceful-close retry reset)
Summary
@utcp/mcp accumulates abort listeners on healthy, successfully-authenticated cached sessions — not only on the dead-session/401 path described in #34. The driver is the MCP SDK's standalone GET SSE notification stream: its reconnection retry counter resets to zero on every graceful close, and hosted MCP servers close idle streams every few seconds — so each process-lifetime cached session sits in an infinite reconnect loop, and every attempt parks an abort listener on the transport-lifetime AbortSignal that undici only releases on GC.
@utcp/mcp builds the transport without exposing reconnectionOptions, so consumers cannot cap or disable this from the outside.
Environment
@utcp/mcp1.1.3,@modelcontextprotocol/sdk1.29.0 (the relevant SDK code is unchanged in 1.30.0)- Node 22 (bundled undici), long-running server process
- Hosted MCP servers (SSE-capable) as
.toolsources — reproduced with three concurrently cached sessions
Symptom
MaxListenersExceededWarning: Possible EventTarget memory leak detected.
1696 abort listeners added to [AbortSignal]. MaxListeners is 1500.
Counts climb steadily around the clock with zero tool traffic — all three sessions register and call tools successfully (no 401s involved).
Allocation site (captured with an EventTarget.addEventListener tracing preload)
[abort-trace] target AbortSignal reached 1400 net 'abort' listeners. Add site:
at EventTarget.addEventListener (trace)
at Object.addAbortListener (node:internal/deps/undici/undici:1648:16)
at new Request (node:internal/deps/undici/undici:10116:18)
at fetch (node:internal/deps/undici/undici:10656:25)
at StreamableHTTPClientTransport._startOrAuthSse
(@modelcontextprotocol/sdk/src/client/streamableHttp.ts:220)
Mechanism (three parts, none wrong in isolation)
McpCommunicationProtocol._getOrCreateSessioncaches one client+transport perserver:transportfor the protocol's lifetime — reasonable, sessions are expensive.- The SDK's
StreamableHTTPClientTransportmaintains a standalone GET SSE stream for server-initiated notifications. In_handleSseStream, a stream that closes gracefully schedules_scheduleReconnection(options, 0)— the attempt counter resets, so the defaultmaxRetries: 2never engages against servers that politely close idle streams. Result: reconnect-every-few-seconds, forever, per cached session. - Every reconnect's
fetchregisters an abort listener on the transport-lifetime_abortController.signal(new Request→addAbortListener). undici releases these via FinalizationRegistry, i.e. only when GC collects the finished request — under steady churn the live count runs far ahead of collection.
The notification stream is dead weight for UTCP's strictly request/response tool usage, which makes the churn pure cost: background network chatter plus unbounded-until-GC listener growth.
Suggested fixes
- Expose
reconnectionOptionson the MCP call template / transport options so consumers can cap or disable notification-stream reconnection (maxRetries: 0is the right default for request/response tool calling, arguably). - Independently worth reporting to
modelcontextprotocol/typescript-sdk: the graceful-close path resetting the retry counter to 0 defeatsmaxRetriesentirely against servers that close idle streams cleanly (still present in 1.30.0).
Workaround we ship
pnpm patch on @utcp/[email protected] adding to the transport options in _getOrCreateSession:
reconnectionOptions: {
maxRetries: 0,
initialReconnectionDelay: 1e3,
reconnectionDelayGrowFactor: 1.5,
maxReconnectionDelay: 3e4,
}
Warnings stop; tool calls are unaffected. Trade-off: a genuinely dropped mid-call stream now fails instead of resuming — the right failure mode for these tools, but a behavior change.
Related: #34 reports the same warning via a different path (dead cached session never evicted on 401 — each retry on the dead transport accumulates the same way). Both share the underlying shape: one transport-lifetime signal fed into every fetch, with GC-bound listener release.