简体   繁体   English

分叉子进程的问题是杀死父进程而不显示错误

[英]Problem with forked child process is killing parent process without showing error

I have a child process which loops through colors to grab a palette.我有一个子进程,它循环通过 colors 来获取调色板。 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.它适用于较小的图像,但是当我上传大图像时它会崩溃(4k 崩溃,2k 不会),而不是子进程而是我的整个服务器。 PM2 quickly reboots it, but I don't want it to happen at all. PM2 迅速重新启动它,但我根本不希望它发生。

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):这段代码在子进程中(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.我应该说上面的代码在 try {} 块中,但这似乎并不重要。

I think only way PM2 has to know that the process exited due to a SIGKILL is to send that signal itself.我认为 PM2 必须知道进程因SIGKILL退出的唯一方法是自己发送该信号。

Once said that you could try to split the message data in chunks.曾经说过,您可以尝试将消息数据分成块。

Hope this helps希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM