2

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.

stackers
  • 2,701
  • 4
  • 34
  • 66
  • I understand that you're trying to use a multi-threading to handle computation heavy task. Why not use worker-threads instead, as creating a child process is resource heavy. https://blog.logrocket.com/node-js-multithreading-what-are-worker-threads-and-why-do-they-matter-48ab102f8b10/ . Also look https://nodejs.org/api/worker_threads.html – v1shva Jun 20 '20 at 22:15

1 Answers1

0

I think only way PM2 has to know that the process exited due to a SIGKILL is to send that signal itself.

Once said that you could try to split the message data in chunks.

Hope this helps

Daniele Ricci
  • 15,422
  • 1
  • 27
  • 55