-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
fix(mcp): prevent orphan processes by handling stdin close/end and startup race condition #2049
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,20 @@ function onceMessage(transport: CompatibleStdioServerTransport): Promise<any> { | |
| }); | ||
| } | ||
|
|
||
| function trackClose(transport: CompatibleStdioServerTransport): { | ||
| onclose: ReturnType<typeof vi.fn>; | ||
| closed: Promise<void>; | ||
| } { | ||
| const onclose = vi.fn(); | ||
| const closed = new Promise<void>((resolve) => { | ||
| transport.onclose = () => { | ||
| onclose(); | ||
| resolve(); | ||
| }; | ||
| }); | ||
| return { onclose, closed }; | ||
| } | ||
|
|
||
| describe('CompatibleStdioServerTransport', () => { | ||
| let stdin: PassThrough; | ||
| let stdout: PassThrough; | ||
|
|
@@ -258,4 +272,57 @@ describe('CompatibleStdioServerTransport', () => { | |
|
|
||
| expect(raw).toBe('{"jsonrpc":"2.0","id":1,"result":{"ok":true}}\n'); | ||
| }); | ||
|
|
||
| // ─── stdin lifecycle behavior ────────────────────────────── | ||
|
|
||
| it('stdin end closes transport and calls onclose exactly once', async () => { | ||
| const { onclose, closed } = trackClose(transport); | ||
|
|
||
| await transport.start(); | ||
| stdin.push(null); | ||
|
|
||
| await closed; | ||
| expect(onclose).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('stdin close closes transport and calls onclose exactly once', async () => { | ||
| const { onclose, closed } = trackClose(transport); | ||
|
|
||
| await transport.start(); | ||
| stdin.destroy(); | ||
|
|
||
| await closed; | ||
| expect(onclose).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('close() is idempotent: calling close twice calls onclose once', async () => { | ||
| const onclose = vi.fn(); | ||
| transport.onclose = onclose; | ||
|
|
||
| await transport.start(); | ||
| await transport.close(); | ||
| await transport.close(); | ||
|
|
||
| expect(onclose).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('start() immediately closes if stdin is already ended/destroyed before listeners register', async () => { | ||
| const endedStdin = new PassThrough(); | ||
| endedStdin.push(null); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2 [code-read] Verified: |
||
|
|
||
| const t = new CompatibleStdioServerTransport(endedStdin, new PassThrough()); | ||
| const { onclose, closed } = trackClose(t); | ||
|
|
||
| await t.start(); | ||
|
|
||
| await closed; | ||
| expect(onclose).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('start() after close throws', async () => { | ||
| await transport.start(); | ||
| await transport.close(); | ||
|
|
||
| await expect(transport.start()).rejects.toThrow(/close/i); | ||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.