Skip to content

Commit 6645340

Browse files
chore: avoid multiple calls to onclose when terminating
1 parent 449cdbe commit 6645340

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

src/client/stdio.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ export class StdioClientTransport implements Transport {
138138
this._process.on("error", (error) => {
139139
if (error.name === "AbortError") {
140140
// Expected when close() is called.
141-
this.onclose?.();
142141
return;
143142
}
144143

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,30 @@ describe("Process cleanup", () => {
6060

6161
expect(isProcessRunning(pid!)).toBe(false);
6262
});
63+
64+
it("onclose should be called exactly once", async () => {
65+
const client = new Client({
66+
name: "test-client",
67+
version: "1.0.0",
68+
});
69+
70+
const transport = new StdioClientTransport({
71+
command: process.argv0,
72+
args: ["test-server.js"],
73+
cwd: __dirname,
74+
});
75+
76+
let onCloseWasCalled = 0;
77+
client.onclose = () => {
78+
onCloseWasCalled++;
79+
};
80+
81+
await client.connect(transport);
82+
await client.close();
83+
84+
// A short delay to allow the close event to propagate
85+
await new Promise((resolve) => setTimeout(resolve, 50));
86+
87+
expect(onCloseWasCalled).toBe(1);
88+
});
6389
});
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)