From b582f34649509f11265f9ca28daf50e3086cc80c Mon Sep 17 00:00:00 2001 From: Hermes Date: Thu, 19 Nov 2026 06:45:09 +0000 Subject: [PATCH] mcp: do not lose the first pipe when the second one fails stdio_exchange created two pipes with one short-circuiting condition: if (pipe(in_pipe) != 0 || pipe(out_pipe) != 0) { return -1; } When the first succeeded and the second failed, its two fds were already open and the error path did not close them -- one fd pair per failed start, in a long-running gateway. The analyzer (gcc -fanalyzer) named it; the second pipe now has its own failure path that closes the first pair. Junction built with ASAN+UBSAN and the Python suites (mcp, control, mcp_concurrency, mcp_envelope) pass without a report. --- src/mcp.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/mcp.c b/src/mcp.c index d2a8231..8b43a81 100644 --- a/src/mcp.c +++ b/src/mcp.c @@ -502,7 +502,15 @@ static int stdio_exchange(const j_mcp *m, const char *body, mcp_resp *out, doubl if (!st->ready) { if (st->pid > 0) stdio_stop(st); int in_pipe[2], out_pipe[2]; - if (pipe(in_pipe) != 0 || pipe(out_pipe) != 0) { + if (pipe(in_pipe) != 0) { + pthread_mutex_unlock(&st->mu); + return -1; + } + /* The second pipe can fail on its own; the first one's two fds are + * already open and leaked on that path (the analyzer found it). */ + if (pipe(out_pipe) != 0) { + close(in_pipe[0]); + close(in_pipe[1]); pthread_mutex_unlock(&st->mu); return -1; }