I was using PM2 to start my web application in cluster mode when I realized that it wasn't working on windows, I tried to track the issue and realized that all kinds of clustering are failing on my system.
When I try to start any clustered node application, it starts ok, until the first request is made, then one of the child processes (forks) dies, and the app stops responding to http requests.
I made up this simplest example to show off the problem:
const cluster = require('cluster');
const http = require('http');
const numCPUs = 4;
if (cluster.isMaster) {
for (let i = 0; i < numCPUs; i++) {
cluster.fork().on('exit', (code, signal) => {
if (signal) {
console.log(`worker was killed by signal: ${signal}`);
} else if (code !== 0) {
console.log(`worker exited with error code: ${code} = 0x${code.toString(16)}`);
} else {
console.log('worker success!');
}
});
}
} else {
http.createServer(function(req, res) {
res.writeHead(200);
res.end(`process ${process.pid} says hello!`);
}).listen(8000);
process.on('uncaughtException', () => console.log('uncaughtException'));
process.on('unhandledRejection', () => console.log('unhandledRejection'));
process.on('error', () => console.log('error'));
process.on('exit', () => console.log('exit'));
}
I run this example with
node app.js
the output is empty until the first request is made, then it outputs:
worker exited with error code: 3221225477 = 0xc0000005
and none of the fork error handlers print anything.
and basically the same thing happens when i run a similar example with pm2 (without if (cluster.isMaster)
part), in which case after one process dies pm2 revives it but the app still won't respond to http requests.
I'm using node version 8.1.2
64 bits, the sample works perfectly on Ubuntu, I haven't had the chance to test it on a different windows machine.
Update: I tried with 32-bits version of node and it worked, still interested in solving it though.