StdioClientTransport.close() doesn't clean up listeners, causing EventEmitter memory leak warning

オープン
#3,070 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@shhhhhhjokerist がすでに取り組んでいます。

2026年11月8日 から。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
node.js, typescript
領域
backend, devtools

調査の方向性

Start with StdioClientTransport.start() and close() in the transport implementation, then trace how the stderr pipe() and process stream listeners are cleaned up. Reproduce the warning with a local stdio MCP server that writes to stderr, and check the transport’s existing lifecycle tests. Done means closing the transport removes the leaked listeners and repeated use no longer triggers the warning; an open linked pull request is already addressing this issue.

索引モデルが issue の本文から書いたものです。

説明

v1 v2

Bug: StdioClientTransport.close() doesn't clean up listeners, causing EventEmitter memory leak warning

Description

When using local (stdio) MCP servers, Node.js emits the following warning:

MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 drain listeners added to [WriteStream]. MaxListeners is 10. Use emitter.setMaxListeners() to increase limit
Root cause

In StdioClientTransport:

  1. start() attaches permanent listeners to the stdio pipes:
this._process.stdin?.on("error", ...)
this._process.stdout?.on("data", ...)
this._process.stdout?.on("error", ...)
this._process.stderr.pipe(this._stderrStream)  // pipe() internally adds a "drain" listener
  1. close() kills the process but doesn't remove any listeners:
async close(){
  // ... kills the process ...
  // NO removeListener / off / unpipe here
}

The pipe() call on stderr automatically adds a drain listener on the source stream to handle back-pressure. When the process is killed, this listener is orphaned.

Reproduction
  1. Configure any local (stdio) MCP server that produces stderr output
  2. Launch an MCP client (e.g., opencode TUI)
  3. Observe the warning in stderr

Minimal MCP servers that don't produce stderr output don't trigger the warning. Real MCP servers (gitlab, google-workspace, etc.) that log to stderr do trigger it.

Proposed fix

In close(), add proper cleanup:

this._process.stderr?.unpipe()
this._process.stdin?.removeAllListeners()
this._process.stdout?.removeAllListeners()
this._process.stderr?.removeAllListeners()

Or alternatively, raise the max listeners limit in start():

this._process.stderr?.setMaxListeners(20)
Impact
主要言語
TypeScript
スター
13.8k
フォーク
2.5k
平均マージ
6日 23時間
マージ済み PR(30日)
2

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

modelcontextprotocol/typescript-sdk のほかの issue

modelcontextprotocol/typescript-sdk の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。