Redesign: allow deletions of empty containers from locked buckets
Problem:
Multiple code paths in Dragonfly iterate DenseSet-based containers (StringMap for hashes, StringSet for sets) under a bucket lock during:
- RDB snapshot serialization (
src/server/rdb_save.cc) - Cluster migration serialization (
src/server/journal/cmd_serializer.cc) - Search index document loading (
src/server/search/doc_index.cc) - DEBUG commands traversing the prime table (
src/server/debugcmd.cc)
Lazy field/member expiry during iteration can empty the container. Cleaning up the now-empty key requires FindMutable on the same bucket, which deadlocks because the bucket is already locked by the iterating caller:
F synchronization.cc:26] Check failed: FiberActive() != locked_fiber_A subsequent SAVE on the empty container crashes on LOG(DFATAL) in rdb_save.cc:274 because OBJ_HASH does not permit empty values.
Current workaround:
Disable lazy expiry before iteration with set_time(0), restore afterwards:
if (pv.Encoding() == kEncodingStrMap2) {
sm = static_cast<StringMap*>(pv.RObjPtr());
sm->set_time(0);
}
// ... iterate ...
if (sm)
sm->set_time(MemberTimeSeconds(GetCurrentTimeMs()));Applied in:
rdb_save.cc::SaveHSetObject/SaveSetObject(pre-existing)cmd_serializer.cc::SerializeHash/SerializeSet(#7167)
Other callers (search, DEBUG, SORT, ZUNIONSTORE, FIELDEXPIRE, HTTL, SHRINK) use a different workaround: perform the cleanup outside the locked section (DeleteIfEmpty after Iterate returns).
Downsides of workarounds:
- Stale time state: between
set_time(0)and restore, the DenseSet reports non-expired entries to any other code path touching it - Expired entries serialized to destination: during migration, expired members/fields are sent to the destination node and only expire lazily there
- Every new caller must remember the pattern: easy to miss, leading to new zombie-crash bugs
- N separate PRs for the same class of bug
Proposed solution: Introduce a mechanism to queue deletions for keys that become empty under a bucket lock. The queue is drained after the lock is released, through the journal, so replicas and persistence stay consistent.
Benefits:
- Single generic mechanism replaces every per-caller workaround
- Lazy expiry stays enabled -> serialization reflects the correct current state
- Makes this class of bug impossible by construction for new callers
- Obsoletes the workarounds
Related:
- #7133 — original zombie-hash fuzz crash
- PRs individual workarounds: https://github.com/dragonflydb/dragonfly/pull/7167 https://github.com/dragonflydb/dragonfly/pull/7166 https://github.com/dragonflydb/dragonfly/pull/7165 https://github.com/dragonflydb/dragonfly/pull/7164 https://github.com/dragonflydb/dragonfly/pull/7163 https://github.com/dragonflydb/dragonfly/pull/7162 https://github.com/dragonflydb/dragonfly/pull/7161 https://github.com/dragonflydb/dragonfly/pull/7160 https://github.com/dragonflydb/dragonfly/pull/7159 https://github.com/dragonflydb/dragonfly/pull/7158
Source: dragonflydb/dragonfly