Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detecting dead-locks in async.auto() and throwing an Error #663

Merged
merged 3 commits into from
May 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,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 @@ -580,6 +580,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