Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/everything/resources/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,22 @@ export const stopSimulatedResourceUpdates = (sessionId?: string) => {
subsUpdateIntervals.delete(sessionId);
}
};

/**
* Removes a disconnected session from all subscription Sets.
*
* Called from the server factory's `cleanup()` when a transport closes so
* that a client that subscribed and then disconnects without unsubscribing
* does not leave its sessionId in every Set it joined for the life of the
* process. Drops empty entries to avoid unbounded map growth.
*
* @param sessionId - The session ID to remove (can be undefined for stdio).
*/
export const removeSubscriber = (sessionId?: string) => {
for (const [uri, subscribers] of subscriptions.entries()) {
subscribers.delete(sessionId);
if (subscribers.size === 0) {
subscriptions.delete(uri);
}
}
};
2 changes: 2 additions & 0 deletions src/everything/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
import {
setSubscriptionHandlers,
stopSimulatedResourceUpdates,
removeSubscriber,
} from "../resources/subscriptions.js";
import { registerConditionalTools, registerTools } from "../tools/index.js";
import { registerResources, readInstructions } from "../resources/index.js";
Expand Down Expand Up @@ -110,6 +111,7 @@ export const createServer: () => ServerFactoryResponse = () => {
// Stop any simulated logging or resource updates that may have been initiated.
stopSimulatedLogging(sessionId);
stopSimulatedResourceUpdates(sessionId);
removeSubscriber(sessionId);
// Clean up task store timers
taskStore.cleanup();
if (initializeTimeout) clearTimeout(initializeTimeout);
Expand Down