Introduction

Possible EventEmitter memory leak detected warnings appear when the same event keeps collecting listeners faster than they are removed. The warning often signals duplicate subscriptions, not just a low max listener limit.

Symptoms

  • Node prints MaxListenersExceededWarning in logs
  • Memory grows over time as requests or jobs run
  • The same handler seems to run more than once for one event

Common Causes

  • Listener registration happens inside loops or repeated request paths
  • Cleanup code never calls off or removeListener
  • Reconnect logic adds fresh listeners every retry

Step-by-Step Fix

  1. 1.Trace where listeners are being added and count them before and after the hot path.
  2. 2.Move listener registration out of loops and request handlers.
  3. 3.Remove listeners during shutdown, unmount, or reconnect cleanup.
  4. 4.Increase max listeners only after the growth pattern is understood.

Prevention

  • Keep listener lifecycle close to the code that owns the resource
  • Log listener counts in long-running workers and reconnect loops
  • Treat setMaxListeners as a capacity setting, not a leak fix