Skip to content

[8.6-rse] [MOD-17512] Fix unloadable RDB from leftover pre-2.0 index keys breaking replication, backups and exports - #10938

Merged
dor-forer merged 1 commit into
8.6-rsefrom
backport-agent/pr-10817-to-8.6-rse
Aug 16, 2026
Merged

dor-forer merged 1 commit into
8.6-rsefrom
backport-agent/pr-10817-to-8.6-rse

Conversation

@redisearch-backport-pull-request

@redisearch-backport-pull-request redisearch-backport-pull-request Bot commented Aug 16, 2026 •

Copy link
Copy Markdown
Contributor

Backport of #10817 to 8.6-rse.

Original PR

Cherry-pick result

Resolved 1 conflict(s) — see Conflict Log below.

Conflict Log

tests/cpptests/test_cpp_rdb.cpp:921-1005

  • Conflict: The source side appended both an unrelated HNSW RERANK round-trip test and this PR’s legacy-type serialization tests, while the release branch ended earlier.
  • Why it conflicted: The HNSW RERANK test landed on master after 8.6-rse diverged, making the append location part of the cherry-pick context.
  • Resolution: Kept only the two legacy-type tests added by PR [MOD-17512] Fix unloadable RDB from leftover pre-2.0 index keys breaking replication, backups and exports #10817 and dropped the unrelated HNSW RERANK test.
  • Rationale: This preserves the original backport’s serialization coverage without importing another PR’s feature-specific test into 8.6-rse.

Release notes

  • This PR requires release notes
  • This PR does not require release notes

🤖 Generated by the auto-backport workflow.

…ing replication, backups and exports (#10817)

* Fix legacy type dummy values RDB serialization

* [MOD-15685] Harden legacy type persistence and cover it with tests

Follow-up on the initial zero-byte-save fix:

- Use RS_ASSERT_ALWAYS, not RS_ASSERT, in the three legacy rdb_save callbacks.
  Writing an empty payload over a value that is not the sentinel would silently
  discard real data, so release builds must crash rather than corrupt. The
  original bug existed precisely because RS_ABORT compiles to a no-op without
  ENABLE_ASSERT; repeating that pattern on the same path would hide the next one.

- Give the legacy types their own no-op aof_rewrite handler. The shared
  GenericAofRewrite_DisabledHandler calls abort(), so AOF rewrite crashed any
  shard still holding legacy keys. A legacy key carries no data, so emitting no
  commands is both safe and desirable: the husk does not come back on replay.

- Drop the now-unused util/misc.h include.

- test_cpp_rdb: round-trip each legacy type's dummy value through the mock RDB
  IO, asserting the payload is non-empty, the loader returns the sentinel, no IO
  error is raised, and the loader consumes exactly the bytes written. Fails on a
  zero-byte save. Numeric is also covered under the v0 framing, since its loader
  selects the wire format from encver.

- test_rdb_compatibility: assert the upgrade sweep leaves no legacy module-type
  keys behind, and dump-and-reload each upgraded fixture so the save path is
  exercised and not just the load path.

Co-Authored-By: Claude Opus 5 <[email protected]>

* [MOD-15685] Harden legacy loaders against untrusted payloads

These loaders are reachable from RESTORE, so every count they read is
attacker-controlled. Once the stream is exhausted the Load* calls return 0
without consuming anything, so a counted loop kept iterating the declared
number of times: a truncated payload declaring UINT64_MAX entries spun the
server. numericdx v0, ft_invidx block counts and ft_tagidx tag counts all had
this shape.

- Stop every counted loop on RedisModule_IsIOError, and return NULL on error so
  a corrupt payload cannot materialize a key.
- Add the missing encver guard to TagIndex_RdbLoad_Consume. Redis matches a
  module type on its 54-bit signature and ignores the 10-bit encoding version,
  so a crafted RESTORE could reach it with any encver.
- Propagate the nested InvertedIndex_RdbLoad_Consume failure in the tag loader
  instead of discarding it.
- Tests: truncated-huge-count and unknown-encver rejection for all three types.
- Document why the AOF handler emits nothing: a replayable RESTORE would need a
  full DUMP envelope that no module API can build, and deleting the key is not
  possible from the forked AOF child. The resulting command-AOF vs RDB-preamble
  divergence is noted in the code.

Co-Authored-By: Claude Opus 5 <[email protected]>

* [MOD-15685] Make legacy keys survive AOF rewrite instead of dropping them

The previous change emitted no commands, on the mistaken premise that
recreating the key was impossible. It is not: DUMP produces the envelope, so
the module never has to build one. deps/rmutil/util.c has done this for years
in RMUtil_DefaultAofRewrite, which is dead code here.

Emit DUMP -> RESTORE so a command-only AOF and an AOF with an RDB preamble
agree: both keep the key. Without this, whether the key survives depends on
which AOF format is in use, so a live replica and a pure-AOF-restarted master
diverge on EXISTS/TYPE/DBSIZE/TTL.

Select the IO's database first. A detached thread-safe context starts on DB 0,
which is why RMUtil_DefaultAofRewrite cannot be reused verbatim.

Also corrects the reasoning in the previous commit message: the shared
GenericAofRewrite_DisabledHandler calls abort() inside the forked rewrite
child, so it kills that child and leaves the AOF unable to rewrite - it does
not crash the shard parent.

Co-Authored-By: Claude Opus 5 <[email protected]>

* [MOD-15685] Add real-Redis coverage and harden the AOF callback

The C++ tests drive the callbacks against a mock whose framing is not Redis's,
so they prove save/load symmetry but not that the bytes we emit are valid Redis
framing. These tests cross that boundary - Redis validates the module type id,
the module EOF marker, the DUMP footer and the CRC for us.

tests/pytests/test_legacy_module_types.py builds RESTORE payloads from first
principles, since a legacy key can only be created by deserializing one. That is
also how these keys reach production: Enterprise import forwards RESTORE rather
than handing an RDB to Redis. testDumpPayloadHelperMatchesRedis guards the
builder by checking our crc64 against a footer the server wrote, so a broken
fixture fails one obvious test instead of all of them confusingly.

Covered: round-trip through RESTORE/DUMP/RESTORE and a reload; malformed counts
and unknown encvers rejected without creating a key; survival of an AOF rewrite
under the command-only format, which is the only format that runs the
aof_rewrite callback; and a DB 1 key with a colliding DB 0 name, which fails if
the callback forgets to select the IO's database.

Two fixes in that callback:

- Assert RedisModule_SelectDb succeeded. Silently staying on DB 0 would dump a
  same-named, unrelated key.
- Treat a NULL DUMP reply as an expired key and skip it, but fail the rewrite on
  any other reply. Warning and emitting nothing would install an AOF base
  missing the key, which is the divergence this callback exists to prevent;
  failing leaves Redis's previous base valid.

Also record why there is no ThreadSafeContextLock here: the rewrite child
inherits the module GIL already held.

Co-Authored-By: Claude Opus 5 <[email protected]>

* [MOD-15685] Address review on the AOF rewrite and its tests

- Add the mandatory license header to the new test file.
- Pass the RESTORE TTL as 0LL. EmitAOF's "l" specifier consumes a long long, so
  a bare 0 is undefined behaviour on any ABI that lays variadic int and long
  long out differently.
- Stop using waitForRdbSaveToFinish for AOF rewrites. It polls only
  rdb_bgsave_in_progress, so it returned immediately and DEBUG LOADAOF could
  replay a rewrite that was still running or merely scheduled - the two AOF
  tests were passing without ever waiting. Poll aof_rewrite_in_progress and
  aof_rewrite_scheduled instead, and parse INFO from the dict redis-py's own
  response callback returns.
- Map a unix-socket pool's `path` onto the client's `unix_socket_path`, so the
  binary connection helper also works under UNIX=1.

Co-Authored-By: Claude Opus 5 <[email protected]>

* [MOD-15685] Narrow this PR to the serialization fix

Move the loader hardening out. It fixed a real, separately reachable CPU denial
of service - the three loaders trust attacker-controlled counts and a truncated
payload declaring UINT64_MAX entries spins the server - but that is a distinct
defect from the zero-byte save this PR exists to fix, and it deserves its own
review rather than riding along.

Reverted here: the RedisModule_IsIOError loop guards, the NULL-on-error returns,
the tag loader's missing encver guard, and the propagation of its nested
inverted-index failure, together with their four tests. The loaders are now
byte-identical to master.

Kept: the three per-type rdb_save callbacks that make a leftover legacy key
serialize to something its own loader accepts, the DUMP -> RESTORE AOF rewrite
that replaces a handler which aborts the rewrite child, and the round-trip
coverage at both the callback and real-Redis layers.

The DoS remains open on master until the hardening lands separately; the work is
preserved on dor-mod15685-loader-hardening.

Co-Authored-By: Claude Opus 5 <[email protected]>

* [MOD-15685] Make the AOF tests fail when the rewrite itself fails

Waiting for the activity flags to go idle also succeeds after a *failed* rewrite.
DEBUG LOADAOF then replays the previous AOF generation - which in these tests
still contains the RESTORE commands that created the keys - so the assertions
passed regardless of what LegacyType_AofRewrite emitted.

Check two things instead, because neither is sufficient alone:

- aof_rewrites increments at rewrite start, just before Redis forks, so a change
  in it proves a rewrite actually began. The flags alone can read idle before it
  starts.
- aof_last_bgrewrite_status is the only signal that it succeeded.

Folding BGREWRITEAOF into the helper so the baseline count cannot be missed.

Co-Authored-By: Claude Opus 5 <[email protected]>

* [MOD-15685] Write legacy husks with an empty-payload encoding version

Per review: instead of fabricating the smallest payload each loader happens to
accept, say what is actually true - there is no payload - and stamp the record
with a version the loader recognises.

LEGACY_EMPTY_ENC_VER 2 is now the registered version for ft_invidx, numericdx and
ft_tagidx, so records we write carry it. GenericType_EmptyRdbSave writes nothing,
and each loader returns the sentinel immediately when it sees that version,
consuming no bytes - which leaves Redis's module EOF marker exactly where it left
it. Writing zero bytes under version 1 is precisely what corrupted the RDB: the
loader then read a payload that was not there and ate the marker instead.

Replaces three per-type savers with one, and removes the invented
flags/lastId/numDocs/n_blocks zeros.

Loading genuine 1.x records is unchanged. Redis passes the record's own encoding
version to rdb_load with no ceiling check, so versions 0 and 1 still take the
existing consume paths; a new test pins that.

One consequence, deliberately accepted: an RDB written by this build carries
version 2 for these types, and an older build's loader rejects anything above
version 1, so it will refuse to load it. That matters only for reading new data
with an older module - downgrade, a mixed-version replica, or restoring a new
backup on an old build. The previous approach was interoperable both ways because
it wrote a valid version 1 payload.

Co-Authored-By: Claude Opus 5 <[email protected]>

* [MOD-15685] Reduce to the minimum change

Keep GenericType_DummyRdbSave under its own name. Renaming it was an artifact of
the earlier design, where each type wrote a different fabricated payload and so
needed its own saver. With LEGACY_EMPTY_ENC_VER all three write the same thing -
nothing - so the original function is exactly right; only its body was ever wrong.
The registration lines go back to being untouched.

Drop LegacyType_AofRewrite and restore GenericAofRewrite_DisabledHandler, with its
tests and helpers. Note what that gives up: that handler calls abort() inside the
forked AOF rewrite child, so a database holding one of these keys still cannot
complete an AOF rewrite and its AOF grows without bound. Separable from the RDB
corruption this PR fixes, and now left for its own change.

src/legacy_types.c is down to 25 insertions and 4 deletions: the new version
constant, one line inside the saver, an early return in each of the three loaders,
and the three registered versions.

Co-Authored-By: Claude Opus 5 <[email protected]>

* [MOD-15685] Address review: debug-only assert, and guard ordering

Two review points from @GuyAv46.

Use RS_ASSERT rather than RS_ASSERT_ALWAYS in the saver. The _ALWAYS form was
justified when RS_ABORT compiling away *was* the bug - the function then wrote
nothing, and writing nothing was wrong. Now writing nothing is correct, so the
assertion is not load-bearing: it only guards a value that the loaders never
produce. Crashing a production shard for that is the wrong trade.

Put the error path first in all three loaders and track the latest encoding
version. The previous order worked but left a guard reading "reject anything above
version 1" while version 2 was valid - the same kind of stale-constant trap that
caused this bug. TagIndex_RdbLoad_Consume had no ceiling guard at all and now gets
the same shape as the other two.

Co-Authored-By: Claude Opus 5 <[email protected]>

* [MOD-15685] Preserve TLS and unix sockets in the test connection helper

Rebuilding the client with redis.Redis(**connection_kwargs) drops the transport
selector: the connection class is what distinguishes plaintext, TLS and unix
socket, and connection_kwargs carries no ssl flag. Under RLTest TLS mode the clone
therefore dialled the TLS-only port in plaintext.

Reuse the source pool connection class and pass its kwargs verbatim, which also
removes the need for the earlier path -> unix_socket_path rename - that rename only
existed because the two constructors spell it differently.

---------

Co-authored-by: Claude Opus 5 <[email protected]>
@redisearch-backport-pull-request redisearch-backport-pull-request Bot added auto-backport This PR was automatically backported by an agent auto-backport-conflicts There were conflicts in this automatic backport labels Aug 16, 2026
@sonarqubecloud

Copy link
Copy Markdown

@dor-forer
dor-forer added this pull request to the merge queue Aug 16, 2026
Merged via the queue into 8.6-rse with commit 3d8d6b7 Aug 16, 2026
41 checks passed
@dor-forer
dor-forer deleted the backport-agent/pr-10817-to-8.6-rse branch August 16, 2026 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto-backport This PR was automatically backported by an agent auto-backport-conflicts There were conflicts in this automatic backport size:M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant