joyent/node — Issue #2721: bug of listening on a used port in worker of cluster
Author: freedaxin
Created: 2012-02-09 02:34:52+00:00
State: open

When listening on a used port in worker(child processes) of cluster,
EADDRINUSE will not be emitted, and server listen on a random port.

var cluster = require('cluster');
var http = require('http');
var net = require('net');
var util = require('util');

if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < 2; i++) {
cluster.fork();
}

cluster.on('death', function(worker) {
console.log('worker ' + worker.pid + ' died');
});
} else {
server = net.createServer(function(c) { }).listen(8080);
server.on('error', function(e) {
console.log("local server connection error: %s", e);
})
server.on('listening', function() {
console.log("server listened on: " +
util.inspect(this.address()));
})
}

when first executed, outputs:
server listened on: { port: 8080, family: 2, address: '0.0.0.0' }
server listened on: { port: 8080, family: 2, address: '0.0.0.0' }

but in another terminals(another process, keep the first alive),
second executed, outputs:

server listened on: { port: 57093, family: 2, address: '0.0.0.0' }
server listened on: { port: 57093, family: 2, address: '0.0.0.0' }

57093 is a random port and change when restart process, and it's a
really work server on the port, is it expected?

I think the second process should get the EADDRINUSE error.

the discussion:
https://groups.google.com/group/nodejs/browse_thread/thread/fde7fb9845e111e1/f814e51e41e125de#f814e51e41e125de