Skip to content

(v6.x backport) dns: fix `resolve` failed starts without network

Refs: https://github.com/nodejs/node/issues/13076

Fix the bug that you start process without network at first, but it connected lately, dns.resolve will stay failed with ECONNREFUSED because c-ares servers fallback to 127.0.0.1 at the very beginning.

If c-ares servers "127.0.0.1" is detected and its not set by user self, and last query is not OK, recreating ares_channel operation will be triggered to reload servers.

You can test with this script:

const dns = require("dns");

function test() {
    dns.resolve("google.com", function(err, ret) {
        console.log(err, ret, dns.getServers());
        setTimeout(test, 10000);
    });
}

test();

Turn off your network at first before starting this script. Then try to open the network and then turn off. Make the steps above in a loop and you will get the result.

Fixes: https://github.com/nodejs/node/issues/1644

refack: added ref to original PR

Checklist
Affected core subsystem(s)

dns

Merge request reports

Loading