Skip to content

Commit 029954b

Browse files
fix: detect a duplicate item callback in the async helpers
`each` and `eachLimit` handed the shared completion callback to every item, so an iterator reporting twice decremented the counter twice and settled the run while a later item was still in flight — silently, where `neo-async` threw. Wrap each item's callback so it can only report once. `map` already could not be corrupted this way; it now throws too, so all four helpers fail the same way.
1 parent 786d167 commit 029954b

2 files changed

Lines changed: 60 additions & 27 deletions

File tree

‎lib/util/async.js‎

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,27 @@
55

66
"use strict";
77

8+
/**
9+
* Wraps the shared completion callback for one item, so an iterator that
10+
* reports twice fails loudly instead of settling the run early.
11+
* @template E
12+
* @param {(err?: E | null) => void} done shared completion callback
13+
* @returns {(err?: E | null) => void} completion callback for a single item
14+
*/
15+
const once = (done) => {
16+
let pending = done;
17+
return (err) => {
18+
const fn = pending;
19+
if (fn === undefined) throw new Error("Callback was already called");
20+
pending = undefined;
21+
fn(err);
22+
};
23+
};
24+
825
/**
926
* Runs `iterator` over every item with unlimited concurrency.
10-
* Every item is started from one flat loop and they share a single completion
11-
* callback, so a synchronous iterator neither grows the stack nor allocates
12-
* per item.
27+
* Every item is started from one flat loop, so a synchronous iterator never
28+
* grows the stack.
1329
* @template T
1430
* @template E
1531
* @param {Iterable<T>} collection items to iterate
@@ -45,15 +61,15 @@ const each = (collection, iterator, callback) => {
4561
remaining = length;
4662
// Dispatching continues past an error on purpose: callers rely on every
4763
// item being started, e.g. to close every watcher when one close fails.
48-
for (let i = 0; i < length; i++) iterator(array[i], done);
64+
for (let i = 0; i < length; i++) iterator(array[i], once(done));
4965
return;
5066
}
5167

5268
// `remaining` starts at 1 so that completion cannot fire while items of a
5369
// collection of unknown length are still being dispatched.
5470
for (const item of collection) {
5571
remaining++;
56-
iterator(item, done);
72+
iterator(item, once(done));
5773
}
5874
done(null);
5975
};
@@ -109,7 +125,7 @@ const eachLimit = (collection, limit, iterator, callback) => {
109125
item = array[index++];
110126
}
111127
running++;
112-
iterator(item, done);
128+
iterator(item, once(done));
113129
}
114130
dispatching = false;
115131
if (exhausted && running === 0 && !finished) {
@@ -186,9 +202,7 @@ const map = (collection, iterator, callback) => {
186202
let position = index;
187203
return (err, result) => {
188204
const at = position;
189-
// Guards against an iterator that calls back twice, which would
190-
// otherwise settle the whole run early.
191-
if (at === -1) return;
205+
if (at === -1) throw new Error("Callback was already called");
192206
position = -1;
193207
// A result arriving after the run settled must not reach `results`,
194208
// which the callback already handed to the caller.

‎test/Async.unittest.js‎

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ describe("each", () => {
9898
);
9999
});
100100

101+
it("throws when an iterator calls back twice", () => {
102+
// Without this the run settles early, while later items are still going.
103+
expect(() => {
104+
each(
105+
[1, 2, 3, 4],
106+
(item, callback) => {
107+
callback();
108+
if (item === 1) callback();
109+
},
110+
() => {}
111+
);
112+
}).toThrow("Callback was already called");
113+
});
114+
101115
it("does not grow the stack for a synchronous iterator", (done) => {
102116
const items = Array.from({ length: 200000 }, (_, i) => i);
103117
let count = 0;
@@ -251,6 +265,20 @@ describe("eachLimit", () => {
251265
});
252266
});
253267

268+
it("throws when an iterator calls back twice", () => {
269+
expect(() => {
270+
eachLimit(
271+
[1, 2, 3, 4],
272+
2,
273+
(item, callback) => {
274+
callback();
275+
if (item === 1) callback();
276+
},
277+
() => {}
278+
);
279+
}).toThrow("Callback was already called");
280+
});
281+
254282
it("completes immediately when the limit is below one", () => {
255283
/** @type {(Error | null | undefined)[]} */
256284
const calls = [];
@@ -403,26 +431,17 @@ describe("map", () => {
403431
});
404432
});
405433

406-
it("ignores an iterator that calls back twice", (done) => {
407-
let calls = 0;
408-
map(
409-
[1, 2],
410-
(item, callback) => {
411-
setImmediate(() => {
434+
it("throws when an iterator calls back twice", () => {
435+
expect(() => {
436+
map(
437+
[1, 2],
438+
(item, callback) => {
412439
callback(null, item);
413440
callback(null, item);
414-
});
415-
},
416-
(err, results) => {
417-
calls++;
418-
expect(err).toBeNull();
419-
expect(results).toEqual([1, 2]);
420-
setImmediate(() => {
421-
expect(calls).toBe(1);
422-
done();
423-
});
424-
}
425-
);
441+
},
442+
() => {}
443+
);
444+
}).toThrow("Callback was already called");
426445
});
427446

428447
it("works with a synchronous iterator", (done) => {

0 commit comments

Comments
 (0)