question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reconnect feature cause an EventEmitter memory leak

See original GitHub issue

When used with connections pools, this module can cause an EventEmitter memory leak:

(node:9472) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 error listeners added to [PoolConnection]. Use emitter.setMaxListeners() to increase limit
MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 error listeners added to [PoolConnection]. Use emitter.setMaxListeners() to increase limit
    at _addListener (events.js:261:17)
    at PoolConnection.addListener (events.js:277:10)
    at PoolConnection.once (events.js:306:8)
    at addReconnectHandler (/.../node_modules/promise-mysql/lib/connection.js:138:16)
    at /.../node_modules/promise-mysql/lib/connection.js:41:17
    at tryCatcher (/.../node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/.../node_modules/bluebird/js/release/promise.js:517:31)
    at Promise._settlePromise (/.../node_modules/bluebird/js/release/promise.js:574:18)
    at Promise._settlePromiseCtx (/.../node_modules/bluebird/js/release/promise.js:611:10)
    at _drainQueueStep (/.../node_modules/bluebird/js/release/async.js:142:12)
    at _drainQueue (/.../node_modules/bluebird/js/release/async.js:131:9)
    at Async._drainQueues (/.../node_modules/bluebird/js/release/async.js:147:5)
    at Immediate.Async.drainQueues (/.../node_modules/bluebird/js/release/async.js:17:14)
    at processImmediate (internal/timers.js:439:21)
    at process.topLevelDomainCallback (domain.js:126:23)

Pool.getConnection create a new PoolConnection which call addReconnectHandler and add an error listener. But this listener is never removed and another will be added on the next Pool.getConnection call.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
nathan818frcommented, Sep 20, 2019

Reconnect is a base feature of any pools system. Without reconnection a pool will be fundamentally broken. As you can see here, when acquiring a connection, if the connection is closed (or encounter any error):

  • the acquirer is pushed back to the connection queue (and it will acquire a new connection - either another already open connection or a new one that will be created)
  • and the broken connection is purged

Also note that when an existing connection is retrieved, a ping is made to check if the connection is still good.

In addition, currently, the reconnect feature of this module does not work with pools (so the fix will not even be a breaking change!). Explanation:

1reaction
nathan818frcommented, Aug 20, 2019

Anyway the reconnect feature does not make any sense with connections Pools. They already have an reconnect feature built internally.

The best fix seem to add reconnect: false here: https://github.com/lukeb-uk/node-promise-mysql/blob/f2e72814d40146f3025b04c4b78d606c6aec8be2/lib/pool.js#L42:L46

Read more comments on GitHub >

github_iconTop Results From Across the Web

possible EventEmitter memory leak detected - node.js
I'd like to point out here that that warning is there for a reason and there's a good chance the right fix is...
Read more >
Node.js sends warnings when you add too many listeners to ...
Node.js gives friendly warnings in case you add too much event listeners to an event emitter.
Read more >
[NODE-1270] Multiple databases causing EventEmitter warnings
Using more than 9 databases with one client (with a registered listener) causes MaxListenersExceededWarning: Possible EventEmitter memory leak ...
Read more >
what is this "Possible EventEmitter memory leak detect ed ...
Every time you start a function that does an event connection, the same event gets connected over and over. You have to disconnect...
Read more >
How to fix possible EventEmitter memory leak detected - cri.dev
The warning possible EventEmitter memory leak detected happens when you have more than 10 listeners (read EventEmitter) attached to an event ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found