MaxListenersExceededWarning triggered by Claude Code's internal event listeners

Resolved 💬 3 comments Opened Jul 4, 2025 by ras434 Closed Jul 4, 2025

Description

Claude Code triggers Node.js MaxListenersExceededWarning even in minimal applications that use EventEmitters responsibly. The warning appears to be caused by Claude Code's own internal event listener management rather than user code.

Environment

  • Claude Code Version: 1.0.43
  • Node.js Version: v22.10.0
  • npm Version: 11.1.0
  • OS: macOS Darwin 24.5.0 (darwin platform)
  • Architecture: Apple Silicon (arm64)

Steps to Reproduce

  1. Create any Node.js application that uses EventEmitters
  2. Run it through Claude Code
  3. The MaxListenersExceededWarning appears even with minimal EventEmitter usage

Minimal Reproduction Script

const EventEmitter = require('events');
const emitter = new EventEmitter();

// Add exactly 11 listeners (1 more than default limit of 10)
for (let i = 0; i < 11; i++) {
    emitter.on('test', () => {});
}

Expected Behavior

  • User applications should only see MaxListenersExceededWarning when they actually exceed listener limits
  • Claude Code's internal event listeners should not interfere with user application warnings

Actual Behavior

(node:54193) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 test listeners added to [EventEmitter]. MaxListeners is 10. Use emitter.setMaxListeners() to increase limit
    at _addListener (node:events:601:17)
    at EventEmitter.addListener (node:events:619:10)
    at Object.<anonymous> (/Users/ras434/ClaudeCodeProjects/maintenance/test-listeners.js:6:13)

Root Cause Analysis

Through diagnostic testing, I've determined that:

  1. The warning is environmental: It only occurs when running under Claude Code
  2. Claude Code appears to register many internal event listeners: This causes the Node.js warning threshold to be reached even with minimal user code
  3. The warning is misleading: It suggests a memory leak in user code when the issue is actually in the Claude Code runtime environment

Impact

  • Developer Experience: Confusing warnings that don't reflect actual issues in user code
  • Debugging: Makes it harder to identify real EventEmitter memory leaks
  • False Positives: Developers may waste time trying to fix non-existent issues in their code

Suggested Solutions

  1. Increase Claude Code's internal listener limits: Use emitter.setMaxListeners(0) or a higher value for Claude Code's internal emitters
  2. Isolate internal listeners: Ensure Claude Code's event listeners don't count against user application limits
  3. Suppress warnings selectively: Only suppress warnings for Claude Code's internal emitters, not user code
  4. Document the behavior: If this is expected, document it so developers know to ignore these warnings when running under Claude Code

Diagnostic Script

I've attached a diagnostic script (maxlisteners-diagnostic.js) that can help reproduce and analyze the issue. This script:

  • Shows environment details
  • Creates minimal EventEmitter usage
  • Demonstrates that the warning appears even with responsible listener management
  • Shows process-wide listener counts

Additional Notes

  • The warning does not appear when running the same code directly with Node.js (outside Claude Code)
  • Using --no-warnings flag suppresses all warnings, which is not ideal as it hides legitimate issues
  • The issue affects any Node.js application that uses EventEmitters, making it a widespread concern

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗