#15839·Redis

[BUG] SRANDMEMBER key <count> never updates the per-slot allocation accounting, unlike the no-count form (CLUSTER SLOT-STATS memory-bytes grows without bound)

Author: hanke580Created Sep 23, 2026Updated Sep 23, 2026

Describe the bug

srandmemberCommand() (src/t_set.c) brackets its single lookup with the memory-tracking update:

c
    if (server.memory_tracking_enabled)
        oldsize = kvobjAllocSize(set);
    setTypeRandomElement(set, &str, &len, &llele);
    if (server.memory_tracking_enabled)
        updateSlotAllocSize(c->db, getKeySlot(c->argv[1]->ptr), set, oldsize, kvobjAllocSize(set));

srandmemberWithCountCommand() — the branch taken for SRANDMEMBER key <count>, which touches the set far more (setTypeRandomElement() in a loop, setTypeRandomElements(), and a full dict walk in cases 3 and 4) — has no oldsize and no updateSlotAllocSize() anywhere in its ~200 lines.

For a hashtable-encoded set a lookup runs an incremental rehash step; the step that completes the rehash frees the old table and the object shrinks. Nothing records it, and the difference is stranded permanently because dbGenericDelete() later subtracts the object's current size.

To reproduce

Cluster-enabled node (--cluster-enabled yes --cluster-slot-stats-enabled yes, all slots assigned). Each round: 200 × SADD of a 95-byte member (one member per command, so the dict is still rehashing), a few probes, then DEL. DBSIZE is 0 at every measurement:

probe                                        rounds   CLUSTER SLOT-STATS memory-bytes
SRANDMEMBER k      (no count, hashtable)     20       0
SRANDMEMBER k 4    (20 members, listpack)    20       0
SRANDMEMBER k 4    (200 members, hashtable)  1        1024
                                             2        2048
                                             5        5120
                                             20       20480

The window is exactly the rehash: 64 and 128 members leak nothing, 129 leaks on the second probe, 200 on the first, 500 (rehash already finished) nothing.

With the built-in cross-check a single command is enough (standalone suffices, since key-memory-histograms yes also sets memory_tracking_enabled):

redis-server --key-memory-histograms yes --enable-debug-command yes
redis> DEBUG ALLOCSIZE-SLOTS-ASSERT 1
redis> SADD s m00000<90 bytes> ... m00199<90 bytes>    # one SADD per member
redis> SRANDMEMBER s 4
# Guru Meditation: dbgAssertAllocSizePerSlot: slot=0 expected=27724 actual=28748 #db.c:236

A self-checking Python reproducer (exit 1 when present, 0 when fixed; starts its own cluster-enabled redis-server) is attached / available on request. It requires the no-count form over the same set and the count form on a listpack set to both read 0, and the drift to be exactly linear in the number of rounds.

Expected behavior

CLUSTER SLOT-STATS memory-bytes should return to 0 when the slot is empty, as it does after SRANDMEMBER key without a count. Bracket the count path the same way as the no-count path — one capture after the lookup, one updateSlotAllocSize() before each of its returns (there are several, including the count == 0 / empty-set early exits and the four cases):

c
 void srandmemberWithCountCommand(client *c) {
     ...
+    size_t oldsize = 0;
     if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.emptyarray))
         == NULL || checkType(c,set,OBJ_SET)) return;
+    if (server.memory_tracking_enabled)
+        oldsize = kvobjAllocSize(set);
     size = setTypeSize(set);

Additional information

  • Reproduced on unstable @ c3215f01 (REDIS_VERSION 8.9.241, 2026-09-22) and on the released 8.10.2 (498ecd0d); both built from source with make MALLOC=jemalloc. On 8.10.2 the assertion reports #db.c:219.
  • Ubuntu 22.04.2, Linux 5.15.0-187-generic, gcc 11.4.0, x86_64.
  • On unstable: srandmemberWithCountCommand() spans src/t_set.c:1130–1332 and contains no updateSlotAllocSize(); the no-count srandmemberCommand() at :1335 calls it at :1358.
  • Only reachable once the set is past set-max-listpack-entries / set-max-listpack-value and mid-rehash (the window after it crosses a power-of-two size); below either boundary the omission is harmless, which is why it reads as correct.
  • Same accounting scheme, different sites: HGETDEL on a missing field (reported separately) has the same effect through an early return.
  • Searched the tracker for srandmember alloc size, SRANDMEMBER slot-stats, updateSlotAllocSize, ALLOCSIZE-SLOTS-ASSERT, dbgAssertAllocSizePerSlot: the accounting fixes in #14746 (LTRIM/LREM, module string truncate) and #15037 (XINFO argv index) are the closest; nothing about SRANDMEMBER.