p-queue icon indicating copy to clipboard operation
p-queue copied to clipboard

Abort signals don't remove tasks from the queue

Open noahm opened this issue 9 months ago • 2 comments
trafficstars

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.

noahm avatar Jan 26 '25 06:01 noahm

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!

jessevdp avatar Mar 11 '25 13:03 jessevdp

In the meantime you can use my fix which I published as @nmann/p-queue

noahm avatar Mar 11 '25 14:03 noahm

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.

alexp3y avatar Sep 07 '25 14:09 alexp3y

Thanks for the nudge on publishing! My forked version on npm is now marked as deprecated.

noahm avatar Sep 07 '25 20:09 noahm