Skip to content

Commit dca7a2c

Browse files
ForestDengHKclaude
andcommitted
Fix Redis SCAN infinite loop - use string cursor comparison
Root cause of 997K Redis reads in ~2 hours: - Upstash Redis returns cursor as STRING to safely handle large numbers that exceed JavaScript's Number.MAX_SAFE_INTEGER (2^53) - Previous code compared number cursor to number 0 - Large cursor values could lose precision when converted to Number, causing cursor !== 0 to always be true (infinite loop) Fix: - Keep cursor as string, compare to string "0" - Maintain MAX_ITERATIONS safeguard as backup protection - Pass Number(cursor) to scan() as required by API Reference: redis/node-redis#2879 Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 487aa6c commit dca7a2c

3 files changed

Lines changed: 20 additions & 16 deletions

File tree

‎src/db/index.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,20 @@ export async function getAllCreaturesWithLikes(): Promise<Array<{
6666
try {
6767
const redis = getRedis();
6868

69-
// Scan for all like count keys - with safeguard against infinite loops
69+
// Scan for all like count keys - with safeguards against infinite loops
70+
// IMPORTANT: Upstash returns cursor as STRING to handle large numbers
71+
// that exceed JavaScript's Number.MAX_SAFE_INTEGER
7072
const results: Array<{ creatureId: string; totalLikes: number; ips: string[] }> = [];
71-
let cursor: number = 0;
73+
let cursor = "0"; // Start with string "0"
7274
let iterations = 0;
73-
const MAX_ITERATIONS = 10; // Safeguard
75+
const MAX_ITERATIONS = 10; // Safeguard against infinite loops
7476

7577
do {
76-
const scanResult = await redis.scan(cursor, {
78+
const scanResult = await redis.scan(Number(cursor), {
7779
match: 'creature:*:likes:count',
7880
count: 100
7981
});
80-
cursor = Number(scanResult[0]); // Ensure it's a number
82+
cursor = String(scanResult[0]); // Keep as string for safe comparison
8183
const keys = scanResult[1];
8284

8385
for (const key of keys) {
@@ -102,7 +104,7 @@ export async function getAllCreaturesWithLikes(): Promise<Array<{
102104
console.warn('Likes scan loop hit max iterations, breaking');
103105
break;
104106
}
105-
} while (cursor !== 0);
107+
} while (cursor !== "0"); // Compare string to string "0"
106108

107109
// Sort by likes descending
108110
results.sort((a, b) => b.totalLikes - a.totalLikes);

‎src/pages/admin/comments.astro‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,27 @@ async function getAllComments(): Promise<Comment[]> {
2424
const redis = getRedis();
2525
const comments: Comment[] = [];
2626
27-
// Scan for all comment sorted set keys - with safeguard against infinite loops
28-
let cursor: number = 0;
27+
// Scan for all comment sorted set keys - with safeguards against infinite loops
28+
// IMPORTANT: Upstash returns cursor as STRING to handle large numbers
29+
let cursor = "0"; // Start with string "0"
2930
const commentKeys: string[] = [];
3031
let iterations = 0;
3132
const MAX_ITERATIONS = 10; // Safeguard
3233
3334
do {
34-
const scanResult = await redis.scan(cursor, {
35+
const scanResult = await redis.scan(Number(cursor), {
3536
match: '*:comments',
3637
count: 100
3738
});
38-
cursor = Number(scanResult[0]); // Ensure it's a number
39+
cursor = String(scanResult[0]); // Keep as string for safe comparison
3940
const keys = scanResult[1];
4041
commentKeys.push(...keys);
4142
iterations++;
4243
if (iterations >= MAX_ITERATIONS) {
4344
console.warn('Comments scan loop hit max iterations, breaking');
4445
break;
4546
}
46-
} while (cursor !== 0);
47+
} while (cursor !== "0"); // Compare string to string "0"
4748
4849
// Process each comment key
4950
for (const key of commentKeys) {

‎src/pages/admin/index.astro‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ try {
2424
topCreature = { name: creature?.name || top.creatureId, likes: top.totalLikes };
2525
}
2626
27-
// Count comments - with safeguard against infinite loops
27+
// Count comments - with safeguards against infinite loops
28+
// IMPORTANT: Upstash returns cursor as STRING to handle large numbers
2829
const redis = getRedis();
29-
let cursor: number = 0;
30+
let cursor = "0"; // Start with string "0"
3031
let iterations = 0;
3132
const MAX_ITERATIONS = 10; // Safeguard
3233
do {
33-
const scanResult = await redis.scan(cursor, { match: '*:comments', count: 100 });
34-
cursor = Number(scanResult[0]); // Ensure it's a number
34+
const scanResult = await redis.scan(Number(cursor), { match: '*:comments', count: 100 });
35+
cursor = String(scanResult[0]); // Keep as string for safe comparison
3536
for (const key of scanResult[1]) {
3637
const count = await redis.zcard(key);
3738
totalComments += count;
@@ -41,7 +42,7 @@ try {
4142
console.warn('Scan loop hit max iterations, breaking');
4243
break;
4344
}
44-
} while (cursor !== 0);
45+
} while (cursor !== "0"); // Compare string to string "0"
4546
} catch (e) {
4647
console.error('Error loading stats:', e);
4748
}

0 commit comments

Comments
 (0)