p-queue
p-queue copied to clipboard
Abort signals don't remove tasks from the queue
The documentation for the signal option of the add() method is misleading:
AbortSignal for cancellation of the operation. When aborted, it will be removed from the queue and the queue.add() call will reject...
However, when a providing an abort signal, the associated items are not removed from the queue. This is problematic when combined with an intervalCap:
const queue = new PQueue({
concurrency: 1,
interval: 1000,
intervalCap: 1,
});
const controller = new AbortController();
for (let i = 0; i < 10; i++) {
queue.add(() => console.log('foo'), { signal: controller.signal });
}
queue.add(() => console.log('Hello World'));
controller.abort();
In such a scenario I would expect to get "Hello World" logged immediately (or perhaps after 1 second) but instead all 10 of the cancelled tasks remain in the queue and are ejected one by one, causing "Hello World" to print after 10 seconds.
My team also just ran into a situation where we batch up a bunch of stuff into the queue and then sometimes cancel a large portion. The fact that these are still counted towards the intervalCap is having a pretty big impact on the throughput/speed.
Let's get this merged!
In the meantime you can use my fix which I published as @nmann/p-queue
The fix works well, thanks @noahm.
@Richienb @sindresorhus I see the PR #220 has been approved. Hopefully it can be merged & released soon as it's a major improvement.
Thanks for the nudge on publishing! My forked version on npm is now marked as deprecated.