Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

dns.lookup fails when offline on Windows #25338

Closed
seanpoulter opened this issue May 16, 2015 · 8 comments
Closed

dns.lookup fails when offline on Windows #25338

seanpoulter opened this issue May 16, 2015 · 8 comments

Comments

@seanpoulter
Copy link

From dns.js, dns.lookup for "localhost" fails, throwing an ENOENT error from getaddrinfo. It's reproducible with the following block:

var dns = require("dns");
dns.lookup("localhost", {"hints":3072}, function(err, ip, addressType) {
  console.log(err, ip, addressType);
})

Running it when connected produces the output:

null '127.0.0.1' 4

but without a network connection:

{ [Error: getaddrinfo ENOENT]
  code: 'ENOENT',
  errno: 'ENOENT',
  syscall: 'getaddrinfo',
  hostname: 'localhost' } undefined undefined

Unfortunately debugging even this simple block with node debug <file> fails trying to connect to port 5858:

>node debug troubleshoot-dns.js
< Debugger listening on port 5858
connecting to port 5858.............There was an internal error in Node's debugger. Please report this bug.
getaddrinfo ENOENT
Error: getaddrinfo ENOENT
    at Object.exports._errnoException (util.js:746:11)
    at errnoException (dns.js:49:15)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:94:26)

One solution is to patch dns.js to replace "localhost" with "127.0.0.1" which produces the expected output, but I think the better solution is to fix the getaddrinfo calls.

@dnakamura
Copy link

which node version are you running?

@seanpoulter
Copy link
Author

Running v0.12.2 x64.

@dnakamura
Copy link

First off you should really be using the constants, ie:

var dns = require("dns");
dns.lookup("localhost", {"hints":dns.ADDRCONFIG | dns.V4MAPPED}, function(err, ip, addressType) {
  console.log(err, ip, addressType);
})

Secondly the behaviour you describe is expected. From the documentation:

dns.ADDRCONFIG: Returned address types are determined by the types of addresses supported by the current system. For example, IPv4 addresses are only returned if the current system has at least one IPv4 address configured. Loopback addresses are not considered.

@seanpoulter
Copy link
Author

Thanks for piecing that together and correcting the example code @dnakamura.

Since the API confirms this expected behaviour, I guess we should close this issue and open one against Socket.prototype.connect for improperly handling the ENOENT error thrown when performing the dns.lookup on localhost. Sound good?

@dnakamura
Copy link

yeah

@jasnell
Copy link
Member

jasnell commented Jun 5, 2015

@seanpoulter @dnakamura ... did the other issue get opened? Closing this but want to make sure the other issue is not dropped.

@seanpoulter
Copy link
Author

Thanks @jasnell. I hadn't opened the other yet.

Refer to: #25489

@rameshprasad
Copy link

I fixed this by modifying below line in file: \node_modules\mysql\lib\ConnectionConfig.js
this.host = options.host || 'localhost';
as
this.host = options.host || '127.0.0.1';

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants