From 8c53d2fe9f102944cc1889c4efcac7a86224cf0a Mon Sep 17 00:00:00 2001 From: Santiago Gimeno Date: Tue, 31 May 2016 11:16:02 +0200 Subject: [PATCH] cluster: don't send messages if no IPC channel Avoid sending messages if the IPC channel is already disconnected. It avoids undesired errors when calling `process.disconnect` when there are still pending IPC messages. PR-URL: https://github.com/nodejs/node/pull/7132 Reviewed-By: James M Snell Reviewed-By: Colin Ihrig --- lib/cluster.js | 3 +++ .../test-cluster-process-disconnect.js | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 test/parallel/test-cluster-process-disconnect.js diff --git a/lib/cluster.js b/lib/cluster.js index 228ad0dfe28c06..b4e2ea42dd516f 100644 --- a/lib/cluster.js +++ b/lib/cluster.js @@ -743,6 +743,9 @@ function workerInit() { var seq = 0; var callbacks = {}; function sendHelper(proc, message, handle, cb) { + if (!proc.connected) + return false; + // Mark message as internal. See INTERNAL_PREFIX in lib/child_process.js message = util._extend({ cmd: 'NODE_CLUSTER' }, message); if (cb) callbacks[seq] = cb; diff --git a/test/parallel/test-cluster-process-disconnect.js b/test/parallel/test-cluster-process-disconnect.js new file mode 100644 index 00000000000000..f83dd0094998df --- /dev/null +++ b/test/parallel/test-cluster-process-disconnect.js @@ -0,0 +1,18 @@ +'use strict'; +const common = require('../common'); +const assert = require('assert'); +const cluster = require('cluster'); + +if (cluster.isMaster) { + const worker = cluster.fork(); + worker.on('exit', common.mustCall((code, signal) => { + assert.strictEqual(code, 0, 'worker did not exit normally'); + assert.strictEqual(signal, null, 'worker did not exit normally'); + })); +} else { + const net = require('net'); + const server = net.createServer(); + server.listen(common.PORT, common.mustCall(() => { + process.disconnect(); + })); +}