Version: v6.11.2
Platform: Linux workstation 4.4.0-92-generic #115-Ubuntu SMP Thu Aug 10 09:04:33 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
Subsystem: net
const net = require('net');
function createServer(port) {
var server = net.createServer();
server.on('connection', (socket) => {
console.log('Server '+port+': socket connected', socket.remotePort);
});
server.listen(port);
}
createServer(6001);
createServer(6002);
var client = new net.Socket();
client.connect({
port: 6001,
address: '::ffff:127.0.0.1',
localPort: 6002
});
Output: Server 6001: socket connected 35372
Expected: Server 6001: socket connected 6002 (or EADDRINUSE error)
I can reproduce this with master. I think this is similar to what https://github.com/nodejs/node/blob/2154a3ce0f2eca1d26e1ad8e7bbeae1039822a5a/lib/net.js#L1383-L1390 tries to address. I will open a PR fixing this shortly (the fix is causing other tests to fail because they are depending on this bug...).
After the fix, will it throw EADDRINUSE or use the port 6002 (in above example code) ?
@beingmohit
It will throw EADDRINUSE when the localAddress option of net.connect(options, cb) is specified as the same as the host passed to listen(port, host, cb). It will use the port 6002 if neither EDIT: oops, the default host nor localAddress is specified because libuv sets SO_REUSEADDR on TCP sockets, so we will be connecting from localhost(default of socket.connect) to the unspecified address, which is allowed. localAddress of socket.connect is also the unspecified address...so it will throw if neither is specified. If only one of them is unspecified, SO_REUSEADDR would allow the client to bind to that port.
So in the example above, it will use 6002 if I set localAddress to 127.0.0.1 (I don't use the IPv6-prefixed version because my current ISP does not support IPv6, although I think it should work either way. Note that address is not a valid option to socket.connect(), I guess what you are trying to set is localAddress?). Also the snippet does not close the servers so running it again after killing it will probably result in EADDRINUSE if those sockets are still in TIME_WAIT state.
Also I am getting consistent errors with these tests with master after running the tests with my patch...I am pretty sure this is caused by https://github.com/nodejs/node/pull/14781 , still investigating..
See errors
=== release test-http-client-req-error-dont-double-fire ===
Path: parallel/test-http-client-req-error-dont-double-fire
Mismatched <anonymous> function calls. Expected exactly 1, actual 0.
at Object.exports.mustCall (/Users/joyee/projects/node/test/common/index.js:477:10)
at Object.<anonymous> (/Users/joyee/projects/node/test/parallel/test-http-client-req-error-dont-double-fire.js:10:24)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
at tryModuleLoad (module.js:470:12)
at Function.Module._load (module.js:462:3)
at Function.Module.runMain (module.js:609:10)
at startup (bootstrap_node.js:202:16)
Mismatched <anonymous> function calls. Expected exactly 1, actual 0.
at Object.exports.mustCall (/Users/joyee/projects/node/test/common/index.js:477:10)
at Object.<anonymous> (/Users/joyee/projects/node/test/parallel/test-http-client-req-error-dont-double-fire.js:14:40)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
at tryModuleLoad (module.js:470:12)
at Function.Module._load (module.js:462:3)
at Function.Module.runMain (module.js:609:10)
at startup (bootstrap_node.js:202:16)
Command: out/Release/node /Users/joyee/projects/node/test/parallel/test-http-client-req-error-dont-double-fire.js
=== release test-net-better-error-messages-port-hostname ===
Path: parallel/test-net-better-error-messages-port-hostname
assert.js:41
throw new errors.AssertionError({
^
AssertionError [ERR_ASSERTION]: 'EADDRNOTAVAIL' === 'ENOTFOUND'
at Socket.<anonymous> (/Users/joyee/projects/node/test/parallel/test-net-better-error-messages-port-hostname.js:12:10)
at Socket.<anonymous> (/Users/joyee/projects/node/test/common/index.js:509:15)
at emitOne (events.js:115:13)
at Socket.emit (events.js:210:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
Command: out/Release/node /Users/joyee/projects/node/test/parallel/test-net-better-error-messages-port-hostname.js
=== release test-net-connect-immediate-finish ===
Path: parallel/test-net-connect-immediate-finish
assert.js:41
throw new errors.AssertionError({
^
AssertionError [ERR_ASSERTION]: 'ETIMEDOUT' === 'ENOTFOUND'
at Socket.client.once.common.mustCall (/Users/joyee/projects/node/test/parallel/test-net-connect-immediate-finish.js:35:10)
at Socket.<anonymous> (/Users/joyee/projects/node/test/common/index.js:509:15)
at Object.onceWrapper (events.js:316:30)
at emitOne (events.js:115:13)
at Socket.emit (events.js:210:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
Command: out/Release/node /Users/joyee/projects/node/test/parallel/test-net-connect-immediate-finish.js
[02:00|% 100|+ 1805|- 3]: Done
make: *** [test] Error 1
EDIT: uh, I am stuck with a DNS-hijacking ISP again and that's what has been failing my tests :/
Most helpful comment
@beingmohit
It will throw EADDRINUSE when the
localAddressoption ofnet.connect(options, cb)is specified as the same as thehostpassed tolisten(port, host, cb).It will use the port 6002 if neitherEDIT: oops, the defaulthostnorlocalAddressis specified because libuv setsSO_REUSEADDRon TCP sockets, so we will be connecting fromlocalhost(default ofsocket.connect) to the unspecified address, which is allowed.localAddressofsocket.connectis also the unspecified address...so it will throw if neither is specified. If only one of them is unspecified,SO_REUSEADDRwould allow the client to bind to that port.So in the example above, it will use 6002 if I set
localAddressto127.0.0.1(I don't use the IPv6-prefixed version because my current ISP does not support IPv6, although I think it should work either way. Note thataddressis not a valid option tosocket.connect(), I guess what you are trying to set islocalAddress?). Also the snippet does not close the servers so running it again after killing it will probably result inEADDRINUSEif those sockets are still inTIME_WAITstate.Also I am getting consistent errors with these tests with master after running the tests with my patch...I am pretty sure this is caused by https://github.com/nodejs/node/pull/14781 , still investigating..
See errors
EDIT: uh, I am stuck with a DNS-hijacking ISP again and that's what has been failing my tests :/