Skip to content

Commit 0bbb581

Browse files
UgaTheDevKush Zingade
andauthored
fix(client): terminate scan iterators when the cursor is a Buffer (#3428)
The six `*Iterator` helpers on the client compared the reply cursor against the string `'0'`. Under a Blob String type mapping the cursor comes back as a Buffer, which never equals `'0'`, so every iterator scanned the keyspace forever instead of stopping — and re-issuing SCAN with a `Buffer('0')` cursor restarts the iteration, so the loop never makes progress toward an exit either. Compare by string value, matching the guard the Sentinel `scanIterator` already carries for the same reason. Related to #2879. Signed-off-by: Kush Zingade <[email protected]> Co-authored-by: Kush Zingade <[email protected]>
1 parent 4f2935a commit 0bbb581

2 files changed

Lines changed: 43 additions & 6 deletions

File tree

‎packages/client/lib/client/index.spec.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,41 @@ describe('Client', () => {
10511051
assert.deepEqual(map, results);
10521052
}, GLOBAL.SERVERS.OPEN);
10531053

1054+
testUtils.testWithClient('scan iterators terminate under a Blob String type mapping', async client => {
1055+
// Regression: the iterators compared the reply cursor against the string
1056+
// '0', which a Buffer cursor never equals, so they scanned the keyspace
1057+
// forever. The Sentinel scanIterator already guarded against this.
1058+
const fields: Record<string, string> = {};
1059+
for (let i = 0; i < 100; i++) {
1060+
fields[i.toString()] = i.toString();
1061+
}
1062+
1063+
await client.mSet(Object.entries(fields).flat());
1064+
await client.hSet('hash', fields);
1065+
await client.sAdd('set', Object.keys(fields));
1066+
await client.zAdd('zset', Object.keys(fields).map(value => ({ value, score: 1 })));
1067+
1068+
const buffers = client.withTypeMapping({
1069+
[RESP_TYPES.BLOB_STRING]: Buffer
1070+
});
1071+
1072+
// Bound the page count so an unterminated iterator fails the test instead
1073+
// of hanging it.
1074+
const MAX_PAGES = 200;
1075+
async function drain(name: string, iterator: AsyncIterable<unknown>) {
1076+
let pages = 0;
1077+
for await (const _page of iterator) {
1078+
assert.ok(++pages <= MAX_PAGES, `${name} did not terminate within ${MAX_PAGES} pages`);
1079+
}
1080+
}
1081+
1082+
await drain('scanIterator', buffers.scanIterator({ COUNT: 10 }));
1083+
await drain('hScanIterator', buffers.hScanIterator('hash', { COUNT: 10 }));
1084+
await drain('hScanValuesIterator', buffers.hScanValuesIterator('hash', { COUNT: 10 }));
1085+
await drain('sScanIterator', buffers.sScanIterator('set', { COUNT: 10 }));
1086+
await drain('zScanIterator', buffers.zScanIterator('zset', { COUNT: 10 }));
1087+
}, GLOBAL.SERVERS.OPEN);
1088+
10541089
describe('PubSub', () => {
10551090
testUtils.testWithClient('should be able to publish and subscribe to messages', async publisher => {
10561091
function assertStringListener(message: string, channel: string) {

‎packages/client/lib/client/index.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2024,7 +2024,9 @@ export default class RedisClient<
20242024
const reply = await this.scan(cursor, options);
20252025
cursor = reply.cursor;
20262026
yield reply.keys;
2027-
} while (cursor !== '0');
2027+
// Cursor may be a Buffer when a Blob String type mapping is in use;
2028+
// compare by string value so iteration actually terminates.
2029+
} while (cursor.toString() !== '0');
20282030
}
20292031

20302032
async* hScanIterator(
@@ -2037,7 +2039,7 @@ export default class RedisClient<
20372039
const reply = await this.hScan(key, cursor, options);
20382040
cursor = reply.cursor;
20392041
yield reply.entries;
2040-
} while (cursor !== '0');
2042+
} while (cursor.toString() !== '0');
20412043
}
20422044

20432045
async* hScanValuesIterator(
@@ -2050,7 +2052,7 @@ export default class RedisClient<
20502052
const reply = await this.hScan(key, cursor, options);
20512053
cursor = reply.cursor;
20522054
yield reply.entries.map(entry => entry.value);
2053-
} while (cursor !== '0');
2055+
} while (cursor.toString() !== '0');
20542056
}
20552057

20562058
async* hScanNoValuesIterator(
@@ -2063,7 +2065,7 @@ export default class RedisClient<
20632065
const reply = await this.hScanNoValues(key, cursor, options);
20642066
cursor = reply.cursor;
20652067
yield reply.fields;
2066-
} while (cursor !== '0');
2068+
} while (cursor.toString() !== '0');
20672069
}
20682070

20692071
async* sScanIterator(
@@ -2076,7 +2078,7 @@ export default class RedisClient<
20762078
const reply = await this.sScan(key, cursor, options);
20772079
cursor = reply.cursor;
20782080
yield reply.members;
2079-
} while (cursor !== '0');
2081+
} while (cursor.toString() !== '0');
20802082
}
20812083

20822084
async* zScanIterator(
@@ -2089,7 +2091,7 @@ export default class RedisClient<
20892091
const reply = await this.zScan(key, cursor, options);
20902092
cursor = reply.cursor;
20912093
yield reply.members;
2092-
} while (cursor !== '0');
2094+
} while (cursor.toString() !== '0');
20932095
}
20942096

20952097
async MONITOR(callback: MonitorCallback<TYPE_MAPPING>) {

0 commit comments

Comments
 (0)