9

I'm using node.js, and using the cluster module. Everytime I run cluster.fork(), I always get a

throw er; // Unhandled 'error' event
Error: bind EADDRINUSE
    at exports._errnoException (util.js:746:11)
at cb (net.js:1205:33)
at rr (cluster.js:592:14)
at Worker.<anonymous> (cluster.js:563:9)
at process.<anonymous> (cluster.js:692:8)
at process.emit (events.js:129:20)
at handleMessage (child_process.js:324:10)
at Pipe.channel.onread (child_process.js:352:11)

I've been googling this, and I have no idea how this is happening because I'm not passing in any port numbers.

Thanks

EDIT: Posting code

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

 }

and this is a function that is called in the app.js which I run by calling node app.js

Gakho
  • 603
  • 1
  • 9
  • 18

2 Answers2

2

I was starting a server more than once with all the threads so the port was bound already

Gakho
  • 603
  • 1
  • 9
  • 18
0

The stack trace you provide indicates that EADDRINUSE is coming from the net module. EADDRINUSE typically means that you are trying to listen on an IP/port combination more than once. So, for example, if this is a clustered web server, perhaps all your workers are trying to bind to port 80 on the same IP address. Without more code, it's impossible to tell what's happening.

The example code you gave in the subsequent comment does not trigger EADDRINUSE for me. Instead it errors with cluster.fork is not a function because there's no check for cluster.isMaster before calling cluster.fork().

Trott
  • 66,479
  • 23
  • 173
  • 212