Skip to content
This repository was archived by the owner on Oct 25, 2026. It is now read-only.

Skip expired listpack hash fields in ValkeyModule_ScanKey - #92

Closed
madolson wants to merge 1 commit into
upstream-unstable-8f67ab136from
fix-module-scankey-listpack-hash-expired
Closed

madolson wants to merge 1 commit into
upstream-unstable-8f67ab136from
fix-module-scankey-listpack-hash-expired

Conversation

@madolson

Copy link
Copy Markdown
Owner

Since valkey-io/valkey#3212, a listpack hash can hold a field that has expired but has not yet been reaped by active expire. HGETALL, HSCAN, and the hashtable branch of ValkeyModule_ScanKey all hide that field, but the listpack branches of ValkeyModule_ScanKey and ValkeyModule_ScanKeyRawBorrowed pass it to the module callback. A module that indexes hashes through ScanKey, such as valkey-search, sees data the client can no longer read. The fix applies the visibility check HSCAN already uses to both listpack loops.

AI generated details

Based on valkey-io/valkey unstable at 8f67ab136, so the base branch here is upstream-unstable-8f67ab136.

Problem

valkey-io/valkey#3212 (c80da01) stores a field's TTL as a metadata entry after its value in the listpack. Readers have to check it explicitly with hashTypeListpackFieldIsValid(). hashTypeNext() (t_hash.c:900) and HSCAN (db.c:1391) do. The hashtable branch of ScanKey gets the same filtering from hashtableScan's validateEntry callback.

The listpack loops in VM_ScanKey (module.c:12320) and VM_ScanKeyRawBorrowed (module.c:12469, added in valkey-io/valkey#4403) walk field/value pairs with lpNext(). lpNext() skips over metadata entries, so neither loop ever reads the expiry.

Both commits are in 9.2.0-rc1 only.

The existing scan.tcl tests never set a field TTL, so nothing exercised this.

Case, at base 8f67ab136 ScanKey ScanKeyRawBorrowed After fix
hashtable hash, one field expired hidden hidden unchanged
listpack hash, one field expired returned returned hidden
listpack hash, every field expired (key still exists) returned returned empty
listpack hash, integer field expired returned returned hidden
listpack hash, TTL in the future returned returned unchanged
listpack zset no metadata no metadata unchanged

Fix

Once each loop reaches the value entry, it checks hashTypeListpackFieldIsValid(hashTypeListpackGetExpiry(lp, vptr)) and skips the pair if the field is not visible. This is the same check HSCAN uses, so expiration policy (POLICY_IGNORE_EXPIRE during loading, on replicas, and so on) behaves the same way on both encodings. Zset listpacks carry no metadata, so hashTypeListpackGetExpiry() returns EXPIRY_NONE for them and they are never skipped.

Alternative considered

Rewrite the hash case on top of hashTypeInitIterator()/hashTypeNext(), which already filter. That would split the loop, which is shared with listpack zsets, and the RawBorrowed variant would still have to unpack integer-encoded entries on its own. Two added checks are a smaller cherry-pick into 9.2.

Testing

The new tests in tests/unit/moduleapi/scan.tcl turn off active expire, let one field of a two-field listpack hash expire, and confirm it was not reaped (HLEN is still 2, encoding is still listpack). With the module.c hunks reverted:

*** [err]: Module scan hash listpack skips expired fields in tests/unit/moduleapi/scan.tcl
Expected '{f2 v2}' to equal or match '{f1 v1} {f2 v2}'
*** [err]: Module scan_key_raw hash listpack skips expired fields in tests/unit/moduleapi/scan.tcl
Expected '{f2 v2}' to equal or match '{f1 v1} {f2 v2}'

Removing either guard on its own fails only the test for that function.

This was generated by AI but verified, with love, by a human.

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 <madelyneolson@gmail.com>
@madolson madolson added the promoted Change has been promoted upstream label Oct 25, 2026
@madolson madolson closed this Oct 25, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

promoted Change has been promoted upstream

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant