summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGraeme Yeates <yeatesgraeme@gmail.com>2016-11-30 13:31:36 -0500
committerGraeme Yeates <yeatesgraeme@gmail.com>2016-11-30 13:31:36 -0500
commit7d51d6fca345080b44ed0a794f982c0965a5223e (patch)
tree2f535b8e287b45e43d34e1c18bf544b2dfeef6dc
parent74d7dc558802995f3041148317d9d7c2bfe8e4b0 (diff)
downloadasync-7d51d6fca345080b44ed0a794f982c0965a5223e.tar.gz
Slightly different approach to avoiding starting to processing tasks with sync callbacks
-rw-r--r--lib/internal/queue.js20
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;