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
7 changes: 7 additions & 0 deletions packages/server/src/server/stdio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ export class StdioServerTransport implements Transport {
_onerror = (error: Error) => {
this.onerror?.(error);
};
_onstdinclose = () => {
this.close().catch(() => {});
};
_onstdouterror = (error: Error) => {
this.onerror?.(error);
this.close().catch(() => {
Expand All @@ -74,6 +77,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._onstdinclose);
this._stdout.on('error', this._onstdouterror);
}

Expand Down Expand Up @@ -101,6 +106,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._onstdinclose);
this._stdout.off('error', this._onstdouterror);

// Check if we were the only data listener
Expand Down
30 changes: 30 additions & 0 deletions packages/server/test/server/stdio.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,36 @@ test('should not fire onclose twice when close() is called after stdout error',
expect(closeCount).toBe(1);
});

test('should close when stdin closes', async () => {
const server = new StdioServerTransport(input, output);

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

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

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

test('should close when stdin ends', async () => {
const server = new StdioServerTransport(input, output);

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

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

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

test('should reject send() when stdout errors before drain', async () => {
let completeWrite: ((error?: Error | null) => void) | undefined;
const slowOutput = new Writable({
Expand Down
Loading