diff --git a/lib/wait-port.js b/lib/wait-port.js index 4c477ac..6bee464 100644 --- a/lib/wait-port.js +++ b/lib/wait-port.js @@ -103,6 +103,15 @@ function tryConnect(options, timeout) { debug('Socket not open: ECONNRESET'); socket.destroy(); return resolve(false); + } else if (options.ipVersion === 6 && (err.code === 'EADDRNOTAVAIL' || err.code === 'ENOTFOUND')) { + // This will occur if the IP address we are trying to connect to does not exist + // This can happen for ::1 or other IPv6 addresses if the IPv6 stack is not enabled. + // In this case we disable the IPv6 lookup + debug(`Socket cannot be opened for IPv6: ${err.code}`); + debug('Disabling IPv6 lookup'); + IPv6enabled = false; + socket.destroy(); + return resolve(false); } else if (err.code === 'ENOTFOUND') { // This will occur if the address is not found, i.e. due to a dns // lookup fail (normally a problem if the domain is wrong). @@ -116,21 +125,19 @@ function tryConnect(options, timeout) { // ...otherwise, we will explicitly fail with a meaningful error for // the user. return reject(new ConnectionError(`The address '${options.host}' cannot be found`)); - } else if (err.code === 'EADDRNOTAVAIL' && options.ipVersion === 6) { - // This will occur if the IP address we are trying to connect to does not exist - // This can happen for ::1 or other IPv6 addresses if the IPv6 stack is not enabled. - // In this case we disable the IPv6 lookup - debug('Socket cannot be opened for IPv6: EADDRNOTAVAIL'); - debug('Disabling IPv6 lookup'); - IPv6enabled = false; - - return resolve(false); } // Trying to open the socket has resulted in an error we don't // understand. Better give up. debug(`Unexpected error trying to open socket: ${err}`); socket.destroy(); + + // If we are currently checking for IPv6 we ignore this error and disable IPv6 + if (options.ipVersion === 6) { + IPv6enabled = false; + return resolve(false); + } + return reject(err); }