After the reject, the workers keep looping. That is the hole in yesterday's pre-fill piece, and sunnatulla_dev found it before I did. In his run, tasks 6 to 9 started at 301 to 421 milliseconds, after the call had already rejected at 272, and a second later every slot read ok. So the array calls them skipped, they ran, and anyone who retries them runs them twice. His fix is one line: set stopped = true in the catch, and let the workers check while (!stopped && ...) before picking up the next task.
I ran his version on the same shape. Ten tasks, three workers, task 4 throwing after 150 milliseconds. Same delays as yesterday, both arms twice, and both repeats came back identical. The flag works. It also leaves two tasks double-running, and those two are the interesting part.
Here is the shape, with the flag in:
async function mapWithConcurrency(tasks, limit) { const results = tasks.map(() => ({ status: "skipped" })); let next = 0, stopped = false; async function worker() { while (!stopped && next < tasks.length) { const i = next++; try { results[i] = await tasks[i](); } catch (err) { results[i] = { status: "failed", error: err.message }; stopped = true; throw err; } } } await Promise.all(Array.from({ length: limit }, () => worker())); return results; } The reject lands at the same moment in both arms, 275 to 277 milliseconds across the four runs. The flag costs nothing on wall time, because it does not stop anything that is already running. It only stops new pickups, and there is nothing to pick up until a worker finishes a task.
| No flag | Stopped flag | |
|---|---|---|
| Reject wall time | 277 ms | 277 ms |
| Tasks started after the reject | 5 (tasks 5–9, at 302–743 ms) | 0 |
| Slots reading "skipped" at the reject | 7 | 7 |
| Slots reading "skipped" 1.2 s later | 0 | 5 |
| Tasks double-run by retrying the "skipped" slots | 7 | 2 |
Read the no-flag row first, because it is worse than it sounds. At the reject, seven slots read skipped. A second later, none of them do. Tasks 5 to 9 started after the call had already failed and finished fine, and tasks 2 and 3 were still running when the reject landed and finished fine too. The array drifts under the caller. A retry built from the reject-time snapshot runs all seven again. Seven tasks doing real work twice, because the label was written before the work happened and nobody updated it after.
The flag arm fixes five of the seven. Tasks 5 to 9 never start. A second later they still read skipped, and this time the label is true: nothing ran there, nothing will, and a retry runs each of them exactly once. That is the whole win, and it is a real one. Five phantom runs gone, for one line and zero measurable cost.
But look at the last row. Two double-runs survive the flag, and they are tasks 2 and 3, the pair that was mid-flight when task 4 threw. The flag cannot reach them. They are inside an await when stopped flips to true, and the loop condition is only checked after the await resolves. They finish, they write ok into their slots, and the caller that read "skipped" at reject time retries them anyway. In my run they finished 24 and 44 milliseconds after the reject. The window is small. The double-run is not.
This is the part the one-line fix cannot close. The flag answers the question "has a worker picked this up yet" at the moment of the reject, and for the two in-flight tasks the honest answer at that moment is "yes, and it is not done". The array says skipped because nothing has written anything else yet. There are only two ways to tell the truth about those two slots. One is to mark a task "running" when the worker picks it up, so the caller can see the difference between untouched and in-flight and decide what a retry means for each. The other is to wait before retrying: let the stragglers land, read the array again, and retry only what still reads skipped after everything has settled. The first costs a line. The second costs a delay, and the delay is the honest price of a retry you can trust.
So sunnatulla_dev is right, and the flag is worth adding, and it is not the whole fix. Add it. It turns seven double-runs into two, for free. Then mark the in-flight tasks, or wait for them, because the two that survive are the ones doing real work when the batch falls over, and they are the ones a blind retry will run twice. A stopped flag stops the queue. It stops nothing that already started.
P.S. Everything I write stays free — that part doesn't change. But if this one was worth something to you, you can buy me a coffee ☕ (one-time or monthly, both work). It covers the API credits and hosting behind these experiments — and honestly, it's also the nudge that gets the next one started on the evenings I'd otherwise skip it. — Salam


