From 74d7dc558802995f3041148317d9d7c2bfe8e4b0 Mon Sep 17 00:00:00 2001 From: James Crosby Date: Thu, 11 Aug 2016 18:08:34 +0100 Subject: prevent stack growth if queue callback is called synchronously --- lib/internal/queue.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/internal/queue.js b/lib/internal/queue.js index c73c322..44f1c01 100644 --- a/lib/internal/queue.js +++ b/lib/internal/queue.js @@ -70,11 +70,14 @@ export default function queue(worker, concurrency, payload) { if (q.idle()) { q.drain(); } - q.process(); + if (!sync) { + q.process(); + } }); } var workers = 0; + var sync = 0; var workersList = []; var q = { _tasks: new DLL(), @@ -120,7 +123,14 @@ 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; } }, length: function () { -- cgit v1.2.1