Skip to content

Commit b2b1920

Browse files
authored
Merge branch 'main' into fix/module-format
2 parents aca6164 + e72ff8d commit b2b1920

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Server } from "../server/index.js";
2+
import { StdioServerTransport } from "../server/stdio.js";
3+
4+
describe("Process cleanup", () => {
5+
jest.setTimeout(5000); // 5 second timeout
6+
7+
it("should exit cleanly after closing transport", async () => {
8+
const server = new Server(
9+
{
10+
name: "test-server",
11+
version: "1.0.0",
12+
},
13+
{
14+
capabilities: {},
15+
}
16+
);
17+
18+
const transport = new StdioServerTransport();
19+
await server.connect(transport);
20+
21+
// Close the transport
22+
await transport.close();
23+
24+
// If we reach here without hanging, the test passes
25+
// The test runner will fail if the process hangs
26+
expect(true).toBe(true);
27+
});
28+
});

src/server/stdio.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,19 @@ export class StdioServerTransport implements Transport {
6262
}
6363

6464
async close(): Promise<void> {
65+
// Remove our event listeners first
6566
this._stdin.off("data", this._ondata);
6667
this._stdin.off("error", this._onerror);
68+
69+
// Check if we were the only data listener
70+
const remainingDataListeners = this._stdin.listenerCount('data');
71+
if (remainingDataListeners === 0) {
72+
// Only pause stdin if we were the only listener
73+
// This prevents interfering with other parts of the application that might be using stdin
74+
this._stdin.pause();
75+
}
76+
77+
// Clear the buffer and notify closure
6778
this._readBuffer.clear();
6879
this.onclose?.();
6980
}

0 commit comments

Comments
 (0)