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

Conversation

zbjornson
Copy link
Collaborator

A cluster worker can still be in the list of workers even if its IPC channel has closed (e.g. when the worker exits abruptly). Fix by checking that its IPC channel is open before sending a message.

This happens extremely infrequently (less than once a month for us, running several hundred VMs scraping every 10 seconds), but I've tested in an artificial situation that this makes communication safe:

const cluster = require("cluster");

if (cluster.isMaster) {
	for (let i = 0; i < 4; i++) {
		const worker = cluster.fork();
		// THIS SHOULDN'T LOG WITH THE FIX
		worker.on("error", err => console.log("worker error", err));
	}

	cluster.on("exit", (worker, code, signal) => {
		setTimeout(() => cluster.fork(), 500);
	});

	setInterval(() => {
		for (const id in cluster.workers) {
			if (cluster.workers[id].isConnected()) // TOGGLE THIS LINE
				cluster.workers[id].send({type: "hi!"});
		}
	}, 5);
} else {
	setTimeout(() => process.exit(1), 500);
}

Also removes some code that was for compat with Node.js pre-v6, since v11 of this lib dropped support for pre-v6.

A cluster worker can still be in the list of workers even if its IPC channel has closed (e.g. when the worker exits abruptly). Check that its IPC channel is open before sending a message.
@kevinolson
Copy link

@zbjornson any chance we could get this merged and a new tag pushed out as I'm also running into this issue. Thanks!

@tadjohnston tadjohnston mentioned this pull request Apr 1, 2019
@zbjornson
Copy link
Collaborator Author

@SimenB okay with you?

@siimon siimon merged commit 5d21477 into siimon:master Apr 2, 2019
@siimon
Copy link
Owner

siimon commented Apr 2, 2019

Published as 11.3.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants