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

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

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

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

Related

When running or lifting sails/nodeJS application Error: listen EADDRINUSE

events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:901:11)
at Server._listen2 (net.js:1039:14)
at listen (net.js:1061:10)
at Server.listen (net.js:1135:5)
at Array.async.auto.start [as 0] (/var/www/html/zentiera/node_modules/sails/lib/hooks/http/start.js:29:35)
While using sails js or node js, The error may create difficulties in newbies of technology.
This specially comes when we try to stop the application using (ctrl+z)
and unfortunately the error creates while restarting it
^Z
[1]+ Stopped sails lift
There are 2 methods to recover this situation :
Need to kill the previous port
fuser -k 9002/tcp
Declare a different port number in your application
localhost//config/local.js
port: process.env.PORT || newPortNumber,

error message, node project installation on server?

Im trying to start my node project on my node server but I keep getting a error message, the server is ready to use node, I already have some other projects installed on it but this I can't understand. Can anyone understand this error message?
events.js:69
throw arguments[1]; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:850:11)
at Server._listen2 (net.js:995:14)
at listen (net.js:1022:10)
at Server.listen (net.js:1071:5)
at Function.app.listen (/srv/www/ikonset.com/node_modules/express/lib/application.js:531:24)
The error EADDRINUSE means you already have a process bound to the port you're trying to listen on (3000 or whatever). What you should do is first STOP your other process on that port, then restart your Node app.

Nodejs: Listen on port 80 after using setuid/setgid

I just got convinced by some internet articles, that using setuid/setgid to switch to a lower privileged user might be important. Since I am developing a web app, I decided to go for www-data.
So I am using the userid NPM module to figure out the user and group ID of www-data, and then change to it. However, when I do that - and it doesn't matter where entirely - I get the following (in this example, the security handler was executed at the very, very bottom of the code):
2014-09-04T23:07:05.812Z - info: BIRD3 Security -> Changed to www-data:www-data (33:33)
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EACCES
at errnoException (net.js:904:11)
at Server._listen2 (net.js:1023:19)
at listen (net.js:1064:10)
at net.js:1146:9
at dns.js:72:18
at process._tickCallback (node.js:419:13)
at Function.Module.runMain (module.js:499:11)
at startup (node.js:119:16)
at node.js:906:3
As you can see, as soon as I change my privilege level, it drops the accessibility to port 80 too.
Is there a way how I can implement security but keep using port 80 without using something like this?
You don't need a third-party module to do that. process.setgid() and process.setuid() both accept either an ID or a groupname/username.
Also make sure you are dropping the privileges AFTER listening on port 80 and that you call process.setgid() before process.setuid().
Example:
var net = require('net');
var srv = net.createServer(function(s) {
});
srv.listen(24, function() {
console.log('listening');
});
process.setgid('www-data');
process.setuid('www-data');
Isn't the setgid() and setuid() supposed to run after connecting, within the callback response?
srv.listen(24, function() {
console.log('listening');
process.setgid('www-data');
process.setuid('www-data');
});

Kill NodeJS child processes

During development I make mistakes in my NodeJS project. Mistakes lead to an error message like this.
events.js:85
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at exports._errnoException (util.js:742:11)
at Server._listen2 (net.js:1148:14)
at listen (net.js:1170:10)
at net.js:1253:9
at dns.js:82:18
at process._tickCallback (node.js:343:11)
No problem, I hit ctrl+C and restart the process but I see some spawned child processes still active. How can I kill all processes spawned by the root process?
Sample code:
module.exports.start = function(options) {
gulp.watch(['*.js'], onServerRestart);
onServerRestart();
var server;
function onServerRestart(e) {
if (server) server.kill();
server = require('child_process').spawn("node", ['--harmony', './server.js'], {stdio: "inherit", cwd: process.cwd() });
};
};
Adding this
process.on('uncaughtException', function(err) {
console.log(err);
server.kill();
process.kill();
});
solves the problem. Any suggestions how to handle this in your app?
This means, even though you may think you exited all servers, one is indeed still running, and that maybe the server you are trying to access.
Therefore go to your terminal and type the following:
killall node
This should solve it.
BAM.

Sending and receiving a UDP broadcast to and from 255.255.255.255 in Node.js

I'm trying to implement a BOOTP server in Node, for which broadcasting is a necessity. Sadly the docs are a little bit confusing and I'm getting weird errors all the way. Funny enough, the errors are different on Windows 7 and Ubuntu.
Did someone actually manage to send a UDP broadcast to 255.255.255.255 or receive one under this address?
Could someone provide me a simple Node UDP broadcasting demo?
Using punt I tried to bind a connection to 255.255.255.255 on port 5000 and I get this error EADDRNOTAVAIL
I think the address it too general. See this link
Here is the code, which is just a slightly modified version of a punt example.
var punt = require('punt');
var server = punt.bind('255.255.255.255:5000');
var a = punt.connect('255.255.255.255:5000');
server.on('message', function(msg){
console.log(msg);
});
setInterval(function(){
a.send({ hello: 'world' });
}, 150);
which yields this error:
events.js:72
throw er; // Unhandled 'error' event
^
Error: bind EADDRNOTAVAIL
at errnoException (dgram.js:439:11)
at dgram.js:206:28
at dns.js:72:18
at process._tickCallback (node.js:415:13)
at Function.Module.runMain (module.js:499:11)
at startup (node.js:119:16)
at node.js:901:3

Resources