Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MaxListenersExceededWarning: Possible EventEmitter memory leak detected #27

Open
achingbrain opened this issue Dec 13, 2021 · 1 comment

Comments

@achingbrain
Copy link
Contributor

achingbrain commented Dec 13, 2021

After about 10 hours running I start seeing MaxListenersExceededWarning:

node:internal/process/warning:50 (node:81670) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 _device listeners added to [Ssdp]. Use emitter.setMaxListeners() to increase limit
    at _addListener (node:events:465:17)
    at Ssdp.addListener (node:events:487:10)
    at Ssdp.search (/Users/alex/Documents/Workspaces/ipfs/js-ipfs/node_modules/nat-api/lib/upnp/ssdp.js:84:10)
    at Client.findGateway (/Users/alex/Documents/Workspaces/ipfs/js-ipfs/node_modules/nat-api/lib/upnp/index.js:186:25)
    at Client.portMapping (/Users/alex/Documents/Workspaces/ipfs/js-ipfs/node_modules/nat-api/lib/upnp/index.js:23:10)
    at NatAPI._upnpMap (/Users/alex/Documents/Workspaces/ipfs/js-ipfs/node_modules/nat-api/index.js:315:22)
    at listOnTimeout (node:internal/timers:557:17)
    at processTimers (node:internal/timers:500:7)
@cascandaliato
Copy link

I believe the problem lies in this method:

_upnpMap (opts, cb) {

Specifically, here:

nat-api/index.js

Lines 327 to 332 in e404453

if (self.autoUpdate) {
self._upnpIntervals[opts.publicPort + ':' + opts.privatePort + '-' + opts.protocol] = setInterval(
self._upnpMap.bind(self, opts, () => {}),
self._timeout
)
}

When autoupdate is enabled (default behavior), _upnpMap gets called at regular intervals and, each time it executes, it adds an additional timer. This means that, over time, you get more and more _upnpMap calls firing at the same time, until you reach a point where the underlying code registers too many listeners in a short amount of time.

Partially related: I feel like the same could happen if you map too many ports with the same TTL. You would have a spike in the number of listeners, except that in this case there is no leak because listeners are correctly unregistered.

@alxhotel, any chance you can have a look at my analysis? I'll be happy to create a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants