Skip to content
Open
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
14 changes: 14 additions & 0 deletions packages/server/src/server/stdio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ 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
});
};
_onstdinend = () => {
this.close().catch(() => {
// Ignore errors during close — stdin pipe ended
});
};

/**
* Starts listening for messages on `stdin`.
Expand All @@ -58,6 +68,8 @@ 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._stdin.on('end', this._onstdinend);
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 +97,8 @@ 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._stdin.off('end', this._onstdinend);
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
53 changes: 53 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,56 @@ 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 () => {
// Use autoDestroy:false + emitClose:false so push(null) fires 'end' but NOT 'close',
// ensuring the test fails unless an 'end' listener is explicitly registered.
const endOnlyInput = new Readable({
autoDestroy: false,
emitClose: false,
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); // confirms close was NOT emitted
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