help icon indicating copy to clipboard operation
help copied to clipboard

Please help me to sort out the issue described below

Open mingzhangyang opened this issue 4 years ago • 11 comments

Details

(node:233037) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 error listeners added to [TLSSocket]. Use emitter.setMaxListeners() to increase limit
(Use `node --trace-warnings ...` to show where the warning was created)

Warning stack: MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 error listeners added to [TLSSocket]. Use emitter.setMaxListeners() to increase limit
    at _addListener (node:events:465:17)
    at TLSSocket.addListener (node:events:487:10)
    at TLSSocket.Readable.on (node:internal/streams/readable:876:35)
    at TLSSocket.socketListenerWrap [as on] (node:_http_server:1002:54)
    at TLSSocket.socketOnError (node:_http_server:666:8)
    at onParserExecuteCommon (node:_http_server:697:19)
    at onParserExecute (node:_http_server:640:3)

Node.js version

v16.13.2

Example code

No response

Operating system

CentOS Linux release 7.9.2009 (Core)

Scope

Runtime. The application can run correctly. But the warning message pops up one or two times a day.

Module and version

    "async": "^3.2.3",
    "body-parser": "^1.12.3",
    "bunyan": "^1.5.1",
    "cli-table": "^0.3.1",
    "connect-mongo": "^4.6.0",
    "cors": "^2.6.0",
    "ejs": "^3.1.6",
    "errorhandler": "^1.3.5",
    "express": "^4.14.0",
    "express-ipfilter": "1.2.0",
    "express-session": "^1.17.2",
    "https": "^1.0.0",
    "method-override": "^3.0.0",
    "morgan": "^1.10.0",
    "mssql": "^8.0.0",
    "passport": "^0.4.1",
    "passport-google-oauth": ">= 0.0.0",
    "socket.io": "^4.2.0"

mingzhangyang avatar Mar 07 '22 13:03 mingzhangyang

The warning appears when your listener count on the emitter exceeds the default maxListeners value (which is 10). This is done to help identify memory leaks. More on that here.

You are getting this warning as you have 11 listeners. If this is the expected number of listeners, you have nothing to worry about. To get rid of this warning, you can set emitter.setMaxListeners(n) to 12 or some number a bit higher than your expected number of listeners.

marsonya avatar Mar 21 '22 09:03 marsonya

Thanks @marsonya. Do you have an idea how to find the explicit line or function call that leads to the warning? The warning stack didn't provide any clues to whether my code causes it.

mingzhangyang avatar Mar 21 '22 15:03 mingzhangyang

Thanks @marsonya. Do you have an idea how to find the explicit line or function call that leads to the warning? The warning stack didn't provide any clues to whether my code causes it.

There won't be an exact line for this.

Your code is using emitter.on() 11 times. If this is intended then you need to set emitter.setMaxListeners(11) right after new EventEmitter(). That will get rid of the warning.

If 11 subscriptions is not intended, that means you have a memory leak somewhere in your code and you are subscribing to events multiple times. You need to find where you are subscribing and make changes there.

marsonya avatar Mar 21 '22 18:03 marsonya

Thank you @marsonya . But below is exactly the reason I posted the question here. I didn't use emitter.on() directly in my code. I used http and https in different environments. The warning seems only happen with https server. I am not sure the memory leak is due to my code or the internal package if possible.

If 11 subscriptions is not intended, that means you have a memory leak somewhere in your code and you are subscribing to events multiple times. You need to find where you are subscribing and make changes there.

mingzhangyang avatar Mar 22 '22 13:03 mingzhangyang

And another fact I can confirm is that the warning happens rarely. The normal business operations never induces it. The open ports on our server were routinely looked through and received fake requests for spotting security vulnerabilities. This warning only happens in this period but not always.

mingzhangyang avatar Mar 22 '22 13:03 mingzhangyang

And another fact I can confirm is that the warning happens rarely. The normal business operations never induces it. The open ports on our server were routinely looked through and received fake requests for spotting security vulnerabilities. This warning only happens in this period but not always.

I'd like to point out here that the warning is there for a reason and there's a good chance the right fix is not increasing the limit but figuring out why you're adding so many listeners to the same event. Only increase the limit if you know why so many listeners are being added and are confident it's what you really want.

marsonya avatar Apr 01 '22 17:04 marsonya

I'm facing the same issue and saw you are bunyan for logging. I have a background service that loops forever with a sleep timer, and after a while I get the same warning.

In my case I'm also using (besides debug stream) the package 'bunyan-seq' to log to a remote seq instance via bunyan. Just realized that when disabling the seq stream the warning disappears, so it seems in my case that this package is responsible for this. Maybe that helps.

chris-metz avatar Sep 06 '22 16:09 chris-metz

@chris-metz Hi Chris, thank you very much for the valued information. That's very helpful. Finally got a direction to expolore.

mingzhangyang avatar Sep 06 '22 16:09 mingzhangyang

No problem, also opened an issue for my specific case which is already resolved, in case it's your problem too:

https://github.com/datalust/bunyan-seq/issues/47

chris-metz avatar Sep 09 '22 08:09 chris-metz

It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.

github-actions[bot] avatar May 08 '24 01:05 github-actions[bot]

It seems there has been no activity on this issue for a while, and it is being closed. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.

github-actions[bot] avatar Jun 08 '24 01:06 github-actions[bot]