Skip to content

Commit 6eed1ee

Browse files
authored
fix(cluster): prevent infinite loop (#3078)
getRandomNode could end up in an infinite loop if this.masters is empty and this.replicas is empty. fixes: #3075
1 parent e2702b6 commit 6eed1ee

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

packages/client/lib/cluster/cluster-slots.spec.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import RedisClusterSlots from './cluster-slots';
55

66
describe('RedisClusterSlots', () => {
77
describe('initialization', () => {
8-
98
describe('clientSideCache validation', () => {
109
const mockEmit = ((_event: string | symbol, ..._args: any[]): boolean => true) as EventEmitter['emit'];
1110
const clientSideCacheConfig = { ttl: 0, maxEntries: 0 };
@@ -45,4 +44,14 @@ describe('RedisClusterSlots', () => {
4544
});
4645
});
4746
});
47+
48+
describe('getRandomNode', ()=> {
49+
it('should not enter infinite loop when no nodes', () => {
50+
const slots = new RedisClusterSlots({
51+
rootNodes: []
52+
}, () => true)
53+
slots.getRandomNode()
54+
slots.getRandomNode()
55+
});
56+
});
4857
});

packages/client/lib/cluster/cluster-slots.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ export default class RedisClusterSlots<
462462
}
463463

464464
*#iterateAllNodes() {
465+
if(this.masters.length + this.replicas.length === 0) return
465466
let i = Math.floor(Math.random() * (this.masters.length + this.replicas.length));
466467
if (i < this.masters.length) {
467468
do {
@@ -542,7 +543,7 @@ export default class RedisClusterSlots<
542543
this.masters[index] :
543544
this.replicas[index - this.masters.length],
544545
client = this.#createClient(node, false);
545-
546+
546547
this.pubSubNode = {
547548
address: node.address,
548549
client,

0 commit comments

Comments
 (0)