bug of listening on a used port in worker of cluster ?

879 views
Skip to first unread message

王 逍

unread,
Feb 6, 2012, 11:07:51 PM2/6/12
to nodejs
When listen on a used port in worker(child processes) of cluster,
EADDRINUSE will not be emitted, and server listen on a random port.
But in master, it emmits EADDRINUSE error, which is expected. Is it a
bug in child process?

Ben Noordhuis

unread,
Feb 7, 2012, 7:06:53 AM2/7/12
to nod...@googlegroups.com

I'm not quite sure what you mean. The cluster module shares a bound
socket between the master and its workers. If you get EADDRINUSE in
the master, then there is no socket to share with the workers.

Shinuza

unread,
Feb 7, 2012, 9:49:20 AM2/7/12
to nod...@googlegroups.com
Not sure if I get this either.

But from my understanding: if you run the Cluster example multiple times (i.e in two different terminals) the listening port will be shared across all of the workers.
If you have 4 "cpus", using this example, you'd get 8 ( 2 x 4 ) workers and 2 masters. 10 processes. If I get it right, all of the workers will listen on the same port.

If this is the excepted behavior then there is no reason to emit a EADDRINUSE.

PS:  @王 逍 when you said "master" did you mean the github repo?

王 逍

unread,
Feb 7, 2012, 11:14:27 PM2/7/12
to nodejs
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.

thanks!

On 2月7日, 下午10时49分, Shinuza <samorigo...@gmail.com> wrote:
> Not sure if I get this either.
>
> But from my understanding: if you run the Cluster example<http://nodejs.org/docs/latest/api/cluster.html#cluster>multiple times (i.e in two different terminals) the listening port will be

王 逍

unread,
Feb 7, 2012, 11:22:47 PM2/7/12
to nodejs
And if the port is used by other non node processes, listen in child
process will success on random port too.

On 2月8日, 下午12时14分, 王 逍 <freeda...@gmail.com> wrote:
> varcluster= require('cluster');
> > But from my understanding: if you run theClusterexample<http://nodejs.org/docs/latest/api/cluster.html#cluster>multiple times (i.e in two different terminals) the listening port will be

Ben Noordhuis

unread,
Feb 8, 2012, 7:45:58 AM2/8/12
to nod...@googlegroups.com

Right. Yes, that's a bug. Can you open an issue?

王 逍

unread,
Feb 8, 2012, 9:35:45 PM2/8/12
to nodejs
https://github.com/joyent/node/issues/2721

Thanks!

On 2月8日, 下午8时45分, Ben Noordhuis <i...@bnoordhuis.nl> wrote:
Reply all
Reply to author
Forward
0 new messages