fix(mcp): close the listener before dropping open connections

closeAllConnections() ran before close(), so a connection accepted in
between missed the forced-close pass and close() could wait for its
request timeout. close() now starts first and closeAllConnections()
follows immediately, as Node documents.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Edgar ArakelyanandClaude Opus 5.5 committed 2026-10-18 16:25:57 -07:00
1 parent 964109a9b2
commit b6bb74ed9f
1 file changed
+5 -3
+5 -3
View File
@@ -385,16 +385,18 @@ export const serveStreamableHttp = async (
return { url, close };
};
/** Drop open connections and release the port. */
/** Stop accepting, drop open connections and release the port. */
const closeHttpServer = (
httpServer: ReturnType<typeof createHttpServer>,
): Promise<void> => {
httpServer.closeAllConnections();
return new Promise<void>((resolve, reject) => {
// close() first, as Node documents: a connection accepted before it would otherwise miss the forced-close pass.
const closed = new Promise<void>((resolve, reject) => {
httpServer.close((error) =>
error === undefined ? resolve() : reject(error),
);
});
httpServer.closeAllConnections();
return closed;
};
/** Translate one node:http exchange through `route`, logging a failure and answering it when headers are still open. */