Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions packages/server/src/server/stdio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ export class StdioServerTransport implements Transport {
// Ignore errors during close — we're already in an error path
});
};
_onstdinclose = () => {
this.close().catch(() => {
// Ignore errors during close — stdin pipe ended
});
};

/**
* Starts listening for messages on `stdin`.
Expand All @@ -58,6 +63,7 @@ export class StdioServerTransport implements Transport {
this._started = true;
this._stdin.on('data', this._ondata);
this._stdin.on('error', this._onerror);
this._stdin.on('close', this._onstdinclose);
this._stdout.on('error', this._onstdouterror);
Comment on lines 68 to 73
Copy link

Copilot AI May 1, 2026

Choose a reason for hiding this comment

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

start() only listens for stdin's close event, but a disconnected pipe commonly emits end (and close is not guaranteed for all Readable streams). To reliably shut down on client disconnect, also listen for stdin's end event and remove that listener in close() alongside the existing close cleanup.

Copilot uses AI. Check for mistakes.
}

Expand Down Expand Up @@ -85,6 +91,7 @@ export class StdioServerTransport implements Transport {
// Remove our event listeners first
this._stdin.off('data', this._ondata);
this._stdin.off('error', this._onerror);
this._stdin.off('close', this._onstdinclose);
this._stdout.off('error', this._onstdouterror);

Comment on lines 97 to 103
Copy link

Copilot AI May 1, 2026

Choose a reason for hiding this comment

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

close() cleans up the newly added stdin close handler, but if you add an end handler for stdin (needed for reliable pipe-disconnect detection), it should also be removed here to avoid leaking listeners across start/close cycles.

Copilot uses AI. Check for mistakes.
// Check if we were the only data listener
Expand Down
43 changes: 43 additions & 0 deletions packages/server/test/server/stdio.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,46 @@ test('should fire onerror before onclose on stdout error', async () => {

expect(events).toEqual(['error', 'close']);
});

test('should fire onclose when stdin emits close', async () => {
const server = new StdioServerTransport(input, output);
server.onerror = error => { throw error; };

let closeCount = 0;
server.onclose = () => { closeCount++; };

await server.start();
input.emit('close');

expect(closeCount).toBe(1);
});

test('should fire onclose when stdin emits end', async () => {
const server = new StdioServerTransport(input, output);
server.onerror = error => { throw error; };

let closeCount = 0;
server.onclose = () => { closeCount++; };

await server.start();
input.push(null); // signals end-of-stream

// Allow microtasks to flush
await new Promise(resolve => setTimeout(resolve, 0));

Copy link

Copilot AI May 1, 2026

Choose a reason for hiding this comment

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

This test claims to validate behavior on stdin end, but input.push(null) may also trigger a close event depending on stream settings, so it can pass even if the transport doesn't handle end at all. Make the test explicitly verify the end path (e.g., emit end without close, or construct a Readable where emitClose/autoDestroy won't emit close on end) so it fails unless an end listener is implemented.

Suggested change
const server = new StdioServerTransport(input, output);
server.onerror = error => { throw error; };
let closeCount = 0;
server.onclose = () => { closeCount++; };
await server.start();
input.push(null); // signals end-of-stream
// Allow microtasks to flush
await new Promise(resolve => setTimeout(resolve, 0));
const endOnlyInput = new Readable({
autoDestroy: false,
emitClose: false,
// We'll use endOnlyInput.push() instead.
read: () => {}
});
const server = new StdioServerTransport(endOnlyInput, output);
server.onerror = error => { throw error; };
let closeCount = 0;
let inputCloseCount = 0;
server.onclose = () => { closeCount++; };
endOnlyInput.on('close', () => { inputCloseCount++; });
await server.start();
endOnlyInput.push(null); // signals end-of-stream without emitting close
// Allow microtasks to flush
await new Promise(resolve => setTimeout(resolve, 0));
expect(inputCloseCount).toBe(0);

Copilot uses AI. Check for mistakes.
expect(closeCount).toBe(1);
});

test('should not fire onclose twice when close() called after stdin close', async () => {
const server = new StdioServerTransport(input, output);
server.onerror = () => {};

let closeCount = 0;
server.onclose = () => { closeCount++; };

await server.start();
input.emit('close');
await server.close();

expect(closeCount).toBe(1);
});
Loading