-
Notifications
You must be signed in to change notification settings - Fork 300
fix(stdio): add stdin close/end listeners to prevent zombie/orphan processes #265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
punkpeye
merged 20 commits into
punkpeye:main
from
ElliotDrel:fix/stdin-close-zombie-process
Jul 23, 2026
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
d4082fd
fix(stdio): add stdin close/end listeners to prevent zombie processes
ElliotDrel 974caf7
style: run prettier on FastMCP.ts
ElliotDrel 32bcdee
fix: address Copilot review — idempotency, listener cleanup, and stdi…
ElliotDrel fe6f3b4
fix: resolve ESLint sort-objects and no-unused-vars in stdio test
ElliotDrel 3baf24f
fix(lint): remove unused params from stdinOffSpy mockImplementation
ElliotDrel 6ef82e0
fix(lint): apply prettier formatting to stdio test file
ElliotDrel 4003dff
fix(test): hoist vi.mock to module level to fix Vitest hoisting issue
ElliotDrel 35685ff
fix(test): use fake timers to skip FastMCPSession capability retry loop
ElliotDrel c69e4d0
fix(test): use regular function in vi.fn mock so new StdioServerTrans…
ElliotDrel 8ecdb87
test: add integration test for stdin-close zombie prevention
ElliotDrel 536db8e
style: fix prettier formatting in stdio integration test
ElliotDrel fb4a76c
fix(lint): fix perfectionist ordering in stdio integration test
ElliotDrel 801a6cb
fix(test): increase timeout for tsx cold-download in CI (60s ready, 9…
ElliotDrel 0ea79e6
fix(test): add tsx devDep, use installed binary instead of npx cold-d…
ElliotDrel 9adbbf5
fix: update pnpm-lock.yaml after adding tsx devDependency
ElliotDrel e0b453f
fix(test): use temp .ts file instead of --eval so ESM imports resolve…
ElliotDrel ab86420
style: fix prettier formatting in integration test
ElliotDrel 19bdd1c
fix(lint): sort named imports, add comment to empty catch blocks
ElliotDrel 21bcb71
revert: remove integration test and tsx devDep
ElliotDrel 4de57ee
Merge branch 'main' into fix/stdin-close-zombie-process
punkpeye File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| import { FastMCP } from "./FastMCP.js"; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Helpers | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| function makeFakeTransport() { | ||
| return { | ||
| close: vi.fn().mockResolvedValue(undefined), | ||
| onclose: undefined as (() => void) | undefined, | ||
| onerror: undefined as ((e: Error) => void) | undefined, | ||
| onmessage: undefined as ((msg: unknown) => void) | undefined, | ||
| send: vi.fn().mockResolvedValue(undefined), | ||
| start: vi.fn().mockResolvedValue(undefined), | ||
| }; | ||
| } | ||
|
|
||
| // Module-level so the vi.mock factory (hoisted) can close over it. | ||
| // Each test reassigns this in beforeEach. | ||
| let fakeTransport: ReturnType<typeof makeFakeTransport>; | ||
|
|
||
| // Must use a regular function (not arrow) so `new StdioServerTransport()` works. | ||
| vi.mock("@modelcontextprotocol/sdk/server/stdio.js", () => ({ | ||
| StdioServerTransport: vi.fn(function () { | ||
| return fakeTransport; | ||
| }), | ||
| })); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Tests | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| // session.connect() retries getClientCapabilities() 10×100ms (~1s real time). | ||
| // Give waitFor enough headroom beyond that. | ||
| const LISTENER_TIMEOUT = 3000; | ||
|
|
||
| describe("stdio stdin listener lifecycle", () => { | ||
| let stdinOnSpy: ReturnType<typeof vi.spyOn>; | ||
| let stdinOffSpy: ReturnType<typeof vi.spyOn>; | ||
| let stdinListeners: Map<string, (...args: unknown[]) => void>; | ||
|
|
||
| beforeEach(() => { | ||
| fakeTransport = makeFakeTransport(); | ||
| stdinListeners = new Map(); | ||
|
|
||
| stdinOnSpy = vi.spyOn(process.stdin, "on").mockImplementation(function ( | ||
| event: string, | ||
| listener: (...args: unknown[]) => void, | ||
| ) { | ||
| stdinListeners.set(event, listener); | ||
| return process.stdin; | ||
| }); | ||
|
|
||
| stdinOffSpy = vi | ||
| .spyOn(process.stdin, "off") | ||
| .mockImplementation(function () { | ||
| return process.stdin; | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it("registers 'close' and 'end' listeners after start({ transportType: 'stdio' })", async () => { | ||
| const server = new FastMCP({ name: "Test", version: "1.0.0" }); | ||
| server.start({ transportType: "stdio" }).catch(() => {}); | ||
|
|
||
| await vi.waitFor( | ||
| () => { | ||
| expect(stdinOnSpy).toHaveBeenCalledWith("close", expect.any(Function)); | ||
| expect(stdinOnSpy).toHaveBeenCalledWith("end", expect.any(Function)); | ||
| }, | ||
| { timeout: LISTENER_TIMEOUT }, | ||
| ); | ||
| }); | ||
|
|
||
| it("calls transport.close() exactly once when 'close' fires", async () => { | ||
| const server = new FastMCP({ name: "Test", version: "1.0.0" }); | ||
| server.start({ transportType: "stdio" }).catch(() => {}); | ||
|
|
||
| await vi.waitFor( | ||
| () => { | ||
| expect(stdinListeners.get("close")).toBeDefined(); | ||
| }, | ||
| { timeout: LISTENER_TIMEOUT }, | ||
| ); | ||
|
|
||
| stdinListeners.get("close")!(); | ||
| expect(fakeTransport.close).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("does NOT call transport.close() a second time when 'end' fires after 'close' (idempotency)", async () => { | ||
| const server = new FastMCP({ name: "Test", version: "1.0.0" }); | ||
| server.start({ transportType: "stdio" }).catch(() => {}); | ||
|
|
||
| await vi.waitFor( | ||
| () => { | ||
| expect(stdinListeners.get("close")).toBeDefined(); | ||
| expect(stdinListeners.get("end")).toBeDefined(); | ||
| }, | ||
| { timeout: LISTENER_TIMEOUT }, | ||
| ); | ||
|
|
||
| stdinListeners.get("close")!(); | ||
| stdinListeners.get("end")!(); | ||
|
|
||
| expect(fakeTransport.close).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("removes both listeners after the handler fires", async () => { | ||
| const server = new FastMCP({ name: "Test", version: "1.0.0" }); | ||
| server.start({ transportType: "stdio" }).catch(() => {}); | ||
|
|
||
| await vi.waitFor( | ||
| () => { | ||
| expect(stdinListeners.get("close")).toBeDefined(); | ||
| }, | ||
| { timeout: LISTENER_TIMEOUT }, | ||
| ); | ||
|
|
||
| const closeListener = stdinListeners.get("close")!; | ||
| closeListener(); | ||
|
|
||
| expect(stdinOffSpy).toHaveBeenCalledWith("close", closeListener); | ||
| expect(stdinOffSpy).toHaveBeenCalledWith("end", closeListener); | ||
| }); | ||
| }); |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.