Skip to content

Commit baddcdc

Browse files
committed
fix(stdio): remove listeners in close() to prevent EventEmitter memory leak
StdioClientTransport.close() was not removing the listeners attached in start() on the stdio pipes. The 'data' listener on stdout keeps the stream flowing, and the 'drain' listener added internally by stderr.pipe() accumulates when back-pressure is triggered, eventually exceeding the default MaxListeners limit of 10 and emitting: MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 drain listeners added to [WriteStream]. This commit adds proper cleanup in close(): - unpipe() the stderr stream to remove the internal 'drain' listener - removeAllListeners() on stdin, stdout, and stderr This is consistent with the existing _dispose() method which already destroys the pipe handles, and addresses the root cause reported in modelcontextprotocol#780 and modelcontextprotocol#3070. Refs: modelcontextprotocol#780, modelcontextprotocol#3070
1 parent 9adb8d5 commit baddcdc

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,18 @@ export class StdioClientTransport implements Transport {
313313
// ignore
314314
}
315315
}
316+
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.
320+
try {
321+
processToClose.stderr?.unpipe(this._stderrStream!);
322+
} catch {
323+
// ignore
324+
}
325+
processToClose.stdin?.removeAllListeners();
326+
processToClose.stdout?.removeAllListeners();
327+
processToClose.stderr?.removeAllListeners();
316328
}
317329

318330
this._readBuffer.clear();

0 commit comments

Comments
 (0)