Skip to content

Commit 8f83e6f

Browse files
olaservoclaude
andcommitted
fix(everything): address review feedback on shutdown and subscription typing
From Copilot's review of #4551. transports/streamableHttp.ts — `httpServer.close()` is callback-based, and `process.exit(0)` ran immediately after it, so the process could exit with the listen socket still open and pending writes unflushed. Now awaited. `closeAllConnections()` is called first because `close()` waits for existing connections to end; that call is defensive rather than demonstrated -- a contrast test with an idle keep-alive connection closed in ~2ms either way, so it guards the documented behavior, not a reproduced hang. Verified the sequence resolves and the port is genuinely rebindable afterwards, i.e. the close completes rather than merely being initiated. resources/subscriptions.ts — `ctx.sessionId` was cast `as string` in both handlers directly beneath a comment saying it can be undefined, and the subscriber sets were then narrowed to `Set<string>` while the map is declared `Map<string, Set<string | undefined>>`. The runtime behavior was already correct: on stdio there is no session, `undefined` is a legitimate subscriber key identifying the single sessionless connection, and the update path matches on it. Only the types disagreed. Casts removed and the intent stated in a comment; the lookups collapse to `??` and `?.` in the process. Also made `sendSimulatedResourceUpdates` synchronous -- it was declared `async`/`Promise<void>` while awaiting nothing, since publishing through the notifier is fire-and-forget, and the JSDoc claimed it resolved once notifications were sent. Both call sites already discarded the return value. While there, dropped a dead `else` branch in the same loop that called `subscribers.delete(sessionId)` on the branch where `subscribers.has( sessionId)` was already false -- a guaranteed no-op, commented "subscriber has disconnected". It cannot reap anyone; that is `cleanupSession` -> `stopSimulatedResourceUpdates`. Pre-existing on main and not introduced here, removed because the surrounding lines were already being rewritten. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 6d8c190 commit 8f83e6f

2 files changed

Lines changed: 30 additions & 21 deletions

File tree

‎src/everything/resources/subscriptions.ts‎

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ export const setSubscriptionHandlers = (server: McpServer) => {
3838
// Get the URI to subscribe to
3939
const { uri } = request.params;
4040

41-
// Get the session id (can be undefined for stdio)
42-
const sessionId = ctx.sessionId as string;
41+
// No session id on stdio. `undefined` is a legitimate subscriber key
42+
// here -- it identifies the single sessionless connection -- so it is
43+
// carried as-is rather than cast away.
44+
const sessionId: string | undefined = ctx.sessionId;
4345

4446
// Acknowledge the subscribe request
4547
await server.sendLoggingMessage(
@@ -53,9 +55,8 @@ export const setSubscriptionHandlers = (server: McpServer) => {
5355
);
5456

5557
// Get the subscribers for this URI
56-
const subscribers = subscriptions.has(uri)
57-
? (subscriptions.get(uri) as Set<string>)
58-
: new Set<string>();
58+
const subscribers =
59+
subscriptions.get(uri) ?? new Set<string | undefined>();
5960
subscribers.add(sessionId);
6061
subscriptions.set(uri, subscribers);
6162
return {};
@@ -69,8 +70,10 @@ export const setSubscriptionHandlers = (server: McpServer) => {
6970
// Get the URI to subscribe to
7071
const { uri } = request.params;
7172

72-
// Get the session id (can be undefined for stdio)
73-
const sessionId = ctx.sessionId as string;
73+
// No session id on stdio. `undefined` is a legitimate subscriber key
74+
// here -- it identifies the single sessionless connection -- so it is
75+
// carried as-is rather than cast away.
76+
const sessionId: string | undefined = ctx.sessionId;
7477

7578
// Acknowledge the subscribe request
7679
await server.sendLoggingMessage(
@@ -84,10 +87,7 @@ export const setSubscriptionHandlers = (server: McpServer) => {
8487
);
8588

8689
// Remove the subscriber
87-
if (subscriptions.has(uri)) {
88-
const subscribers = subscriptions.get(uri) as Set<string>;
89-
if (subscribers.has(sessionId)) subscribers.delete(sessionId);
90-
}
90+
subscriptions.get(uri)?.delete(sessionId);
9191
return {};
9292
}
9393
);
@@ -107,25 +107,27 @@ export const setSubscriptionHandlers = (server: McpServer) => {
107107
* filter decides who receives a `resources/updated`, so the notifier is called
108108
* for every known URI and the bus does the filtering.
109109
*
110+
* Synchronous: publishing through the notifier is fire-and-forget on both
111+
* paths, so there is nothing to await.
112+
*
110113
* @param {McpServer} server - The server instance used to send notifications.
111114
* @param {string | undefined} sessionId - The session ID of the client to check for subscriptions.
112-
* @returns {Promise<void>} Resolves once all applicable notifications are sent.
113115
*/
114-
const sendSimulatedResourceUpdates = async (
116+
const sendSimulatedResourceUpdates = (
115117
server: McpServer,
116118
sessionId: string | undefined
117-
): Promise<void> => {
119+
): void => {
118120
const notifier = getNotifier(server);
119121

120122
// Search all URIs for ones this client is subscribed to
121-
for (const uri of subscriptions.keys()) {
122-
const subscribers = subscriptions.get(uri) as Set<string | undefined>;
123-
124-
// If this client is subscribed, send the notification
123+
for (const [uri, subscribers] of subscriptions) {
124+
// If this client is subscribed, send the notification. Subscribers are
125+
// reaped by `cleanupSession` -> `stopSimulatedResourceUpdates` when the
126+
// session ends, not here: a session that is absent from this URI's set is
127+
// simply not subscribed to it, which says nothing about whether it is
128+
// still connected.
125129
if (subscribers.has(sessionId)) {
126130
notifier.resourceUpdated(uri);
127-
} else {
128-
subscribers.delete(sessionId); // subscriber has disconnected
129131
}
130132
}
131133
};

‎src/everything/transports/streamableHttp.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,14 @@ process.on("SIGINT", async () => {
8585
// sends the graceful-close result on every open `subscriptions/listen` stream.
8686
await handler.close();
8787
setBusNotifier(undefined);
88-
httpServer.close();
88+
89+
// `close()` stops new connections but resolves only once the existing ones
90+
// end, and it is callback-based -- exiting straight after calling it can kill
91+
// the process with the listen socket still open and pending writes unflushed.
92+
// Drop keep-alive sockets first so the close can actually complete, then wait
93+
// for it.
94+
httpServer.closeAllConnections();
95+
await new Promise<void>((resolve) => httpServer.close(() => resolve()));
8996

9097
console.log("Server shutdown complete");
9198
process.exit(0);

0 commit comments

Comments
 (0)