elastic / elasticsearch-js

Official Elasticsearch client library for Node.js
https://ela.st/js-client
Apache License 2.0
5.23k stars 727 forks source link

MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 product-check listeners added #2224

Open dherbolt opened 4 months ago

dherbolt commented 4 months ago

Please read this entire template before posting any issue. If you ignore these instructions and post an issue here that does not follow the instructions, your issue might be closed, locked, and assigned the not reproducible label.

🐛 Bug Report

The very first request executes the product check procedure. When new requests comes during this check the new product-check event listeners are added.

https://github.com/elastic/elasticsearch-js/blob/7.17/lib/Transport.js#L476

The nodeJS warning is then reported:

(node:5099) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 product-check listeners added to [EventEmitter]. Use emitter.setMaxListeners() to increase limit
    at _addListener (node:events:587:17)
    at EventEmitter.addListener (node:events:605:10)
    at EventEmitter.once (node:events:649:8)
    at Transport.request (/Users/daniel.herbolt/test/node_modules/es7/lib/Transport.js:476:29)
    at Client.searchApi [as search] (/Users/daniel.herbolt/test/node_modules/es7/api/api/search.js:61:25)
    at /Users/daniel.herbolt/test/event-listener.js:14:18

To Reproduce

Steps to reproduce the behavior: 1) save the code below into file event-listener.js 1) run code node --trace-warnings ./event-listener.js

Paste your code here:

const { Client: ES7Client } = require('es7')

const elasticConn = 'http://elastic:changeme@localhost:9201'
const ES_INDEX = 'test-index'

const client7 = new ES7Client({
    nodes: elasticConn.split(','),
    suggestCompression: true,
})

const cnt = 50
async function run() {
    const res = await Promise.all(Array.from({length: cnt}, async (_) => {
        return client7.search({
            index: ES_INDEX,
            body: {size: 0},
            ignore_unavailable: true,
        })
    }))
    return res
}

run().then(async () => {
    console.log('Done.')
}).catch(async (e) => {
    console.error(e)
})

Expected behavior

Only the very first request attach the 'product-check' event listener. The others requests are queued and executed from the event handler for the first request.

Paste the results here:

Your Environment

JoshMock commented 4 months ago

Looks similar to https://github.com/elastic/elastic-transport-js/issues/63, which is a long-standing issue now, but that is specific to the 8.x client and its use of Undici. Will keep this open and track separately.

In the short term, if this issue is not causing notable memory leak issues for you, you can increase max listeners globally to quiet the warnings.

sibelius commented 4 months ago

when a new release will come up?

tomimarkus991 commented 3 months ago

when a new release will come up?

+1