From 8d3c9f8d31d6bfd6793eaed1a65b456a6bf2a8aa Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Wed, 18 Nov 2026 08:33:26 +0800 Subject: [PATCH] fix(ida): drain stderr after a broken startup pipe (#11914) --- src/ida/IdaMcpConnection.ts | 13 +++++++- src/ida/IdaMcpOwnedCleanup.test.ts | 51 +++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/src/ida/IdaMcpConnection.ts b/src/ida/IdaMcpConnection.ts index 072fe973f3..9d56d877d8 100644 --- a/src/ida/IdaMcpConnection.ts +++ b/src/ida/IdaMcpConnection.ts @@ -710,7 +710,18 @@ export const createIdaMcpConnection = ( async connect() { await client .connect(transport, { timeout: config.timeoutMs }) - .catch((cause: unknown) => transportFailure(cause, "ida_mcp_connect")); + .catch(async (cause: unknown) => { + // A launcher can close stdin before its diagnostic reaches stderr. + // Join the SDK's bounded teardown before taking the diagnostic snapshot. + if ( + stdio !== undefined && + cause instanceof Error && + "code" in cause && + cause.code === "EPIPE" + ) + await stdio.close(); + return transportFailure(cause, "ida_mcp_connect"); + }); const catalog = await client .listTools(undefined, { timeout: config.timeoutMs, diff --git a/src/ida/IdaMcpOwnedCleanup.test.ts b/src/ida/IdaMcpOwnedCleanup.test.ts index f1552f50cc..65edcaf09c 100644 --- a/src/ida/IdaMcpOwnedCleanup.test.ts +++ b/src/ida/IdaMcpOwnedCleanup.test.ts @@ -10,16 +10,65 @@ import { createIdaMcpConnection } from "./IdaMcpConnection.js"; class FixtureProcess extends EventEmitter implements ProviderProcessHandle { readonly pid = 1; - readonly stdin = new PassThrough(); readonly stdout = new PassThrough(); readonly stderr = new PassThrough(); readonly exitCode = null; readonly signalCode = null; + constructor(readonly stdin = new PassThrough()) { + super(); + } kill(): boolean { return true; } } +it("reports startup stderr drained after the handshake write fails", async () => { + const diagnostic = "IDA runtime initialization failed\n"; + const child = new FixtureProcess( + new PassThrough({ + write(_chunk, _encoding, done) { + done(Object.assign(new Error("write EPIPE"), { code: "EPIPE" })); + // Closing the protocol input and draining stderr are separate events. + setImmediate(() => { + child.emit("exit", 2, null); + child.stderr.write(diagnostic); + child.emit("close", 2, null); + }); + }, + }), + ); + const launcher: OwnedMcpLauncher = async ({ runId }) => ({ + process: child, + ownership: { + runId, + leaderPid: child.pid, + processGroupId: child.pid, + expectedParentPid: child.pid, + }, + cleanup: async () => ({ cleaned: true, signaled: false }), + }); + const connection = createIdaMcpConnection( + { + command: "/opt/ida-mcp/server", + args: [], + env: {}, + protocol: "modern", + mode: "headless", + timeoutMs: 1000, + }, + {}, + { platform: "darwin", launcher }, + ); + try { + await expect(connection.connect()).rejects.toMatchObject({ + _tag: "ProviderAdapterError", + capturedOutput: { stderr: diagnostic, truncated: false }, + }); + } finally { + await connection.close(); + } +}); + it("keeps a failed handshake's incomplete cleanup observable with the redacted server tail", async () => { const child = new FixtureProcess(); let cleanupFails = true;