Skip to content

Commit a4c6e83

Browse files
committed
fix leaking event listeners
1 parent e7a5010 commit a4c6e83

File tree

1 file changed

+11
-9
lines changed
  • packages/redis-worker/src/fair-queue

1 file changed

+11
-9
lines changed

packages/redis-worker/src/fair-queue/index.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -788,15 +788,17 @@ export class FairQueue<TPayloadSchema extends z.ZodTypeAny = z.ZodUnknown> {
788788
// When there's work, immediately process the next batch
789789
if (!hadWork) {
790790
await new Promise<void>((resolve, reject) => {
791-
const timeout = setTimeout(resolve, this.consumerIntervalMs);
792-
this.abortController.signal.addEventListener(
793-
"abort",
794-
() => {
795-
clearTimeout(timeout);
796-
reject(new Error("AbortError"));
797-
},
798-
{ once: true }
799-
);
791+
const abortHandler = () => {
792+
clearTimeout(timeout);
793+
reject(new Error("AbortError"));
794+
};
795+
const timeout = setTimeout(() => {
796+
// Must remove listener when timeout fires, otherwise listeners accumulate
797+
// (the { once: true } option only removes on abort, not on timeout)
798+
this.abortController.signal.removeEventListener("abort", abortHandler);
799+
resolve();
800+
}, this.consumerIntervalMs);
801+
this.abortController.signal.addEventListener("abort", abortHandler, { once: true });
800802
});
801803
}
802804
}

0 commit comments

Comments
 (0)