summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJames Crosby <james@coggle.it>2016-08-11 18:08:34 +0100
committerGraeme Yeates <yeatesgraeme@gmail.com>2016-11-30 13:30:32 -0500
commit74d7dc558802995f3041148317d9d7c2bfe8e4b0 (patch)
tree3a69acfdb8500679beca9ec10359586e2d1009a4 /lib
parent3b66b8a5f897fb514c155e3c97a7168599f67d1f (diff)
downloadasync-74d7dc558802995f3041148317d9d7c2bfe8e4b0.tar.gz
prevent stack growth if queue callback is called synchronously
Diffstat (limited to 'lib')
-rw-r--r--lib/internal/queue.js12
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 () {