fix(mcp): flush stdout/stderr before exiting the CLI to stop large output truncation#8613
Closed
JSONbored wants to merge 1 commit into
Closed
fix(mcp): flush stdout/stderr before exiting the CLI to stop large output truncation#8613JSONbored wants to merge 1 commit into
JSONbored wants to merge 1 commit into
Conversation
…tput truncation process.exit() right after a command finished could race an in-flight, asynchronous POSIX write to a piped stdout -- the packaged CHANGELOG.md (>64KB, past the OS pipe buffer size) was getting cut off mid-write when `changelog --json` piped its output to a consumer, breaking JSON parsing on the other end. Add waitForStdioFlush() (packages/loopover-mcp/lib/cli-error.ts) and await it before each process.exit() call in the CLI dispatcher. Setting process.exitCode and relying on a natural exit instead isn't safe either: a parent that spawns this CLI via child_process with default stdio (e.g. Node's own execFile) leaves the child's stdin pipe open, which keeps the event loop alive forever.
Contributor
|
Superagent didn't find any vulnerabilities or security issues in this PR. |
Owner
Author
|
Closing as duplicate: the identical bug (process.exit() racing an in-flight async POSIX pipe write, truncating loopover-mcp CLI output over ~64KB) was independently found and fixed in #8612, already merged into main. Verified main's fix resolves the originally-failing test and the full mcp-cli test suite stays green. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
loopover-mcp changelog --jsontruncated its output mid-write whenever the packagedCHANGELOG.mdgrew past the OS pipe buffer size (~64KB) —test/unit/mcp-cli-profiles.test.ts's "reports package status and prints the packaged changelog" test was failing deterministically onmainas a result, blocking a greentest:cigate for every branch.process.exit()immediately after a command finished. Writes to a pipedprocess.stdoutare asynchronous on POSIX, soprocess.exit()could terminate the process before the OS finished draining a large write.waitForStdioFlush()(packages/loopover-mcp/lib/cli-error.ts) andawaitit before eachprocess.exit()call. Settingprocess.exitCodeand relying on a natural exit instead isn't safe either — it was tried and reproducibly deadlocked whenever this CLI is spawned viachild_processwith default (piped, unclosed) stdio, e.g. Node's ownexecFile, which is exactly how this repo's own CLI test harness spawns it.Scope
type(scope): short summaryConventional Commit format.CONTRIBUTING.mdand does not reintroduce GitHub Pages, VitePress,site/, orCNAME.main.Validation
git diff --checknpm run typechecknpm run build:mcpnpm run test:mcp-packtest/unit/mcp-cli-error.test.ts(newwaitForStdioFlushunit tests, both branches + empty-list edge case) and the fulltest/unit/mcp-cli-*.test.ts+mcp-tool-rename-aliases.test.tssuite (76 files / 426 tests) — all green.waitForStdioFlushis 100% line/branch/function covered percoverage/lcov.info. The dispatcher's own 4 touched lines (theawait waitForStdioFlush(...)call sites) remain structurally uncoverable — they only execute in a real subprocess spawn, same as the rest of this file's CLI dispatch block (see the existingv8 ignorecomment andcodecov.yml's note onpackages/loopover-mcp/bin/loopover-mcp.ts).npm run test:ci(full local gate) run twice end-to-end against an earlier version of this fix (before extractingwaitForStdioFlushintolib/cli-error.tsfor testability) — both green aside from 3 (then 2) flakes, each independently confirmed unrelated: CPU contention from concurrent test runs in sibling worktrees, and a localui-kitbuild-order gap in manual invocation.npm run actionlint(no workflow files touched),npm run test:workers(no worker code touched),npm run ui:*(no UI code touched),npm audit(no dependency changes — verified viagit diff --name-onlyagainst the rebase base).Safety
Notes