Skip to content

Commit b5da02d

Browse files
authored
fix: cleanup error handler to prevent memory leak (socketio#490)
1 parent e70b1bd commit b5da02d

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

lib/index.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export class RedisAdapter extends Adapter {
106106
private requests: Map<string, Request> = new Map();
107107
private ackRequests: Map<string, AckRequest> = new Map();
108108
private redisListeners: Map<string, Function> = new Map();
109+
private readonly friendlyErrorHandler: () => void;
109110

110111
/**
111112
* Adapter constructor.
@@ -183,16 +184,13 @@ export class RedisAdapter extends Adapter {
183184
);
184185
}
185186

186-
const registerFriendlyErrorHandler = (redisClient) => {
187-
redisClient.on("error", () => {
188-
if (redisClient.listenerCount("error") === 1) {
189-
console.warn("missing 'error' handler on this Redis client");
190-
}
191-
});
187+
this.friendlyErrorHandler = function () {
188+
if (this.listenerCount("error") === 1) {
189+
console.warn("missing 'error' handler on this Redis client");
190+
}
192191
};
193-
194-
registerFriendlyErrorHandler(this.pubClient);
195-
registerFriendlyErrorHandler(this.subClient);
192+
this.pubClient.on("error", this.friendlyErrorHandler);
193+
this.subClient.on("error", this.friendlyErrorHandler);
196194
}
197195

198196
/**
@@ -981,6 +979,9 @@ export class RedisAdapter extends Adapter {
981979
this.redisListeners.get("messageBuffer")
982980
);
983981
}
982+
983+
this.pubClient.off("error", this.friendlyErrorHandler);
984+
this.subClient.off("error", this.friendlyErrorHandler);
984985
}
985986
}
986987

0 commit comments

Comments
 (0)