diff --git a/lib/net.js b/lib/net.js index 621a1dd7b78..439573954cd 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1061,6 +1061,25 @@ function Server (listener) { self.connections = 0; + self.paused = false; + self.pauseTimeout = 1000; + + function pause () { + // We've hit the maximum file limit. What to do? + // Let's try again in 1 second. + self.watcher.stop(); + + // If we're already paused, don't do another timeout. + if (self.paused) return; + + setTimeout(function () { + self.paused = false; + // Make sure we haven't closed in the interim + if (typeof self.fd != 'number') return; + self.watcher.start(); + }, self.pauseTimeout); + } + self.watcher = new IOWatcher(); self.watcher.host = self; self.watcher.callback = function () { @@ -1068,7 +1087,10 @@ function Server (listener) { try { var peerInfo = accept(self.fd); } catch (e) { - if (e.errno == EMFILE) return; + if (e.errno == EMFILE) { + pause(); + return; + } throw e; } if (!peerInfo) return;