servers icon indicating copy to clipboard operation
servers copied to clipboard

everything: subscriptions map never drops a disconnected session

Open olaservo opened this issue 3 months ago • 1 comments

In src/everything/resources/subscriptions.ts, subscriptions is a module-level Map<uri, Set<sessionId>>. A session id is added on resources/subscribe and removed only on resources/unsubscribe. Nothing removes it when the session ends.

Both transports call cleanup(sessionId) from server.server.onclose (transports/streamableHttp.ts, transports/sse.ts). That function (server/index.ts:109) calls stopSimulatedLogging, stopSimulatedResourceUpdates and taskStore.cleanup(), so the interval timers are cleared, but subscriptions is not touched. A client that subscribes and then disconnects without unsubscribing leaves its session id in every Set it joined, for the life of the process.

Until #4104 the else branch of sendSimulatedResourceUpdates carried a comment saying it removed disconnected subscribers. It did not (it deleted from a Set that provably lacked the element), and the branch is now gone, so there is no longer anything that even implies cleanup exists.

Impact is small for a reference server, but it is the example people copy. Suggested fix: export a removeSubscriber(sessionId) from subscriptions.ts that iterates the map, deletes the session from each Set and drops empty entries, and call it from cleanup().

olaservo avatar Aug 28 '26 22:08 olaservo

I checked the subscription lifecycle in src/everything/resources/subscriptions.ts.

The module-level subscriptions map tracks active session ids per resource uri, but only removes them when an explicit resources/unsubscribe request is received. When a client connection terminates and cleanup(sessionId) is called from onclose, the subscriptions map is never pruned, which leads to dangling session ids and a memory leak over time.

We can add a cleanupSubscriptions(sessionId) helper that iterates and prunes the session from the map upon transport close, with corresponding unit tests. Happy to open a PR for this if you'd like.

hhhhzzzj avatar Aug 30 '26 13:08 hhhhzzzj