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.
This commit is contained in:
Hermes committed 2026-11-19 06:45:09 +00:00
1 parent 107de9e39e
commit b582f34649
1 file changed
+9 -1
+9 -1
View File
@@ -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;
}