szmarczak / cacheable-lookup

A cacheable dns.lookup(…) that respects TTL :tada:
MIT License
193 stars 29 forks source link

Memory leak #32

Closed szmarczak closed 4 years ago

szmarczak commented 4 years ago
  1. Install dnsmasq

  2. Add address=/.test/127.0.0.1 to /etc/dnsmasq.conf

  3. sudo /usr/bin/dnsmasq (start the server)

  4. Run (cacheable-lookup@5.0.2):

const CacheableLookup = require('cacheable-lookup');
const cacheable = new CacheableLookup();

cacheable.servers = ['127.0.0.1'];

setInterval(() => {
    cacheable.lookup(`${Math.random().toString().substr(2)}.test`, () => {});
}, 1);

The memory usage slowly increases every minute. It takes about 10-15 minutes to crash. To cause an instant crash, replace setInterval with while (true).

szmarczak commented 4 years ago

The native Node.js works as expected, no memory leaks whatsoever:

const {Resolver} = require('dns').promises;

const resolver = new Resolver();

resolver.setServers([
    '127.0.0.1'
]);

const withTtl = {ttl: true};

let pending = 0;

const resolve4 = async hostname => {
    pending++;

    const result = await resolver.resolve4(hostname, withTtl);

    pending--;

    return result;
};

setInterval(() => {
    resolve4(`${Math.random().toString().substr(2)}.test`);
}, 1);

setInterval(() => {
    console.log(pending);
}, 1000);
szmarczak commented 4 years ago

For instance this has no memory leak: https://github.com/szmarczak/cacheable-lookup/compare/no-memory-leak?expand=1

szmarczak commented 4 years ago

Weird. As soon as I enable dns.lookup(...) the memory leak appears...

szmarczak commented 4 years ago

Fixed.

molly-hayes commented 3 years ago

Fixed.

what fixed it?

szmarczak commented 3 years ago

@molly-hayes ae7c02ff7dae76e06cce607a02ad98b5afc60691