diff options
-rw-r--r-- | lib/internal/queue.js | 20 |
1 files changed, 9 insertions, 11 deletions
diff --git a/lib/internal/queue.js b/lib/internal/queue.js index 44f1c01..15fb950 100644 --- a/lib/internal/queue.js +++ b/lib/internal/queue.js @@ -70,15 +70,13 @@ export default function queue(worker, concurrency, payload) { if (q.idle()) { q.drain(); } - if (!sync) { - q.process(); - } + q.process(); }); } var workers = 0; - var sync = 0; var workersList = []; + var isProcessing = false; var q = { _tasks: new DLL(), concurrency: concurrency, @@ -102,6 +100,12 @@ export default function queue(worker, concurrency, payload) { _insert(data, true, callback); }, process: function () { + // Avoid trying to start too many processing operations. This can occur + // when callbacks resolve synchronously (#1267). + if (isProcessing) { + return; + } + isProcessing = true; while(!q.paused && workers < q.concurrency && q._tasks.length){ var tasks = [], data = []; var l = q._tasks.length; @@ -123,15 +127,9 @@ export default function queue(worker, concurrency, payload) { } var cb = onlyOnce(_next(tasks)); - - // prevent stack growth when calling callback synchronously: - // unroll the recursion into a loop here. (The callback will - // have reduced the workers count synchronously, causing us to - // loop again) - sync = 1; worker(data, cb); - sync = 0; } + isProcessing = false; }, length: function () { return q._tasks.length; |