node/test/parallel/test-regress-GH-3238.js
Santiago Gimeno 9571be12f6 cluster: fix race condition setting suicide prop
There is no guarantee that the `suicide` property of a worker in the
master process is going to be set when the `disconnect` and `exit`
events are emitted.

To fix it, wait for the ACK of the suicide message from the master
before disconnecting the worker. Also, there's no need to send the
suicide message from the worker if the disconnection has been
initiated in the master.

Add `test-cluster-disconnect-suicide-race` that forks a lot of workers
to consistently reproduce the issue this patch tries to solve.

Modify `test-regress-GH-3238` so it checks both the `kill` and
`disconnect` cases. Also take into account that the `disconnect` event
may be received after the `exit` event.

PR-URL: https://github.com/nodejs/node/pull/4349
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
2016-01-13 20:15:02 -08:00

23 lines
542 B
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const cluster = require('cluster');
if (cluster.isMaster) {
function forkWorker(action) {
const worker = cluster.fork({ action });
worker.on('disconnect', common.mustCall(() => {
assert.strictEqual(worker.suicide, true);
}));
worker.on('exit', common.mustCall(() => {
assert.strictEqual(worker.suicide, true);
}));
}
forkWorker('disconnect');
forkWorker('kill');
} else {
cluster.worker[process.env.action]();
}