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

Aperta
#3,070 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@shhhhhhjokerist ci sta già lavorando.

Dal 8/11/2026.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
node.js, typescript
Ambito
backend, devtools

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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
Lingua principale
TypeScript
Stelle
13.8k
Fork
2.5k
Merge medio
6g 23h
PR unite (30g)
2

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di modelcontextprotocol/typescript-sdk

Tutte le issue di modelcontextprotocol/typescript-sdk

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.