Everytime I run cluster.fork(), I get a Error: bind EADDRINUSE
Asked Answered
A

2

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

Antagonize answered 12/9, 2015 at 0:25 Comment(5)
Please post your code.Carola
What node version are you using?Counterinsurgency
I am using node v0.12.7Antagonize
I made a file called test.js that just contains this var cluster = require('cluster'); for (var i = 0; i < 5; i++) { cluster.fork(); } and I still get the same error.Antagonize
Are you using the net module somewhere in your cluster to set up a server? Show us the code.Stylus
A
2

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

Antagonize answered 15/9, 2015 at 22:19 Comment(1)
it would be much better if you post your answer with updated working codes, it will help others too :)Newborn
R
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().

Renwick answered 12/9, 2015 at 6:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.