enRaiser enRaiser - 3 months ago 12
Node.js Question

child_process exits with code null

I am running my node server in centos6.X, and forking a child process.

var cp = require('child_process');
child = cp.fork(__dirname+'/worker');
child.on('exit', function (code, signal) {
console.log('Child exited with code = ' +code+' signal = '+signal);
});


but after few seconds I get this error


Child exited with code = null signal = SIGKILL


I am now clueless how to get rid of this, there is no more trace, as to what is happening.

more clarification

I have put console.logs in worker and they are getting printed. the worker is now waiting for message from main server. but exits after waiting for few seconds. I also run the worker js independently with node command and there it does not exit.

The same code works fine on my local OSX laptop. but on cloud centos6.X I am facing this issue.

Answer

There are two ways a child process can exit: voluntarily, in which case an exit code will be returned, or involuntarily, through a signal.

From the arguments of the exit handler, the first reflects the exit code, and the second the signal. One of these will contain a value, the other one will be null. As explained in the documentation:

If the process exited, code is the final exit code of the process, otherwise null. If the process terminated due to receipt of a signal, signal is the string name of the signal, otherwise null. One of the two will always be non-null.

If your child process receives a SIGKILL, and you're not the one that (explicitly) sent that signal, there's a good chance that the child process got killed by the Linux OOM killer, which kills processes in case the system is strapped for memory ("OOM" means "Out Of Memory").

So make sure that your system has enough RAM, enough swap, and/or that your process doesn't take up too much memory (if possible).