Permalink
Browse files

Close sockets when children exit to avoid file descriptor leaks.

Not closing the socket connections leaves open files on the system. On a
system with fd limit at 1024 and 15 cores we quickly reached the limit
after a few children restarts; resulting in the master process raising
an (EMFILE, Too many open files) exception. The leaking was checked with
lsof.
  • Loading branch information...
1 parent 40ead73 commit 9580da13a332b70befad961f284956873c47baba @4d47 4d47 committed Aug 31, 2011
Showing with 4 additions and 2 deletions.
  1. +4 −2 lib/multi-node.js
View
@@ -52,6 +52,8 @@ exports.listen = function(options, server){
}
var createChild = function(i){
var childConnection = netBinding.socketpair();
+ var siblingConnection = netBinding.socketpair();
+ var masterChildConnection = netBinding.socketpair();
env._CHILD_ID_ = "child-" + i;
// spawn the child process
var child = children[i] = childProcess.spawn(
@@ -65,11 +67,9 @@ exports.listen = function(options, server){
child.master.write("tcp", "ascii", tcpDescriptor);
(function(child){
for(var j = 0; j < i; j++){
- var siblingConnection = netBinding.socketpair();
child.master.write("sibling", "ascii", siblingConnection[1]);
children[j].master.write("sibling", "ascii", siblingConnection[0]);
}
- var masterChildConnection = netBinding.socketpair();
process.nextTick(function(){
var stream = new net.Stream(masterChildConnection[0], "unix");
emitter.emit("node", stream);
@@ -78,6 +78,8 @@ exports.listen = function(options, server){
});
})(child);
child.addListener("exit", function(){
+ // cleanup connections
+ [].concat(childConnection, siblingConnection, masterChildConnection).forEach(netBinding.close);
// remove the dead one
children.splice(i, 1);
// make a new process to replace the dead one

0 comments on commit 9580da1

Please sign in to comment.