StdioClientTransport.close() doesn't clean up listeners, causing EventEmitter memory leak warning
维护者通常 1 天内回复
@shhhhhhjokerist 已经在做这个了。
开始于 2026年11月8日。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 35/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 停滞
- 技术栈
- node.js, typescript
调研方向
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 内容生成。
描述
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:
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
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
- Configure any local (stdio) MCP server that produces stderr output
- Launch an MCP client (e.g., opencode TUI)
- 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
- Non-blocking, but indicates a potential memory leak over long sessions
- Affects all MCP clients that use the TypeScript SDK with local (stdio) servers
- Reported in opencode issue #58173: https://github.com/anomalyco/opencode/issues/58173
- 主要语言
- TypeScript
- 星标
- 13.8k
- 派生
- 2.5k
- 平均合并
- 6 天 23 小时
- 30 天内合并 PR
- 2
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
modelcontextprotocol/typescript-sdk 的其他 Issue
-
v2
难度 1/5 1 小时以内 新手友好度 72/100
modelcontextprotocol/typescript-sdk#3270 · 1 条评论 ·
维护者通常 1 天内回复
-
`callTool` with `allowInputRequired: true` throws on an `input_required` result when the tool declares an `outputSchema`可能已有人在做 @mgoldsborough 于 6 天前认领。 未关闭v2
难度 2/5 1-3 小时 新手友好度 78/100
modelcontextprotocol/typescript-sdk#3249 ·
维护者通常 1 天内回复
-
v2
难度 2/5 1-3 小时 新手友好度 68/100
modelcontextprotocol/typescript-sdk#3243 · 3 条评论 ·
维护者通常 1 天内回复
-
TypeScript client accepts an unknown `resultType` in the first `server/discover` reply可能已有人在做 @Tidehyc 于 7 天前认领。 未关闭v2
难度 2/5 1-3 小时 新手友好度 72/100
modelcontextprotocol/typescript-sdk#3235 · 3 条评论 ·
维护者通常 1 天内回复
-
UriTemplate.match() throws a TypeError on an empty expression it accepts可能已有人在做 @yunusemrejs 于 12 天前认领。 未关闭v1 v2
难度 2/5 1-3 小时 新手友好度 80/100
modelcontextprotocol/typescript-sdk#3221 · 3 条评论 ·
维护者通常 1 天内回复
查看 modelcontextprotocol/typescript-sdk 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 72/100
-
难度 2/5 1-3 小时 新手友好度 68/100
Vessel9817/onion-site-template#512 ·
维护者通常 1 天内回复
-
nightly-tests
难度 2/5 1-3 小时 新手友好度 68/100
BlackBeltTechnology/pi-agent-dashboard#1256 ·
维护者通常 1 天内回复
-
approved
难度 2/5 1-3 小时 新手友好度 85/100
fritzprix/libr-agent#3266 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 88/100
better-auth/better-auth#13101 · 1 条评论 · 1 个 reaction ·
维护者通常 1 天内回复