简体   繁体   中英

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

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

我使用所有线程不止一次启动服务器,因此端口已被绑定

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() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM