AgentFixboard

stdio client close leaves drain listeners and warns MaxListenersExceededWarning

Local stdio MCP servers that write to stderr produce MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 drain listeners added to [WriteStream]. MaxListeners is 10.

From GitHub

modelcontextprotocol/typescript-sdk

Difficulty
Easy
Time
~5 min
Steps
2
Fixed in
—
Last checked

Not fixed upstream yet: the issue is open. Source: issue open. Last checked 2026-10-23.

Symptom

Local stdio MCP servers that write to stderr produce MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 drain listeners added to [WriteStream]. MaxListeners is 10.

Evidence what you'll see

  • StdioClientTransport.start() attaches listeners on stdin error, stdout data and error, and this._process.stderr.pipe(this._stderrStream), which adds a drain listener.
  • close() kills the process and does not unpipe or removeAllListeners.
  • Minimal servers that produce no stderr do not trigger the warning. Servers that log to stderr do. The same warning is recorded on anomalyco/opencode issue #58173.

Why it happens

pipe() adds a drain listener for back-pressure. After the child is killed that listener is left behind, so later stdio sessions accumulate past the default max of 10.

Affected versions

TypeScript SDK stdio client. The linked issue does not name a package version. Reproduced from a client that starts local stdio servers; issue #780 reports the same leftover listeners from a JSON parse error after close.

Fix steps

0 of 2 done

Your check marks stay in this browser only.

How to check it worked

  • After opening and closing several local stdio servers that write to stderr, Node no longer prints MaxListenersExceededWarning for drain listeners on WriteStream.

If it still fails

  • Raising setMaxListeners only hides the warning. The listeners still accumulate.
  • The linked issue does not give a one-off command that clears already-attached drain listeners besides restarting the Node process.

Sources and credit

Summarized in our own words from these public reports. Credit goes to their authors.

Provided as is, without warranty. Try changes in a non-production environment and back up first. Security-related steps: verify against the official docs. Terms · Request removal or attribution change

Hit an error we don't cover? Submit it