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

Fix #1092 #1140

Merged
merged 4 commits into from May 5, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 47 additions & 19 deletions lib/auto.js
Expand Up @@ -2,7 +2,7 @@

import arrayEach from 'lodash/_arrayEach';
import forOwn from 'lodash/forOwn';
import indexOf from 'lodash/_baseIndexOf';
import indexOf from 'lodash/indexOf';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aearly I wrote my checkForDeadlocks assuming I had the lodash/JavaScript standard semantics. Got test failures because I hadn't included start index.

Thought I was better off importing the public API method, in case someone else were to come and repeat my mistake at another time

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We weren't importing indexof to avoid including the lodash internal
toInteger implementation
On May 4, 2016 3:53 PM, "Paul Anderson" notifications@github.com wrote:

In lib/auto.js
#1140 (comment):

@@ -2,7 +2,7 @@

import arrayEach from 'lodash/_arrayEach';
import forOwn from 'lodash/forOwn';
-import indexOf from 'lodash/_baseIndexOf';
+import indexOf from 'lodash/indexOf';

@aearly https://github.com/aearly I wrote my checkForDeadlocks assuming
I had the lodash/JavaScript standard semantics. Got test failures because I
hadn't included start index.

Thought I was better off importing the public API method, in case someone
else were to come and repeat my mistake at another time


You are receiving this because you commented.
Reply to this email directly or view it on GitHub
https://github.com/caolan/async/pull/1140/files/81e888fd40e99c0de0098a7554d6cdafc540c021#r62103119

import isArray from 'lodash/isArray';
import okeys from 'lodash/keys';
import noop from 'lodash/noop';
Expand Down Expand Up @@ -112,36 +112,33 @@ export default function (tasks, concurrency, callback) {

var readyTasks = [];

// for cycle detection:
var readyToCheck = []; // tasks that have been identified as reachable
// without the possibility of returning to an ancestor task
var uncheckedDependencies = {};

forOwn(tasks, function (task, key) {
if (!isArray(task)) {
// no dependencies
enqueueTask(key, [task]);
readyToCheck.push(key);
return;
}

var dependencies = task.slice(0, task.length - 1);
var remainingDependencies = dependencies.length;

checkForDeadlocks();

function checkForDeadlocks() {
var len = dependencies.length;
var dep;
while (len--) {
if (!(dep = tasks[dependencies[len]])) {
throw new Error('async.auto task `' + key +
'` has non-existent dependency in ' +
dependencies.join(', '));
}
if (isArray(dep) && indexOf(dep, key, 0) >= 0) {
throw new Error('async.auto task `' + key +
'`Has cyclic dependencies');
}
}
if (!remainingDependencies) {
enqueueTask(key, [task]);
readyToCheck.push(key);
}
uncheckedDependencies[key] = remainingDependencies;

arrayEach(dependencies, function (dependencyName) {
if (!tasks[dependencyName]) {
throw new Error('async.auto task `' + key +
'` has a non-existent dependency in ' +
dependencies.join(', '));
}
addListener(dependencyName, function () {
remainingDependencies--;
if (remainingDependencies === 0) {
Expand All @@ -151,9 +148,9 @@ export default function (tasks, concurrency, callback) {
});
});

checkForDeadlocks();
processQueue();


function enqueueTask(key, task) {
readyTasks.push(function () {
runTask(key, task);
Expand Down Expand Up @@ -222,5 +219,36 @@ export default function (tasks, concurrency, callback) {
}
}

function checkForDeadlocks() {
// Kahn's algorithm
// https://en.wikipedia.org/wiki/Topological_sorting#Kahn.27s_algorithm
// http://connalle.blogspot.com/2013/10/topological-sortingkahn-algorithm.html
var currentTask;
var counter = 0;
while (readyToCheck.length) {
currentTask = readyToCheck.pop();
counter++;
arrayEach(getDependents(currentTask), function (dependent) {
if (!(--uncheckedDependencies[dependent])) {
readyToCheck.push(dependent);
}
});
}

if (counter !== numTasks) {
throw new Error(
'async.auto cannot execute tasks due to a recursive dependency'
);
}
}

function getDependents(taskName) {
var result = [];
forOwn(tasks, function (task, key) {
if (isArray(task) && indexOf(task, taskName) >= 0) {
result.push(key);
}
});
return result;
}
}
17 changes: 17 additions & 0 deletions mocha_test/auto.js
Expand Up @@ -305,6 +305,23 @@ describe('auto', function () {
done();
});

// Issue 1092 on github: https://github.com/caolan/async/issues/1092
it('extended cycle detection', function(done) {
var task = function (name) {
return function (results, callback) {
callback(null, 'task ' + name);
};
};
expect(function () {
async.auto({
a: ['c', task('a')],
b: ['a', task('b')],
c: ['b', task('c')]
});
}).to.throw();
done();
});

// Issue 988 on github: https://github.com/caolan/async/issues/988
it('auto stops running tasks on error', function(done) {
async.auto({
Expand Down