From 65e405166246af98c00bab6827218e77fa3f04d6 Mon Sep 17 00:00:00 2001 From: clawsweeper <274271284+clawsweeper[bot]@users.noreply.github.com> Date: Tue, 23 Jun 2026 08:43:44 +0000 Subject: [PATCH] fix: flush CLI stdio before forced exit --- src/cli.ts | 3 +++ tests/cli-force-exit-behavior.integration.test.ts | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/src/cli.ts b/src/cli.ts index 2630176..2294924 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -380,6 +380,9 @@ function flushWriteStream(stream: NodeJS.WriteStream, timeoutMs: number): Promis if (!stream.writable || stream.destroyed || stream.writableEnded) { return Promise.resolve(); } + if (stream.writableLength === 0 && !stream.writableNeedDrain) { + return Promise.resolve(); + } return new Promise((resolve) => { let settled = false; diff --git a/tests/cli-force-exit-behavior.integration.test.ts b/tests/cli-force-exit-behavior.integration.test.ts index f9f04a5..9576d61 100644 --- a/tests/cli-force-exit-behavior.integration.test.ts +++ b/tests/cli-force-exit-behavior.integration.test.ts @@ -50,6 +50,10 @@ const [cliEntry, configPath] = process.argv.slice(2); process.env.MCPORTER_DISABLE_AUTORUN = '1'; let cleanupWriteSeen = false; +Object.defineProperty(process.stdout, 'writableNeedDrain', { + configurable: true, + get: () => true, +}); const originalWrite = process.stdout.write.bind(process.stdout); process.stdout.write = (chunk, encoding, callback) => { const done = typeof encoding === 'function' ? encoding : callback;