fix(ida): drain stderr after a broken startup pipe (#11914)

This commit is contained in:
morluto authored and GitHub committed 2026-11-18 08:33:26 +08:00
1 parent 5cdd7d54bf
commit 8d3c9f8d31
2 files changed
+62 -2

No files matched your search

+12 -1
View File
@@ -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,
+50 -1
View File
@@ -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;