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

Fail the RDB transcode on bytes after the source frame - #104

Closed
madolson wants to merge 1 commit into
upstream-unstable-8f67ab136from
fix-transcoder-trailing-bytes
Closed

madolson wants to merge 1 commit into
upstream-unstable-8f67ab136from
fix-transcoder-trailing-bytes

Conversation

@madolson

Copy link
Copy Markdown
Owner

During a disk-based full sync that transcodes the RDB (wire codec differs from the replica's rdbcompression), any bytes after the end of the primary's VCS frame are left pending in the push reader, and the next transcoder write trips serverAssert(len == 0 || !resuming) and crashes the replica. A well-formed payload never has such bytes, so this is reachable only with a malformed or corrupted payload, but it should fail the sync, not the process. This fails the transcode when the frame ends with input left over, so the replica aborts the sync and retries.

AI generated details

Problem

rdbTranscoderRun() breaks out of its feed loop on STREAM_PUSH_READER_FRAME_DONE (src/rdb_transcoder.c:141), on the assumption stated in its comment that the caller already excluded trailing bytes. streamPushReaderFeed() stashes whatever follows the frame end in pending_input (src/compression_stream.c:616-618). After the frame ends the decompressor consumes nothing (src/compression.c:161), so later input is stashed too. The next feed with input then asserts at src/compression_stream.c:593.

Base 8f67ab136, LZ4 source, plaintext target:

Bytes after the frame Before After
none OK OK
in the same write as the frame end silently dropped, transcode succeeds transcode fails
in one later write stashed, transcode succeeds transcode fails
in two or more later writes serverAssert crash transcode fails

replicaReceiveRDBFromPrimaryToDisk() calls rdbTranscoderWrite() once per read for size-framed transfers and up to twice per read for $EOF-marked ones (src/replication.c:3199-3210), so trailing bytes that span a read boundary reach the crash. The verbatim route (source codec equals target) never decodes; it still writes trailing bytes to disk, where the loader stops at the frame end and ignores them.

Introduced in #4749 (7fe7ca45f on unstable, 4216cddd7 on 9.2). Not in any release tag.

Can a well-behaved primary send trailing bytes?

No. The receiver holds back the trailing RDB_EOF_MARK_SIZE bytes, so the $EOF mark is never fed (src/replication.c:3193-3214). The primary writes exactly one codec frame per RDB (rdbSaveRio(), src/rdb.c:1675), and the diskless $EOF trailer is written after it returns and the writer is detached (src/rdb.c:1631).

Alternative considered

Ignore the trailing bytes instead of failing. The on-disk loader stops at the frame end (CompressionTest.streamReaderStopsBeforeTrailingBytes), so the verbatim route already tolerates them. That loses: nothing legitimate produces them, the transcoder's own contract says there are none, and it needs a frame-done flag plus discarding pending input on every later write rather than one check. Failing the sync costs a retry on a payload that is already wrong.

Behavior change

Trailing bytes that arrive with the frame end used to be dropped and the sync succeeded. They now fail the sync. Only malformed payloads are affected.

Testing

CompressionTest.rdbTranscoderRejectsBytesAfterSourceFrame in src/unit/test_compression.cpp drives the transcoder directly for every source/target pair that converts, with the trailer in the frame's write and split across two later writes. There were no unit tests for the transcoder before. With the src/rdb_transcoder.c hunk reverted:

[ RUN      ] CompressionTest.rdbTranscoderRejectsBytesAfterSourceFrame
test_compression.cpp:1830: Failure
Expected equality of these values:
  rdbTranscoderWrite(&t, with_trailer, sdslen(with_trailer))
    Which is: 0
  -1
Google Test trace:
test_compression.cpp:1824: src=2 target=0 same_write=true
...
test_compression.cpp:1835: Failure
Expected equality of these values:
  rdbTranscoderWrite(&t, trailer, 3)
    Which is: 0
  -1
Google Test trace:
test_compression.cpp:1824: src=2 target=0 same_write=false

The second trailer write then dies in the assert:

frame #3: _serverAssert(estr="len == 0 || !resuming", file="compression_stream.c", line=593) at debug.c:1180:22
frame #4: streamPushReaderFeed(..., len=4, ...) at compression_stream.c:593:5
frame #5: rdbTranscoderRun + 180
frame #6: rdbTranscoderWrite + 392
frame #7: CompressionTest_rdbTranscoderRejectsBytesAfterSourceFrame_Test::TestBody() at test_compression.cpp:1836:21

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

When a disk-based full sync is transcoded, bytes that follow the end of
the source VCS frame are left pending in the push reader.
rdbTranscoderRun() then returns success, and the next write feeds the
reader again while it still holds them, which trips
serverAssert(len == 0 || !resuming) in streamPushReaderFeed() and crashes
the replica. If the trailing bytes instead arrive in the final write they
are silently dropped.

A well-formed payload never has bytes after the frame: the receiver
strips the $EOF mark before feeding the transcoder and the primary writes
a single frame. Treat trailing bytes as a malformed payload and fail the
transcode, so the replica aborts the sync and retries instead of
crashing.

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