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

Đang mở
#3,070 4 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@shhhhhhjokerist đang làm issue này rồi.

Từ ngày 8/11/2026.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
node.js, typescript
Lĩnh vực
backend, devtools

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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
Ngôn ngữ chính
TypeScript
Star
13.8k
Fork
2.5k
Merge trung bình
6 ngày 23 giờ
Pull request đã merge (30 ngày)
2

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của modelcontextprotocol/typescript-sdk

Tất cả issue của modelcontextprotocol/typescript-sdk

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.