Skip to content

refactor(transfer): one marker-aware NDX reader for every role - #7228

Merged
oferchen merged 1 commit into
masterfrom
refactor/shared-ndx-and-attrs-reader
Aug 6, 2026
Merged

oferchen merged 1 commit into
masterfrom
refactor/shared-ndx-and-attrs-reader

Conversation

@oferchen

@oferchen oferchen commented Aug 6, 2026

Copy link
Copy Markdown
Owner

Why

Upstream funnels every NDX read on a live transfer stream through one function,
read_ndx_and_attrs() (rsync.c:322-431). Its loop classifies the value before
any attribute byte is touched, and the branch order is load-bearing - most
notably NDX_DEL_STATS is drained before the !inc_recurse || am_sender gate,
so a peer that never negotiated INC_RECURSE still consumes the counters instead
of desyncing on them.

oc had four partial re-implementations of that rule, each missing a different
branch:

Copy Missing
receiver/file_list/on_demand.rs read_next_frame the NDX_DEL_STATS branch
receiver/file_list/receive.rs receive_extra_file_lists NDX_DONE and NDX_DEL_STATS
receiver/transfer/phases.rs phase-boundary + goodbye reads the sub-list branches; goodbye had its own del-stats loop
generator/transfer/goodbye.rs read_ndx_skipping_del_stats the rsync.c:343 rejection

What

crates/transfer/src/receiver/ndx_stream.rs holds the rule once:

  • read_ndx_step() - one iteration of upstream's read_loop, in upstream's
    order: file index (:332), NDX_DONE (:334), NDX_DEL_STATS (:336), the
    !inc_recurse || am_sender gate (:343), NDX_FLIST_EOF (:353), sub-list
    segment (:360).
  • read_marker_aware_ndx() - the whole loop, for call sites that read no
    attribute tail.
  • read_ndx_and_attrs() - loop plus tail. The attribute read sits inside
    the loop because upstream's protocol-29 keep-alive branch (rsync.c:386-391)
    jumps back into read_loop after iflags is decoded; that branch stays
    unimplemented and is documented as a known incompleteness (it is reachable -
    rsync 3.0.9 emits the raw form at exactly protocol 29, io.c:953-968, and oc
    negotiates down to 28), with the continue seam marked so adding it is an
    insertion rather than a restructuring.

Those branches mutate state the classifier does not own - the receiver's
file_list / ndx_segments / flist_eof, or the generator's deletion counters.
A FlistMarkerSink trait inverts the dependency. ReceiverContext implements it
by delegating to the existing receive_one_extra_segment (segment reception is
reused, not reimplemented); a NoLazyFlist value stands in for the call sites
that must never grow a list, and the generator's goodbye borrows itself through
a small adapter that declares the sender role so upstream's am_sender term is
what rejects a marker.

SenderAttrs::read_with_codec_xattr is split into an NDX half and
read_attrs_after_ndx(). That split is what lets NDX_DONE stop consuming two
phantom iflags bytes the peer never sent (rsync.c:334-335).

Scope

Deliberately not in this change: routing the pipelined per-file echo sites,
collapsing the two NDX codecs in the drivers, the protocol-29 keep-alive branch,
the non-regular-file guard (rsync.c:420-428), and the xname sanitize_path()
pass (rsync.c:407-417). The dir_ndx range check is not duplicated either -
receive_one_extra_segment already performs it (flist.c:2622-2626).

Nothing is written on the NDX_FLIST_EOF branch. Upstream's
write_int(f_out, NDX_FLIST_EOF) (rsync.c:358) never reaches a socket: the
sender is rejected by the :343 gate first, and the receiver child runs with
sock_f_out = -1; f_out = error_pipe[1] (main.c:1063-1067). Upstream's
start_flist_forward() / stop_flist_forward() teeing (io.c:1483-1489) is
likewise not ported - it exists only because upstream's generator and receiver
are separate processes each parsing their own copy; oc runs both over one
file_list.

Tests

New tests pin the branch order, not just behaviour:

  • a sub-list marker interleaved between two per-file echoes is consumed and only
    file NDXs surface;
  • NDX_FLIST_EOF mid-transfer sets flist_eof and does not surface;
  • sub-list dir_ndx values are non-monotonic and non-contiguous - a measured
    upstream oracle produced 1, 9, 2, 10, 3, 11 (level-1 dirs interleaved with
    newly discovered subdirs appended to dir_flist), so no validation may assume
    ordering or contiguity;
  • NDX_DEL_STATS is drained by a sink whose inc_recurse is off, which only
    passes while rsync.c:336-342 precedes rsync.c:343; the sender drains it too
    and its counters reach the sink;
  • NDX_DONE returns without consuming the attribute tail (asserted on the reader
    position, so the regression is visible even though the returned value looks
    correct);
  • a sender whose inc_recurse is genuinely on still rejects every marker with
    upstream's wording, Invalid file index: %d (%d - %d), tagged RERR_PROTOCOL.

The byte-for-byte upstream 3.4.4 daemon capture
(real_upstream_multisegment_sublist_decodes_as_segments) passes unchanged as
the no-regression gate on the classifier.

Upstream funnels every NDX read on a live transfer stream through
read_ndx_and_attrs() (rsync.c:322-431), whose loop classifies the value
before any attribute byte is touched. oc had four partial copies of that
rule, each missing a different branch.

Add crates/transfer/src/receiver/ndx_stream.rs holding the rule once:

  - read_ndx_step() is one iteration of upstream's read_loop, in
    upstream's order: file index, NDX_DONE, NDX_DEL_STATS, the
    !inc_recurse || am_sender gate, NDX_FLIST_EOF, sub-list segment.
    The del-stats drain sits ahead of the gate exactly as upstream has
    it, so a peer without INC_RECURSE still consumes the counters.
  - read_marker_aware_ndx() runs the loop for call sites that read no
    attribute tail.
  - read_ndx_and_attrs() adds the tail, with the attribute read inside
    the loop so the protocol-29 keep-alive re-entry (rsync.c:386-391,
    which jumps back after iflags is decoded) is an insertion rather
    than a restructuring. That branch stays unimplemented and is
    documented as a known incompleteness for peers <= 3.0.x.

The branches mutate state the classifier does not own, so a
FlistMarkerSink trait inverts the dependency: ReceiverContext
implements it by delegating to receive_one_extra_segment, and a
NoLazyFlist value stands in for the call sites that must never grow a
file list. SenderAttrs is split into an NDX half and
read_attrs_after_ndx(), which is what lets NDX_DONE stop consuming two
phantom iflags bytes.

Collapsed onto the primitive:

  - receiver/file_list/on_demand.rs read_next_frame (gains the
    NDX_DEL_STATS branch it lacked; FrameKind folds into NdxStep)
  - receiver/file_list/receive.rs receive_extra_file_lists batched drain
  - receiver/transfer/phases.rs phase-boundary and goodbye NDX_DONE reads
  - generator/transfer/goodbye.rs read_ndx_skipping_del_stats, which
    keeps upstream's sender asymmetry: rsync.c:343 rejects every marker
    when am_sender is set, so the sender uses the primitive with markers
    rejected rather than consumed.

Tests pin the branch order rather than only the behaviour, including a
measured upstream oracle showing sub-list dir_ndx values arrive
non-monotonic (1, 9, 2, 10, 3, 11), so no validation may assume ordering
or contiguity.
@oferchen
oferchen marked this pull request as draft August 6, 2026 15:48
@oferchen

oferchen commented Aug 6, 2026

Copy link
Copy Markdown
Owner Author

Verification status — draft pending a later verification pass.

Verified: Linux interop passed. All 8 decomposed sub-tasks covered (primitive 471 lines + 466 lines of tests; read_attrs_after_ndx split; three duplicate copies of upstream's read_ndx_and_attrs loop collapsed onto one implementation). CI 45 checks green.

NOT verified: nextest (stable), Linux musl, and Windows (stable) — the last is blocked on a Set up job infra rerun. Byte-neutrality evidence (daemon pull/push + proto29 diffed against master) was NOT produced.

Risk note: this touches the receiver NDX read path that all four network cells route through. The byte-neutrality diff is the gate that matters here, and it is outstanding.

@oferchen
oferchen marked this pull request as ready for review August 6, 2026 22:12
@oferchen
oferchen merged commit 44fbc8b into master Aug 6, 2026
54 of 65 checks passed
@oferchen
oferchen deleted the refactor/shared-ndx-and-attrs-reader branch August 11, 2026 13:09
oferchen added a commit that referenced this pull request Aug 24, 2026
Upstream funnels every NDX read on a live transfer stream through
read_ndx_and_attrs() (rsync.c:322-431), whose loop classifies the value
before any attribute byte is touched. oc had four partial copies of that
rule, each missing a different branch.

Add crates/transfer/src/receiver/ndx_stream.rs holding the rule once:

  - read_ndx_step() is one iteration of upstream's read_loop, in
    upstream's order: file index, NDX_DONE, NDX_DEL_STATS, the
    !inc_recurse || am_sender gate, NDX_FLIST_EOF, sub-list segment.
    The del-stats drain sits ahead of the gate exactly as upstream has
    it, so a peer without INC_RECURSE still consumes the counters.
  - read_marker_aware_ndx() runs the loop for call sites that read no
    attribute tail.
  - read_ndx_and_attrs() adds the tail, with the attribute read inside
    the loop so the protocol-29 keep-alive re-entry (rsync.c:386-391,
    which jumps back after iflags is decoded) is an insertion rather
    than a restructuring. That branch stays unimplemented and is
    documented as a known incompleteness for peers <= 3.0.x.

The branches mutate state the classifier does not own, so a
FlistMarkerSink trait inverts the dependency: ReceiverContext
implements it by delegating to receive_one_extra_segment, and a
NoLazyFlist value stands in for the call sites that must never grow a
file list. SenderAttrs is split into an NDX half and
read_attrs_after_ndx(), which is what lets NDX_DONE stop consuming two
phantom iflags bytes.

Collapsed onto the primitive:

  - receiver/file_list/on_demand.rs read_next_frame (gains the
    NDX_DEL_STATS branch it lacked; FrameKind folds into NdxStep)
  - receiver/file_list/receive.rs receive_extra_file_lists batched drain
  - receiver/transfer/phases.rs phase-boundary and goodbye NDX_DONE reads
  - generator/transfer/goodbye.rs read_ndx_skipping_del_stats, which
    keeps upstream's sender asymmetry: rsync.c:343 rejects every marker
    when am_sender is set, so the sender uses the primitive with markers
    rejected rather than consumed.

Tests pin the branch order rather than only the behaviour, including a
measured upstream oracle showing sub-list dir_ndx values arrive
non-monotonic (1, 9, 2, 10, 3, 11), so no validation may assume ordering
or contiguity.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant