Skip to content

Commit 75c8800

Browse files
committed
fix: handle EPIPE errors in StdioServerTransport gracefully
When a client disconnects abruptly (closes stdio pipes before the server can write a response), stdout.write() throws an unhandled EPIPE error that crashes the entire Node.js process. This commit: - Adds an error event listener on stdout that forwards errors to onerror and triggers a clean close() - Wraps stdout.write() in try/catch in send() to catch synchronous write errors and reject the promise instead of crashing - Removes the stdout error listener in close() to prevent leaks Fixes modelcontextprotocol#1564
1 parent 4a7cdf4 commit 75c8800

3 files changed

Lines changed: 81 additions & 5 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@modelcontextprotocol/server': patch
3+
---
4+
5+
Handle EPIPE errors in StdioServerTransport gracefully instead of crashing. When a client disconnects abruptly, the transport now catches stdout write errors, forwards them to `onerror`, and closes cleanly.

‎packages/server/src/server/stdio.ts‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ export class StdioServerTransport implements Transport {
3737
_onerror = (error: Error) => {
3838
this.onerror?.(error);
3939
};
40+
_onstdouterror = (error: Error) => {
41+
this.onerror?.(error);
42+
void this.close();
43+
};
4044

4145
/**
4246
* Starts listening for messages on `stdin`.
@@ -51,6 +55,7 @@ export class StdioServerTransport implements Transport {
5155
this._started = true;
5256
this._stdin.on('data', this._ondata);
5357
this._stdin.on('error', this._onerror);
58+
this._stdout.on('error', this._onstdouterror);
5459
}
5560

5661
private processReadBuffer() {
@@ -72,6 +77,7 @@ export class StdioServerTransport implements Transport {
7277
// Remove our event listeners first
7378
this._stdin.off('data', this._ondata);
7479
this._stdin.off('error', this._onerror);
80+
this._stdout.off('error', this._onstdouterror);
7581

7682
// Check if we were the only data listener
7783
const remainingDataListeners = this._stdin.listenerCount('data');
@@ -87,12 +93,16 @@ export class StdioServerTransport implements Transport {
8793
}
8894

8995
send(message: JSONRPCMessage): Promise<void> {
90-
return new Promise(resolve => {
96+
return new Promise((resolve, reject) => {
9197
const json = serializeMessage(message);
92-
if (this._stdout.write(json)) {
93-
resolve();
94-
} else {
95-
this._stdout.once('drain', resolve);
98+
try {
99+
if (this._stdout.write(json)) {
100+
resolve();
101+
} else {
102+
this._stdout.once('drain', resolve);
103+
}
104+
} catch (error) {
105+
reject(error);
96106
}
97107
});
98108
}

‎packages/server/test/server/stdio.test.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,64 @@ test('should read multiple messages', async () => {
102102
await finished;
103103
expect(readMessages).toEqual(messages);
104104
});
105+
106+
test('should handle stdout write errors gracefully', async () => {
107+
const brokenOutput = new Writable({
108+
write(_chunk, _encoding, callback) {
109+
callback(new Error('write EPIPE'));
110+
}
111+
});
112+
113+
const server = new StdioServerTransport(input, brokenOutput);
114+
115+
const errors: Error[] = [];
116+
server.onerror = error => {
117+
errors.push(error);
118+
};
119+
120+
let didClose = false;
121+
server.onclose = () => {
122+
didClose = true;
123+
};
124+
125+
await server.start();
126+
127+
const message: JSONRPCMessage = {
128+
jsonrpc: '2.0',
129+
id: 1,
130+
result: {}
131+
};
132+
133+
// The send itself should resolve (write returns true before async error),
134+
// but the error handler on the stream should fire and trigger close.
135+
await server.send(message);
136+
137+
// Allow the async error callback to fire
138+
await new Promise(resolve => setTimeout(resolve, 50));
139+
140+
expect(errors.length).toBeGreaterThan(0);
141+
expect(errors[0]!.message).toContain('EPIPE');
142+
expect(didClose).toBe(true);
143+
});
144+
145+
test('should handle synchronous stdout write throws gracefully', async () => {
146+
const throwingOutput = new Writable({
147+
write() {
148+
throw new Error('write EPIPE');
149+
}
150+
});
151+
152+
const server = new StdioServerTransport(input, throwingOutput);
153+
server.onerror = () => {};
154+
155+
await server.start();
156+
157+
const message: JSONRPCMessage = {
158+
jsonrpc: '2.0',
159+
id: 1,
160+
result: {}
161+
};
162+
163+
// send() should reject instead of crashing the process
164+
await expect(server.send(message)).rejects.toThrow('EPIPE');
165+
});

0 commit comments

Comments
 (0)