Skip to content

Instantly share code, notes, and snippets.

@pitaj
Created May 30, 2018 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pitaj/e8463a023847697e72e7f01ac8ed2fd6 to your computer and use it in GitHub Desktop.
Save pitaj/e8463a023847697e72e7f01ac8ed2fd6 to your computer and use it in GitHub Desktop.
Repro for nodejs/node#9706 on windows (at least). Run like `node master 1111`. 3500 works for me, but 4000 will hang forever.
'use strict';
const { fork } = require('child_process');
const { join } = require('path');
const count = process.argv[2] * limit;
// number of cores on your machine - 1
// or whatever you want \_(*_*)_/
const limit = 7;
function log(str) {
process.stdout.write(`\n${Date.now()} | ${str}`);
}
const processes = [];
function loaded(proc) {
processes.push(proc);
if (processes.length >= limit) {
initialized();
}
}
for (let i = 0; i < limit; i += 1) {
const proc = fork(join(__dirname, 'slave.js'), {
stdio: [0, 1, 2, 'ipc'],
});
proc.on('error', err => { throw err; });
proc.on('message', (response) => {
if (response === 'initialized') {
loaded(proc);
}
});
}
const responses = [];
function initialized() {
processes.forEach((proc) => {
proc.on('message', ({ number, hash }) => {
responses.push([number, hash]);
log(`got response ${hash} for ${number}`);
if (responses.length >= count) {
done();
}
});
});
for (let i = 0; i < count; i += 1) {
log(`sending ${i}`);
processes[i % limit].send(i);
}
}
function done() {
processes.forEach(proc => proc.kill());
}
'use strict';
const bcrypt = require('bcrypt');
function log(str) {
process.stdout.write(`\n${Date.now()} | ${str}`);
}
process.on('message', (number) => {
log(`hashing ${number}`);
const hash = bcrypt.hashSync(number.toString(), 12);
log(`hash of ${number}: ${hash}`);
process.send({
number,
hash,
});
});
process.send('initialized');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment