Skip to content

Instantly share code, notes, and snippets.

@sam-github
Last active January 2, 2016 13:09
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 sam-github/8308489 to your computer and use it in GitHub Desktop.
Save sam-github/8308489 to your computer and use it in GitHub Desktop.
var cluster = require('cluster');
var net = require('net');
// listen every second
if(process.env.WORKER || cluster.isWorker) {
process.on('internalMessage', function(m) {
console.log('worker receives', m);
});
listen();
function listen() {
var s = net.createServer();
setTimeout(function() {
s.listen(3000)
.on('listening', function() {
console.log('listening, close and try again');
s.close(listen);
})
.on('error', function(er) {
console.log('error:', er);
listen();
});
}, 5000);
}
} else {
cluster.fork().process.on('internalMessage', function(m) {
console.log('master receives', m);
});
}
@sam-github
Copy link
Author

Do:

  1. nc -l 3000 or `node -e 'require("net").createServer().listen(3000)'
  2. node cluster-addr-in-use.js
  3. ... worker can't listen
  4. kill the node from (1)
  5. ... worker still can't listen

even if code is changed so worker exits, and a new worker is fork()ed to try to listen again, it still never succeeds

the protocol/address/port key in the master server handles table is never cleared on listen failure, so once listen fails, listen will never succeed again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment