[BUG] KEYDB.NHSET and KEYDB.HRENAME modify a key without signalling it: WATCH never aborts, and the dataset is not marked dirty
Describe the bug
KEYDB.NHSET and KEYDB.HRENAME write to the keyspace but never signal that they did. Neither calls
signalModifiedKey, notifyKeyspaceEvent or increments the dirty counter. Two visible
consequences:
1. WATCH does not see the write, so EXEC commits on a key that changed.
WATCH k -> OK
KEYDB.NHSET k f v -> 0 k is created
MULTI -> OK
EXISTS k -> QUEUED
EXEC -> 1) (integer) 1 expected: (nil)
HSET k f v -> 1
WATCH k -> OK
KEYDB.HRENAME k f g -> 1 the hash is modified
MULTI -> OK
HGETALL k -> QUEUED
EXEC -> 1) 1) "g" 2) "v" expected: (nil)
The same commands' ordinary counterparts behave correctly — HSET, SET and DEL on a watched key
all make EXEC reply (nil) — so any check-and-set built on WATCH silently loses its guarantee as
soon as one writer uses the KEYDB.* commands.
2. The write is not counted as a change to the dataset.
rdb_changes_since_last_save does not move for either command, while it moves for every ordinary
write:
| command | delta of rdb_changes_since_last_save |
|---|---|
KEYDB.NHSET n f v |
0 |
KEYDB.HRENAME hr f g |
0 |
HSET h f v |
1 |
SET s 1 |
1 |
That counter is server.dirty, which is also what decides whether a command is propagated to
replicas and the AOF. We have not set up a replica to confirm the propagation half, so please treat
that as the inference it is — but the counter itself is measured above, and the code path (below) has
no dirty increment at all.
To reproduce
docker run -d -p 6402:6379 eqalpha/keydb:x86_64_v6.3.4
From a clean keyspace (FLUSHALL), on one connection:
WATCH k
KEYDB.NHSET k f v
MULTI
EXISTS k
EXEC -> 1) (integer) 1 expected (nil)
and for the dirty counter:
INFO persistence (note rdb_changes_since_last_save)
KEYDB.NHSET n f v
INFO persistence (unchanged)
HSET h f v
INFO persistence (+1)
Each row below is an independent sequence from a clean keyspace.
| case | expected EXEC |
KeyDB 6.3.4 |
|---|---|---|
WATCH k; KEYDB.NHSET k f v; MULTI; EXISTS k; EXEC |
(nil) |
[1] |
WATCH k; KEYDB.HRENAME k f g; MULTI; HGETALL k; EXEC |
(nil) |
[[g, v]] |
control: WATCH k; HSET k f v; … EXEC |
(nil) |
(nil) |
control: WATCH k; SET k v; … EXEC |
(nil) |
(nil) |
control: WATCH k; DEL k; … EXEC |
(nil) |
(nil) |
A self-contained Python 3 reproducer (standard library only) that runs the WATCH cases and measures
the dirty counter is attached as repro.py; it exits 0 when the bug reproduces.
Expected behavior
EXEC should reply (nil) in both cases, and the dirty counter should advance. The
transactions documentation
states the rule with no exemption for custom commands:
WATCHed keys are monitored in order to detect changes against them. If at least one watched key is modified before the EXEC command, the whole transaction aborts, and EXEC returns a Null reply to notify that the transaction failed.
…we are asking Redis to perform the transaction only if none of the WATCHed keys were modified. This includes modifications made by the client, like write commands, and by Redis itself, like expiration or eviction.
Additional information
Version and environment. KeyDB 6.3.4, from the published container image
eqalpha/keydb:x86_64_v6.3.4
(digest sha256:eceb1806730c7850395b8262300182c2e15a6e5dacbf0b72cbab110518caf43f); INFO server
reports redis_version:6.3.4. Source read at 7e7e5e57d25fe246a8201f0acf5e7363c0bf1e14 (tag
v6.3.4). Host: Ubuntu 24.04.5 LTS, kernel 7.0.0-38-generic (x86_64), Docker. Default
configuration, single node, no replication.
Potential Cause. Compare an ordinary hash write — hsetCommand in src/t_hash.cpp ends with all three
calls:
addReplyLongLong(c, created);
...
signalModifiedKey(c,c->db,c->argv[1]);
notifyKeyspaceEvent(NOTIFY_HASH,"hset",c->argv[1],c->db->id);
g_pserver->dirty += (c->argc - 2)/2;
}
with hrenameCommand (src/t_hash.cpp:969-997), which ends right after the reply:
hashTypeDelete(o, szFromObj(c->argv[2]));
hashTypeSet(o, szFromObj(c->argv[3]), sdsT, HASH_SET_TAKE_VALUE);
addReplyLongLong(c, 1);
}
and nhsetCommand (src/t_nhash.cpp:338-365), which delegates to setWithKey and then returns:
try {
if (setWithKey(c->db, c->argv[1], val, true)) {
addReplyLongLong(c, 1); // we replaced a value
} else {
addReplyLongLong(c, 0); // we added a new value
}
} catch (...) { ... }
setWithKey (src/t_nhash.cpp:127) walks the dotted key and mutates the dict directly
(di.setval(val) and friends). Neither it nor nhsetCommand calls signalModifiedKey,
notifyKeyspaceEvent or touches g_pserver->dirty; hrenameCommand does not either. Since
signalModifiedKey is what flips watched_keys_modified for watchers, WATCH cannot see these
writes.
Suggested fix. Add the three calls the ordinary commands make, at the end of each command once the
write has succeeded — for hrenameCommand:
addReplyLongLong(c, 1);
signalModifiedKey(c,c->db,c->argv[1]);
notifyKeyspaceEvent(NOTIFY_HASH,"hrename",c->argv[1],c->db->id);
g_pserver->dirty++;
and the equivalent in nhsetCommand (keyspace event name as you prefer). It is worth auditing the
other KEYDB.* commands in the same files for the same omission; we only tested these two, which are
the two we could reach from a plain client.