Skip to content

Commit 56b6e7d

Browse files
chore: avoid multiple calls to onclose when terminating
1 parent 1152ecd commit 56b6e7d

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

src/client/stdio.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ export class StdioClientTransport implements Transport {
134134
this._process.on('error', error => {
135135
if (error.name === 'AbortError') {
136136
// Expected when close() is called.
137-
this.onclose?.();
138137
return;
139138
}
140139

src/integration-tests/process-cleanup.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Client } from '../client/index.js';
2+
import { StdioClientTransport } from '../client/stdio.js';
13
import { Server } from '../server/index.js';
24
import { StdioServerTransport } from '../server/stdio.js';
35

@@ -25,4 +27,30 @@ describe('Process cleanup', () => {
2527
// The test runner will fail if the process hangs
2628
expect(true).toBe(true);
2729
});
30+
31+
it('onclose should be called exactly once', async () => {
32+
const client = new Client({
33+
name: 'test-client',
34+
version: '1.0.0'
35+
});
36+
37+
const transport = new StdioClientTransport({
38+
command: process.argv0,
39+
args: ['test-server.js'],
40+
cwd: __dirname
41+
});
42+
43+
let onCloseWasCalled = 0;
44+
client.onclose = () => {
45+
onCloseWasCalled++;
46+
};
47+
48+
await client.connect(transport);
49+
await client.close();
50+
51+
// A short delay to allow the close event to propagate
52+
await new Promise(resolve => setTimeout(resolve, 50));
53+
54+
expect(onCloseWasCalled).toBe(1);
55+
});
2856
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { McpServer } from "../../dist/esm/server/mcp.js";
2+
import { StdioServerTransport } from "../../dist/esm/server/stdio.js";
3+
4+
const transport = new StdioServerTransport();
5+
6+
const server = new McpServer({
7+
name: "test-server",
8+
version: "1.0.0",
9+
});
10+
11+
await server.connect(transport);

0 commit comments

Comments
 (0)