Decache A Module That Adds Event Listeners
It seems there is an issue when decaching a module that listens to events. I have a logging module that is used from everywhere. It leverages winston, which adds event listeners so that it can log for uncaughtException. However, when I decache a module and load it again for testing, the logging module is also reloaded, which re-adds all the winston loggers, which adds all the event listeners again. However, as far as process is concerned these listeners are already bound, so they just accumulate and I get the warning: (node) warning: possible EventEmitter memory leak detected. 11 uncaughtException listeners added. Use emitter.setMaxListeners() to increase limit.
I wonder what the correct process is for handling this case. Could it be that a function is called just before decching that would allow some custom code to unregister these listeners (winston.clear())?
Superb use-case addition! do you have time to create a Pull Request?
I tried figuring out a clean way to do this, but was unable. Instead I had to change the way my code is structured to not initialize the winston loggers when the module is loaded, but, instead, to do so when an init function is called.
I seem to be having a similar issue with accumulated listeners. In my case I am attaching the listener via process.once(). The event is emitted immediately prior to calling decache() on the module (which is subsequently re-required). So the hope is that the listener would be unregistered and called prior to re-requiring the module. But I am seeing behavior that is consistent with the issue description.