Skip to content

Commit 58a7bca

Browse files
committed
prevent memory leak in abort listeners
1 parent c8a1df0 commit 58a7bca

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,17 @@ export class WorkerQueueManager {
101101
// This is required because BLPOP blocks the connection
102102
const blockingClient = this.redis.duplicate();
103103

104+
// Define cleanup outside try so it's accessible in finally
105+
// This prevents listener accumulation on the AbortSignal
106+
const cleanup = signal
107+
? () => {
108+
blockingClient.disconnect();
109+
}
110+
: null;
111+
104112
try {
105113
// Set up abort handler
106-
if (signal) {
107-
const cleanup = () => {
108-
blockingClient.disconnect();
109-
};
114+
if (signal && cleanup) {
110115
signal.addEventListener("abort", cleanup, { once: true });
111116

112117
if (signal.aborted) {
@@ -143,6 +148,11 @@ export class WorkerQueueManager {
143148

144149
throw error;
145150
} finally {
151+
// Always remove the listener to prevent accumulation on the AbortSignal
152+
// (once: true only removes if abort fires, not on normal completion)
153+
if (cleanup && signal) {
154+
signal.removeEventListener("abort", cleanup);
155+
}
146156
await blockingClient.quit().catch(() => {
147157
// Ignore quit errors (may already be disconnected)
148158
});

0 commit comments

Comments
 (0)