Skip to content

Commit 85ecec4

Browse files
hyperpolymathclaude
andcommitted
fix: drain pending async handlers before stdin EOF exit
The MCP bridge called handleMessage() without await, causing a race where process.exit(0) on stdin EOF could kill in-flight fetch() calls. Now tracks pending promises and drains them before exiting. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 082eb40 commit 85ecec4

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

mcp-bridge/main.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ let buffer = "";
252252
const MAX_BUFFER_BYTES = 2 * MAX_INPUT_SIZE_BYTES;
253253

254254
process.stdin.setEncoding("utf8");
255+
// Track in-flight message handlers so we can drain before exit.
256+
const pendingMessages = [];
257+
255258
process.stdin.on("data", (chunk) => {
256259
buffer += chunk;
257260
// HARDENING: Drop the buffer if it grows beyond the safety limit
@@ -265,12 +268,16 @@ process.stdin.on("data", (chunk) => {
265268
const line = buffer.slice(0, boundary).trim();
266269
buffer = buffer.slice(boundary + 1);
267270
if (line.length > 0) {
268-
handleMessage(line);
271+
// Queue the async handler and track it so stdin EOF doesn't race.
272+
const p = handleMessage(line).catch(() => {});
273+
pendingMessages.push(p);
269274
}
270275
}
271276
});
272277

273-
process.stdin.on("end", () => {
278+
process.stdin.on("end", async () => {
279+
// Drain all pending message handlers before exiting.
280+
await Promise.allSettled(pendingMessages);
274281
process.exit(0);
275282
});
276283

0 commit comments

Comments
 (0)