I have a child process which loops through colors to grab a palette. It works fine with smaller images, but when I upload large ones it crashes (4k crashes, 2k doesn't), and not the child process but my whole server. PM2 quickly reboots it, but I don't want it to happen at all.
This is the code that creates the child process:
const countColors = fork('count-colors.js', [], {cwd: './modules/node/image-processor', stdio: [ 'pipe', 'pipe', 'pipe', 'ipc' ]});
//send the pixel data to the process
countColors.send(pixels);
countColors.on('message', message => {
console.log('message from child:', message);
callback(message.error, message.colors);
return countColors.kill();
});
And this code is in the child process (count-colors.js):
process.on('message', data => {
var pixels = data.data;
var colors = [];
for (var i = 0; i < pixels.length; i+=4) {
var hex = hex(pixels[i + 0], pixels[i + 1], pixels[i + 2], pixels[i + 3]);
if (!colors.includes(hex))
colors.push(hex);
if (colors.length>256) return process.send({error: 'Image has over 256 colors'});
}
process.send({colors: colors});
});
Unfortunately it doesn't tell me what's happening other than this:
PM2 | App [www] with id [1] and pid [6160], exited with code [0] via signal [SIGKILL]
PM2 | Starting execution sequence in -fork mode- for app name:www id:1
PM2 | App name:www id:1 online
Is there a way to prevent the crash, or at least get some more info on why it's happening? I should say that the code above is within a try {} block, but that doesn't seem to matter.