codename- codename- - 4 days ago 5
Node.js Question

Error: IPC channel is already disconnected

Where/when should I catch this error?

Error: IPC channel is already disconnected


This is called from
child_process
(using
cluster
) module when I call
.disconect()
multiple times. I know that I shouldn't call this twice (or more), but sometimes I don't have control over this. Eventually how to prevent calling this multiple times? This code doesn't works:

try {
if (worker.state !== "disconnected" && worker.state !== "dead") {
worker.disconnect();
}
} catch (error) {}


EDIT:

This is stack trace of this error:

events.js:71
throw arguments[1]; // Unhandled 'error' event
^
Error: IPC channel is already disconnected
at process.target.disconnect (child_process.js:392:26)
at ProgressTracker.callback (cluster.js:437:20)
at ProgressTracker.check (cluster.js:94:32)
at Object.Worker.disconnect [as 44:2] (cluster.js:445:16)
at handleResponse (cluster.js:149:41)
at respond (cluster.js:170:5)
at handleMessage (cluster.js:180:5)
at process.EventEmitter.emit (events.js:126:20)
at handleMessage (child_process.js:269:12)
at Pipe.channel.onread (child_process.js:293:9)

Answer

This error should be handled in worker file. For example using this code:

process.on("uncaughtException", function (error) {
  if (error.toString() !== 'Error: IPC channel is already disconnected') {
    process.stderr.write(error.stack);
    process.exit(1);
  }
}); 

All tries for catching it from parent script was unsuccessful.
I have hope that this help someone deal with this problem.

Comments