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

未關閉
#3,070 4 則留言 0 個 reaction 已指派 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 小時
30 天內合併 PR
2

環境準備

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 閱讀貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

modelcontextprotocol/typescript-sdk 的其他 Issue

查看 modelcontextprotocol/typescript-sdk 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。