Kill NodeJS child processes - node.js

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.

Related

Nodejs server stop with error

My server stops with this error how can i solve it? Or what it is?
events.js:182
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at exports._errnoException (util.js:1024:11)
at TCP.onread (net.js:610:25)
Thanks for answers
"ECONNRESET" means the other side of the TCP conversation abruptly closed its end of the connection. This is most probably due to one or more application protocol errors. You could look at the API server logs to see if it complains about something.
To know more about ECONNRESET, see this answer.
The message Unhandled 'error' event is suggesting that you are not listening for the error event in your code.
You can catch errors like these by catching the error event like the following
connection.on("error", function(err){ // handle "error" event so nodejs will not crash
console.log(err);
});

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

foreverjs turns process in STOPPED state instead of restarting

I have nodejs process that listens for an connection. I want to let foreverjs restart it on any connection error this way:
amqpConnection.on('error', function (err) {
console.error(err);
process.exit(1);
});
But this don't work. After error I see my process in this state:
~> forever list
info: Forever processes running
data: uid command script logfile
data: [0] OpQF /usr/bin/node my-script.js my-script-log.log STOPPED
Log file has connection error and two attempts to restart script.
error: Script restart attempt #2
{ [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect' }
error: Forever detected script exited with code: 1
Why forever stops trying to restart my process?
I should have read warnings that forever prints!
Script that not stay running more than --minUptime (1000ms by default) considered "spinning". One should set --spinSleepTime to make spinning scripts to be restarted. When this parameter is omitted spinning script won't be restarted.
I have reproduced your problem and found that forever successfully restart your process if some delay (1sec) take place before process.exit.
It's look that forever don't restart process when it crash too fast.
setTimeout(function() {
process.exit(1);
}, 1000);

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');
});

Quitting node.js gracefully

I'm reading through the excellent online book http://nodebeginner.org/ and trying out the simple code
var http = require("http");
function onRequest(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
Now I didn't know (and I still don't know!) how to shut down node.js gracefully, so I just went ctrl+z. Now each time I try to run node server.js I get the following error messages.
node.js:134
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: EADDRINUSE, Address already in use
at Server._doListen (net.js:1100:5)
at net.js:1071:14
at Object.lookup (dns.js:153:45)
at Server.listen (net.js:1065:20)
at Object.<anonymous> (/Users/Bob/server.js:7:4)
at Module._compile (module.js:402:26)
at Object..js (module.js:408:10)
at Module.load (module.js:334:31)
at Function._load (module.js:293:12)
at Array.<anonymous> (module.js:421:10)
So, two questions:
1) How do I shut down node.js gracefully?
2) How do I repair the mess I've created?
I currently use Node's event system to respond to signals. Here's how I use the Ctrl-C (SIGINT) signal in a program:
process.on( 'SIGINT', function() {
console.log( "\nGracefully shutting down from SIGINT (Ctrl-C)" );
// some other closing procedures go here
process.exit( );
})
You were getting the 'Address in Use' error because Ctrl-Z doesn't kill the program; it just suspends the process on a unix-like operating system and the node program you placed in the background was still bound to that port.
On Unix-like systems, [Control+Z] is the most common default keyboard
mapping for the key sequence that suspends a process (SIGTSTP).[3]
When entered by a user at their computer terminal, the currently
running foreground process is sent a SIGTSTP signal, which generally
causes the process to suspend its execution. The user can later
continue the process execution by typing the command 'fg' (short for
foreground) or by typing 'bg' (short for background) and furthermore
typing the command 'disown' to separate the background process from
the terminal.1
You would need to kill your processes by doing a kill <pid> or 'killall -9 node' or the like.
Use Ctrl+C to exit the node process gracefully
To clean up the mess depends on your platform, but basically you need to find the remains of the process in which node was running and kill it.
For example, on Unix: ps -ax | grep node will give you an entry like:
1039 ttys000 0:00.11 node index.js
where index.js is the name of your node file.
In this example, 1039 is the process id (yours will be different), so kill -9 1039 will end it, and you'll be able to bind to the port again.
As node.js is an event-driven runtime the most graceful exit is to exhaust the queue of pending events. When the event queue is empty the process will end. You can ensure the event queue is drained by doing things such as clearing any interval timers that are set and by closing down any servers with open socket connections. It gets trickier when using 3rd party modules because you are at the mercy of whether the module author has taken care to gracefully drain the pending events it created. This might not be the most practical way to exit a node.js process as you will spend a lot of effort tracking down 'leaked' pending events, but it is the most graceful I think.
Type either
process.exit()
or
.exit
to exit node gracefully.
Hitting Control + C twice will force an exit.
1) How do I shut down node.js gracefully?
Listening for a SIGINT signal. On Windows you need to listen for a ctrl-c with the readline module.
I've written my own solution to provide an application a graceful shutdown and the usage of domains: grace. It's worth to have a look.

Resources