Files
ruflo/v3/@claude-flow/cli/src/blocking-stdio.ts
T
ruv 275ec2905d fix(cli): flush piped stdout before process.exit (#3851)
Switch piped stdout/stderr to blocking writes at CLI.run start so the
process.exit() calls after a command no longer truncate output at the
64 KiB pipe buffer.

Co-Authored-By: RuFlo <ruv@ruv.net>
2026-10-07 00:21:45 -04:00

23 lines
925 B
TypeScript

/**
* #3851: stdout/stderr writes to a pipe are asynchronous on POSIX, so the
* `process.exit()` calls that follow a command (bin wrappers, CLI.run error
* paths, commands that exit themselves) discard whatever the consumer has not
* read yet, i.e. everything past the pipe buffer (64 KiB). Switching the pipe
* handles to blocking mode makes every write complete before it returns, so
* every exit path is safe. Same technique as the MCP stdio server.
* Files and TTYs are already synchronous and are left alone.
*/
export function ensureBlockingStdio(): void {
for (const stream of [process.stdout, process.stderr]) {
const s = stream as NodeJS.WriteStream & {
_handle?: { setBlocking?: (blocking: boolean) => void };
};
if (s.isTTY) continue;
try {
s._handle?.setBlocking?.(true);
} catch {
// best effort: never fail the CLI because stdio could not be switched
}
}
}