Skip to content

Commit 67a1a31

Browse files
committed
fix(stdio): remove specific listeners in close(), keep no-op error handlers
Address review feedback: - Remove only the specific listeners attached in start() (data, error) instead of removeAllListeners(), to avoid removing error handlers that prevent uncaught exceptions from late EPIPE errors. - Keep no-op error handlers on stdin/stdout/stderr after close() so that late errors don't throw uncaught exceptions. - The 'drain' listener that accumulates with back-pressure is on _stderrStream (the pipe destination), not on processToClose.stderr. unpipe() removes it from _stderrStream. Refs: modelcontextprotocol#780, modelcontextprotocol#3070
1 parent a171c71 commit 67a1a31

1 file changed

Lines changed: 37 additions & 12 deletions

File tree

‎packages/client/src/client/stdio.ts‎

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ export class StdioClientTransport implements Transport {
109109
private _readBuffer: ReadBuffer;
110110
private _serverParams: StdioServerParameters;
111111
private _stderrStream: PassThrough | null = null;
112+
private _stdoutDataHandler?: (chunk: Buffer) => void;
113+
private _stdoutErrorHandler?: (error: Error) => void;
114+
private _stdinErrorHandler?: (error: Error) => void;
112115

113116
onclose?: () => void;
114117
onerror?: (error: Error) => void;
@@ -159,23 +162,26 @@ export class StdioClientTransport implements Transport {
159162
this.onclose?.();
160163
});
161164

162-
this._process.stdin?.on('error', error => {
165+
this._stdinErrorHandler = error => {
163166
this.onerror?.(error);
164-
});
167+
};
168+
this._process.stdin?.on('error', this._stdinErrorHandler);
165169

166-
this._process.stdout?.on('data', chunk => {
170+
this._stdoutDataHandler = chunk => {
167171
try {
168172
this._readBuffer.append(chunk);
169173
this.processReadBuffer();
170174
} catch (error) {
171175
this.onerror?.(error as Error);
172176
this.close().catch(() => {});
173177
}
174-
});
178+
};
179+
this._process.stdout?.on('data', this._stdoutDataHandler);
175180

176-
this._process.stdout?.on('error', error => {
181+
this._stdoutErrorHandler = error => {
177182
this.onerror?.(error);
178-
});
183+
};
184+
this._process.stdout?.on('error', this._stdoutErrorHandler);
179185

180186
if (this._stderrStream && this._process.stderr) {
181187
this._process.stderr.pipe(this._stderrStream);
@@ -314,17 +320,36 @@ export class StdioClientTransport implements Transport {
314320
}
315321
}
316322

317-
// Remove listeners attached in start() to prevent EventEmitter memory leak warnings.
318-
// The 'data' listener on stdout keeps the stream flowing; the 'drain' listener
319-
// added by stderr.pipe() accumulates when back-pressure is triggered.
323+
// Remove the specific listeners attached in start() to prevent
324+
// EventEmitter memory leak warnings. We keep no-op error handlers
325+
// so that late EPIPE/other errors don't throw uncaught exceptions.
326+
//
327+
// The 'drain' listener that accumulates with back-pressure is on
328+
// _stderrStream (the pipe destination), not on processToClose.stderr.
329+
// unpipe() removes it from _stderrStream.
320330
try {
321331
processToClose.stderr?.unpipe(this._stderrStream!);
322332
} catch {
323333
// ignore
324334
}
325-
processToClose.stdin?.removeAllListeners();
326-
processToClose.stdout?.removeAllListeners();
327-
processToClose.stderr?.removeAllListeners();
335+
336+
if (this._stdinErrorHandler) {
337+
processToClose.stdin?.off('error', this._stdinErrorHandler);
338+
this._stdinErrorHandler = undefined;
339+
}
340+
if (this._stdoutDataHandler) {
341+
processToClose.stdout?.off('data', this._stdoutDataHandler);
342+
this._stdoutDataHandler = undefined;
343+
}
344+
if (this._stdoutErrorHandler) {
345+
processToClose.stdout?.off('error', this._stdoutErrorHandler);
346+
this._stdoutErrorHandler = undefined;
347+
}
348+
349+
// Keep no-op error handlers so late errors don't throw uncaught.
350+
processToClose.stdin?.on('error', () => {});
351+
processToClose.stdout?.on('error', () => {});
352+
processToClose.stderr?.on('error', () => {});
328353
}
329354

330355
this._readBuffer.clear();

0 commit comments

Comments
 (0)