got: MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 error listeners added to [Keyv]. Use emitter.setMaxListeners() to increase limit

Describe the bug

When using simple cache adapters MaxListenersExceededWarning is emitted. This has previously been investigated in #792 and #1128 but either it is a regression or did not account for highly asynchronous environments with 100’s of parallel requests.

Unsure if this is best opened in either keyv or cacheable-request, but given it was declared solved in #1128 I’ve opened it here first.

Environment Info:
  System:
    OS: macOS 10.15.7
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  Binaries:
    Node: 12.19.0 - ~/.nvm/versions/node/v12.19.0/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 6.14.8 - ~/.nvm/versions/node/v12.19.0/bin/npm
  Browsers:
    Chrome: 86.0.4240.198
    Safari: 14.0

Got: 11.8.0

Actual behavior

MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 error listeners added to [Keyv]. Use emitter.setMaxListeners() to increase limit

Code to reproduce

const got = require('got');

const client = got.extend({
    cache: {
        get: (key) => {
            return undefined;
        },
        set: (key, value) => {

        }
    }
});

(async () => {
    const promises = [];

    for (let i = 0; i < 20; i++) {
        promises.push(client('https://www.google.com', { method: 'HEAD' }));
    };

    await Promise.all(promises);

    console.log('Done');
})();

Checklist

  • I have read the documentation.
  • I have tried my code with the latest version of Node.js and Got.

Related Tickets

https://github.com/lukechilds/cacheable-request/issues/97

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 4
  • Comments: 21

Most upvoted comments

I’ve opened a PR about this at cacheable-request

Looks good. I’ll look deeper this week to look out for bugs if any.

@szmarczak are you also a maintainer of that lib?

Yes. I’m going to rewrite cacheable-request completely but first we’re releasing Got 12 soon.

It’s because it creates another Keyv instance:

I’m facing issues for similar reasons, it’s also registering a once error listener that gets cleared after response, which is okay-ish but it doesn’t account for databases connection error (eg: replicas failover) which crashes the process and prevent network fallback.

I think Got could also benefit from this, I’ve opened a PR about this at cacheable-request, @szmarczak are you also a maintainer of that lib? WDYT?

Any update on it ?

Hello, I got the same issue Using native JS Map() object. What can I do ? I am using got “got”: “^11.8.0”


const cache = new Map();
export const https = new HttpsProxyAgent({
  keepAlive: true,
  maxSockets: 256,
  proxy: 'http://plugin-gateway.platform:8081',
});

const product: IProduct = await got.get(`${apiUrl}/product/${productId}`, {
      retry: 2,
      cache: cache,
      agent: { https },
      headers: {
        Authorization: apiPassword,
      },
    }).json();

Error : (node:1) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 error listeners added to [Keyv]. Use emitter.setMaxListeners() to increase limit