From e7f8209c908dc4b1cd3d6225fea4057231ab91ee Mon Sep 17 00:00:00 2001 From: Tom V Date: Fri, 18 Apr 2014 20:49:00 -0700 Subject: [PATCH] Fix the nextTick(cb) causing event loop starvation If you run more than 1000 lookups (like processing a big directory of files), you will get lots of warnings from node. Solution is to use setImmediate(cb) instead. --- lib/executor.js | 6 +++++- test/executor.test.js | 21 ++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/executor.js b/lib/executor.js index f06f1ab..a555bd3 100644 --- a/lib/executor.js +++ b/lib/executor.js @@ -22,7 +22,11 @@ function Executor () { // Always tell the queue task is complete. Execute callback if any was given. if (typeof lastArg === 'function') { callback = function () { - process.nextTick(cb); + if (typeof setImmediate === 'function') { + setImmediate(cb); + } else { + process.nextTick(cb); + } lastArg.apply(null, arguments); }; diff --git a/test/executor.test.js b/test/executor.test.js index 17b907e..0c911bd 100644 --- a/test/executor.test.js +++ b/test/executor.test.js @@ -74,7 +74,22 @@ function testRightOrder (d, done) { }); } - + + +// Note: The following test does not have any assertion because it +// is meant to address the deprecation warning: +// (node) warning: Recursive process.nextTick detected. This will break in the next version of node. Please use setImmediate for recursive deferral. +// see +var testEventLoopStarvation = function(d, done){ + var times = 1001; + var i = 0; + while ( i