WebThingsIO/webthing-node

MaxListenersExceededWarning: Possible EventEmitter memory leak detected

Closed this issue · 4 comments

Hi,

on Windows 10 and Node v13.9.0 I do get regularly the following warning and can not close the app by "ctrl+c". Not yet tested on Linux.

The warning occurs mostly when one Websocket client is connected to just one Thing. Without any Websocket connected I can close the app by "ctrl+c".

(node:13352) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 close listeners added to [Server]. Use emitter.setMaxListeners() to increase limit
    at _addListener (events.js:396:17)
    at Server.addListener (events.js:412:10)
    at Server.once (events.js:443:8)
    at Server.close (net.js:1603:12)
    at C:\apps\webiot\node_modules\webthing\lib\server.js:862:19
    at new Promise (<anonymous>)
    at WebThingServer.stop (C:\apps\webiot\node_modules\webthing\lib\server.js:861:19)
    at process.<anonymous> (C:\apps\webiot\index.js:262:16)
    at process.emit (events.js:321:20)

Setting manually the max listener as follows results in the same error:

const server = new WebThingServer(new MultipleThings(things, "mthings"), 8888);
server.server.setMaxListeners(20);
(node:10676) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 21 close listeners added to [Server]. Use emitter.setMaxListeners() to increase limit
    at _addListener (events.js:396:17)
    at Server.addListener (events.js:412:10)
    at Server.once (events.js:443:8)
    at Server.close (net.js:1603:12)
    at C:\apps\webiot\node_modules\webthing\lib\server.js:862:19
    at new Promise (<anonymous>)
    at WebThingServer.stop (C:\apps\webiot\node_modules\webthing\lib\server.js:861:19)
    at process.<anonymous> (C:\apps\webiot\index.js:262:16)
    at process.emit (events.js:321:20)

Any ideas how to close the app in Windows wihout any problems?

In our examples, we use this pattern to handle ctrl+c:

process.on('SIGINT', () => {
  server.stop().then(() => process.exit()).catch(() => process.exit());
});

I'm surprised you're seeing that warning, though. Are you sure there's only one thing on your network connecting to it? For instance, any gateways will automatically connect to it.

Hi @mrstegeman ,

I'm using the process termination from your example; so exactly as you've written here. Of course I've modified the example scripts. But the error still happens with the exact same code from your example directory for MultipleThings. I've just created a repo with your exmaple code for webthing and added a websocket client which connect to the "Thing ID=1": https://github.com/mbecker/webthing-example

Have you ever connected a websocket to a Thing? Only then the error happens.

This resolves the issue by adding a timeout:

process.on('SIGINT', () => {
  Promise.race([
    server.stop(),
    new Promise((res) => setTimeout(res, 2000)),
  ]).finally(() => process.exit());
});

Strangely, I only see the issue when connecting from the browser. If I connect to the webthing from my gateway (e.g. Node.js), I'm able to shut down cleanly.

Either way, I don't see the MaxListenersExceededWarning.

Hi,

seems to work! Thanks!