Skip to content

Commit fcce4b6

Browse files
committed
fix: harden managed MCP session cleanup
1 parent 408c248 commit fcce4b6

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

scripts/benchmark-comparators.mjs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -390,9 +390,6 @@ async function runComparatorViaMcp(adapter, rootPath, tasks) {
390390
return taskResults;
391391
}
392392
).catch((err) => {
393-
if (err.message.startsWith('Failed to connect to')) {
394-
throw err;
395-
}
396393
throw new Error(
397394
err.message.includes('timed out')
398395
? `Failed to connect to ${adapter.name} MCP server: ${err.message}`

scripts/lib/managed-mcp-session.mjs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ function withTimeout(promise, timeoutMs) {
3232
});
3333
}
3434

35+
function delay(timeoutMs) {
36+
return new Promise((resolve) => {
37+
const timer = setTimeout(resolve, timeoutMs);
38+
timer.unref?.();
39+
});
40+
}
41+
3542
async function safeClose(client, transport, connected) {
3643
const closeAttempts = [];
3744

@@ -78,6 +85,10 @@ export async function withManagedStdioClientSession(options, callback) {
7885
}
7986
await new Promise((resolve) => setTimeout(resolve, 10));
8087
}
88+
89+
if (transport.pid !== null) {
90+
onSpawn(transport.pid);
91+
}
8192
})();
8293

8394
try {
@@ -88,6 +99,6 @@ export async function withManagedStdioClientSession(options, callback) {
8899
settling = true;
89100
await safeClose(client, transport, connected);
90101
await spawnNotification.catch(() => undefined);
91-
await connectPromise.catch(() => undefined);
102+
await Promise.race([connectPromise, delay(5_000)]).catch(() => undefined);
92103
}
93104
}

0 commit comments

Comments
 (0)