Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rare ERR_IPC_CHANNEL_CLOSED bug in cluster mode #244

Merged
merged 2 commits into from
Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ project adheres to [Semantic Versioning](http://semver.org/).

### Changed

- Check that cluster worker is still connected before attempting to query it for
metrics. (#244)

### Added

## [11.2.1]
Expand Down
32 changes: 17 additions & 15 deletions lib/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ class AggregatorRegistry extends Registry {
const requestId = requestCtr++;

return new Promise((resolve, reject) => {
const nWorkers = Object.keys(cluster.workers).length;

function done(err, result) {
// Don't resolve/reject the promise if a callback is provided
if (typeof callback === 'function') {
Expand All @@ -50,13 +48,9 @@ class AggregatorRegistry extends Registry {
}
}

if (nWorkers === 0) {
return process.nextTick(() => done(null, ''));
}

const request = {
responses: [],
pending: nWorkers,
pending: 0,
done,
errorTimeout: setTimeout(() => {
request.failed = true;
Expand All @@ -71,7 +65,21 @@ class AggregatorRegistry extends Registry {
type: GET_METRICS_REQ,
requestId
};
for (const id in cluster.workers) cluster.workers[id].send(message);

for (const id in cluster.workers) {
// If the worker exits abruptly, it may still be in the workers
// list but not able to communicate.
if (cluster.workers[id].isConnected()) {
cluster.workers[id].send(message);
request.pending++;
}
}

if (request.pending === 0) {
// No workers were up
clearTimeout(request.errorTimeout);
process.nextTick(() => done(null, ''));
}
});
}

Expand Down Expand Up @@ -147,13 +155,7 @@ function addListeners() {

if (cluster.isMaster) {
// Listen for worker responses to requests for local metrics
cluster.on('message', function(worker, message) {
if (arguments.length === 2) {
// pre-Node.js v6.0
message = worker;
worker = undefined;
}

cluster.on('message', (worker, message) => {
if (message.type === GET_METRICS_RES) {
const request = requests.get(message.requestId);
message.metrics.forEach(registry => request.responses.push(registry));
Expand Down