Skip to content

Added clean shutdown + tests #115

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
merged 4 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/integration-tests/process-cleanup.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Server } from "../server/index.js";
import { StdioServerTransport } from "../server/stdio.js";

describe("Process cleanup", () => {
jest.setTimeout(5000); // 5 second timeout

it("should exit cleanly after closing transport", async () => {
const server = new Server(
{
name: "test-server",
version: "1.0.0",
},
{
capabilities: {},
}
);

const transport = new StdioServerTransport();
await server.connect(transport);

// Close the transport
await transport.close();

// If we reach here without hanging, the test passes
// The test runner will fail if the process hangs
expect(true).toBe(true);
});
});
11 changes: 11 additions & 0 deletions src/server/stdio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,19 @@ export class StdioServerTransport implements Transport {
}

async close(): Promise<void> {
// Remove our event listeners first
this._stdin.off("data", this._ondata);
this._stdin.off("error", this._onerror);

// Check if we were the only data listener
const remainingDataListeners = this._stdin.listenerCount('data');
if (remainingDataListeners === 0) {
// Only pause stdin if we were the only listener
// This prevents interfering with other parts of the application that might be using stdin
this._stdin.pause();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit surprised that this has any effect on termination, but then reading about Node.js readable streams, I'm like 😵

It feels a bit janky to do things this way, but I don't really have a better idea…

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per top-level PR comment, I don't see how both MCP and anything else can co-exist on the same stdin stream.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certainly not while messages are streaming in. The only question is whether there'd ever be a reason for a server implementation to want to read from stdin after terminating the normal MCP stuff. Probably not? But it's always hard to anticipate how people will use a library.

}

// Clear the buffer and notify closure
this._readBuffer.clear();
this.onclose?.();
}
Expand Down
Loading