Pm2: Error [ERR_IPC_CHANNEL_CLOSED]: channel closed

Created on 16 Jul 2017  路  5Comments  路  Source: Unitech/pm2

What's going wrong?

When shutting down my server which has a graceful shutdown using PM2 I get the following error:

Error [ERR_IPC_CHANNEL_CLOSED]: channel closed
    at process.target.send (internal/child_process.js:557:16)
    at ipcSend (/usr/lib/node_modules/pm2/node_modules/pmx/lib/utils/transport.js:22:13)
    at Object.Transport.send (/usr/lib/node_modules/pm2/node_modules/pmx/lib/utils/transport.js:34:3)
    at Timeout._onTimeout (/usr/lib/node_modules/pm2/node_modules/pmx/lib/Probe.js:120:17)
    at ontimeout (timers.js:488:11)
    at Timer.unrefdHandle (timers.js:599:5)

This does not happen when I run the server without PM2. The server shuts down perfectly, allowing ongoing connections to finish.

How could we reproduce this issue?

const cluster = require("cluster");

if (cluster.isMaster) {

    // Fork workers.
    for (let i = 0; i < 2; i++)
        cluster.fork();

    process.on('SIGINT', function() {
        console.log("SIGINT signal received: Master");
        for (let id in cluster.workers) {
            let worker = cluster.workers[id];

            worker.send({
                cmd: 'disconnect'
            });

            worker.disconnect();
        }

    });

} else {

    process.on('SIGINT', function() {});

    process.on("message", (msg) => {

        if (msg.cmd === 'disconnect') {
            console.log("disconnecting server " + process.pid);

            setTimeout(() => {
                console.log("Gracefully closed!");
            }, 10000);
        }

    });

}

Running the snippet with node, the process gracefully shuts down.

node test.js

Send SIGINT signal to process.

^CSIGINT signal received MASTER
disconnecting server 22596
disconnecting server 22601
Gracefully closed!
Gracefully closed!

But when using:

pm2 start test.js
pm2 stop test

I get:

1|test     | SIGINT signal received MASTER
1|test     | disconnecting server 22231
1|test     | disconnecting server 22232
1|test     | Process disconnected from parent !
1|test     | Error [ERR_IPC_CHANNEL_CLOSED]: channel closed
1|test     |     at process.target.send (internal/child_process.js:551:16)
1|test     |     at ipcSend (/usr/local/lib/node_modules/pm2/node_modules/pmx/lib/utils/transport.js:22:13)
1|test     |     at Object.Transport.send (/usr/local/lib/node_modules/pm2/node_modules/pmx/lib/utils/transport.js:33:3)
1|test     |     at Timeout._onTimeout (/usr/local/lib/node_modules/pm2/node_modules/pmx/lib/Probe.js:120:17)
1|test     |     at ontimeout (timers.js:488:11)
1|test     |     at Timer.unrefdHandle (timers.js:599:5)
1|test     | Process disconnected from parent !
1|test     | Error [ERR_IPC_CHANNEL_CLOSED]: channel closed
1|test     |     at process.target.send (internal/child_process.js:551:16)
1|test     |     at ipcSend (/usr/local/lib/node_modules/pm2/node_modules/pmx/lib/utils/transport.js:22:13)
1|test     |     at Object.Transport.send (/usr/local/lib/node_modules/pm2/node_modules/pmx/lib/utils/transport.js:33:3)
1|test     |     at Timeout._onTimeout (/usr/local/lib/node_modules/pm2/node_modules/pmx/lib/Probe.js:120:17)
1|test     |     at ontimeout (timers.js:488:11)
1|test     |     at Timer.unrefdHandle (timers.js:599:5)

Adding this line:

 if(e.code != "ERR_IPC_CHANNEL_CLOSED")
            process.exit(1);

in /usr/lib/node_modules/pm2/node_modules/pmx/lib/utils/transport.js fixes the problem but I'm not certain of the repercussions.

try {
    process.send(JSON.parse(stringify(args)));
  } catch(e) {
    console.error('Process disconnected from parent !');
    console.error(e.stack || e);
    if(e.code != "ERR_IPC_CHANNEL_CLOSED")
            process.exit(1);
  }
PM2 version: 2.4.6
Node version: 8.0.0 & 8.1.4
Ubuntu 16.04

Most helpful comment

@marcosc90 Just use the --no-pmx flag when using the CLI and pmx: false when using a ecosystem file, it should not load this portion of code.

All 5 comments

Are you starting this code in fork mode or cluster mode?

Btw pm2 cluster mode automatically handle the cluster module for you so you don't have to write this logic

Fork mode using:
pm2 start test.js

Yes I know it can handle the cluster module, but I need the code to be able to use cluster mode without depending on PM2.

@marcosc90 Just use the --no-pmx flag when using the CLI and pmx: false when using a ecosystem file, it should not load this portion of code.

@vmarchaud Thanks It's working perfectly without pmx!

@marcosc90 Just use the --no-pmx flag when using the CLI and pmx: false when using a ecosystem file, it should not load this portion of code.

Thanks It's a good way

Was this page helpful?
0 / 5 - 0 ratings

Related issues

waygee picture waygee  路  4Comments

ghost picture ghost  路  3Comments

morfies picture morfies  路  3Comments

liujb picture liujb  路  3Comments

FujiHaruka picture FujiHaruka  路  3Comments