Skip to content

Commit

Permalink
Merge pull request #663 from Mickael-van-der-Beek/dead-locks
Browse files Browse the repository at this point in the history
Detecting dead-locks in async.auto() and throwing an Error
  • Loading branch information
aearly committed May 19, 2015
2 parents c7b3ba0 + 74ea68b commit 9206415
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,17 @@
}
};
var requires = task.slice(0, Math.abs(task.length - 1)) || [];
// prevent dead-locks
var len = requires.length;
var dep;
while (len--) {
if (!(dep = tasks[requires[len]])) {
throw new Error('Has inexistant dependency');
}
if (_isArray(dep) && !!~dep.indexOf(k)) {
throw new Error('Has cyclic dependencies');
}
}
var ready = function () {
return _reduce(requires, function (a, x) {
return (a && results.hasOwnProperty(x));
Expand Down
27 changes: 27 additions & 0 deletions test/test-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,33 @@ exports['auto modifying results causes final callback to run early'] = function(
});
};

// Issue 263 on github: https://github.com/caolan/async/issues/263
exports['auto prevent dead-locks due to inexistant dependencies'] = function(test) {
test.throws(function () {
async.auto({
task1: ['noexist', function(callback, results){
callback(null, 'task1');
}]
});
}, Error);
test.done();
};

// Issue 263 on github: https://github.com/caolan/async/issues/263
exports['auto prevent dead-locks due to cyclic dependencies'] = function(test) {
test.throws(function () {
async.auto({
task1: ['task2', function(callback, results){
callback(null, 'task1');
}],
task2: ['task1', function(callback, results){
callback(null, 'task2');
}]
});
}, Error);
test.done();
};

// Issue 306 on github: https://github.com/caolan/async/issues/306
exports['retry when attempt succeeds'] = function(test) {
var failed = 3
Expand Down

0 comments on commit 9206415

Please sign in to comment.