diff options
author | James Crosby <james@coggle.it> | 2016-08-11 18:08:34 +0100 |
---|---|---|
committer | Graeme Yeates <yeatesgraeme@gmail.com> | 2016-11-30 13:30:32 -0500 |
commit | 74d7dc558802995f3041148317d9d7c2bfe8e4b0 (patch) | |
tree | 3a69acfdb8500679beca9ec10359586e2d1009a4 /lib | |
parent | 3b66b8a5f897fb514c155e3c97a7168599f67d1f (diff) | |
download | async-74d7dc558802995f3041148317d9d7c2bfe8e4b0.tar.gz |
prevent stack growth if queue callback is called synchronously
Diffstat (limited to 'lib')
-rw-r--r-- | lib/internal/queue.js | 12 |
1 files changed, 11 insertions, 1 deletions
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 () { |