From 034d57190b2cd0f9a885a6908afd61a702c87c94 Mon Sep 17 00:00:00 2001 From: Madelyn Olson Date: Wed, 14 Oct 2026 08:21:20 -0700 Subject: [PATCH] Skip expired listpack hash fields in ValkeyModule_ScanKey Since listpack hashes can carry field TTLs, a field that has expired but not yet been reaped by active expire stays in the listpack. HGETALL, HSCAN and the hashtable branch of ScanKey all hide it, but the listpack branches of ValkeyModule_ScanKey and ValkeyModule_ScanKeyRawBorrowed delivered it to the module callback. Apply the same visibility check HSCAN uses to both listpack loops. Signed-off-by: Madelyn Olson --- src/module.c | 11 +++++++++++ tests/unit/moduleapi/scan.tcl | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/module.c b/src/module.c index 5fa5ab096..7bf403080 100644 --- a/src/module.c +++ b/src/module.c @@ -12326,6 +12326,12 @@ int VM_ScanKey(ValkeyModuleKey *key, ValkeyModuleScanCursor *cursor, ValkeyModul robj *field = (vstr != NULL) ? createStringObject((char *)vstr, vlen) : createStringObjectFromLongLongWithSds(vll); p = lpNext(objectGetVal(o), p); + /* Skip fields not visible in the current context, like HSCAN. */ + if (!hashTypeListpackFieldIsValid(hashTypeListpackGetExpiry(objectGetVal(o), p))) { + decrRefCount(field); + p = lpNext(objectGetVal(o), p); + continue; + } vstr = lpGetValue(p, &vlen, &vll); robj *value = (vstr != NULL) ? createStringObject((char *)vstr, vlen) : createStringObjectFromLongLongWithSds(vll); @@ -12483,6 +12489,11 @@ int VM_ScanKeyRawBorrowed(ValkeyModuleKey *key, ValkeyModuleScanCursor *cursor, } p = lpNext(lp, p); if (!p) break; + /* Skip fields not visible in the current context, like HSCAN. */ + if (!hashTypeListpackFieldIsValid(hashTypeListpackGetExpiry(lp, p))) { + p = lpNext(lp, p); + continue; + } unsigned int vlen; long long vll; char vbuf[LONG_STR_SIZE]; diff --git a/tests/unit/moduleapi/scan.tcl b/tests/unit/moduleapi/scan.tcl index 89c954437..6426b3548 100644 --- a/tests/unit/moduleapi/scan.tcl +++ b/tests/unit/moduleapi/scan.tcl @@ -24,6 +24,24 @@ start_server {tags {"modules"}} { lsort [r scan.scan_key hh1] } {{f1 1}} + test {Module scan hash listpack skips expired fields} { + r debug set-active-expire 0 + r del hhexp + r hset hhexp f1 v1 f2 v2 + r hpexpire hhexp 1 FIELDS 1 f1 + wait_for_condition 50 10 { + [r hgetall hhexp] eq {f2 v2} + } else { + fail "field f1 did not expire" + } + # f1 is expired but not reaped + assert_equal 2 [r hlen hhexp] + assert_encoding listpack hhexp + set res [lsort [r scan.scan_key hhexp]] + r debug set-active-expire 1 + set res + } {{f2 v2}} {needs:debug} + test {Module scan hash dict} { r config set hash-max-ziplist-entries 2 r hmset hh3 f1 v1 f2 v2 f3 v3 @@ -83,6 +101,24 @@ start_server {tags {"modules"}} { lsort [r scan.scan_key_raw rh1] } {{f1 1}} + test {Module scan_key_raw hash listpack skips expired fields} { + r debug set-active-expire 0 + r del rhexp + r hset rhexp f1 v1 f2 v2 + r hpexpire rhexp 1 FIELDS 1 f1 + wait_for_condition 50 10 { + [r hgetall rhexp] eq {f2 v2} + } else { + fail "field f1 did not expire" + } + # f1 is expired but not reaped + assert_equal 2 [r hlen rhexp] + assert_encoding listpack rhexp + set res [lsort [r scan.scan_key_raw rhexp]] + r debug set-active-expire 1 + set res + } {{f2 v2}} {needs:debug} + test {Module scan_key_raw hash dict} { r del rh3 r hmset rh3 f1 v1 f2 v2 f3 v3