Skip to content

extract FrameQueue/FrameChunks so the serve framers stop maintaining their byte-count invariant by hand #1834

Closed

Description

@yigitdot

The two serve framers — ChunkFramer (crates/node/src/handlers/client/delivery.rs) and CoherentFrameProducer (crates/node/src/handlers/client/serve_encoder.rs) — hold identical queue state and maintain its invariants by hand, with a free function (wire::drain_frame) holding the seam open via two &mut out-params that must move in lockstep.

Surfaced by the type-design review of #1828. Deliberately deferred from that PR's follow-up because it subsumes and contradicts the hardening that landed there: FrameQueue would delete the very checked_sub and queue.is_empty() guards the follow-up added, so doing both at once would have been incoherent. Harden first, refactor second — this is second.

The invariants, all currently prose

  1. queued == queue.iter().map(Bytes::len).sum() — held by hand at four mutation sites across two files.
  2. No empty Bytes ever enters the queue — enforced by an if len > 0 at each of two independent push sites, and depended on by a third party (drain_frame's front_len == 0 branch).
  3. A non-empty queue with target >= 1 always yields at least one chunk — stated in a doc comment and re-checked as a near-identical anyhow::bail! in two places.

The consequence of a breach is not a panic: it is Ok(None), which serve_leg reads as done_delivering and turns into StreamEnd over a truncated blob — with the client paying the closing voucher. These are money-carrying invariants enforced by runtime checks at the right doors rather than by a type.

Proposed shape

pub(super) struct FrameQueue { queue: VecDeque<Bytes>, queued: usize }

impl FrameQueue {
    fn push(&mut self, b: Bytes);   // drops empties, keeps `queued` in step
    fn len(&self) -> usize;
    fn is_empty(&self) -> bool;
    fn clear(&mut self);            // the fault path
    fn cut(&mut self, target: usize) -> Option<FrameChunks>;  // None iff is_empty()
}

pub(super) struct FrameChunks { chunks: Vec<Bytes>, total: usize }

FrameChunks falls out of cut with a private constructor, which makes the (Vec<Bytes>, usize) tuple's sum invariant unrepresentable-if-violated. drain_frame is the only producer of that tuple in the tree today, so this is a two-field struct in the module that already owns the sole constructor — not a refactor.

What it deletes

  • Both copies of the "cut no chunks from N queued bytes" desync bail, and their two divergent error strings.
  • Both push-site if len > 0 filters.
  • drain_frame's front_len == 0 branch, which today would silently produce a short-but-correctly-labelled frame.
  • Both guards in chunk_frame_bufs — including the sum check, which is already unreachable by construction since drain_frame computes total from the same items it pushes.
  • The checked_sub and queue.is_empty() guards added in the perf(node,protocol): zero-copy ChunkData framing via vectored QUIC writes (#1791) #1828 follow-up.

Net: fewer lines than it adds.

Explicitly out of scope

  • Do not fold faulted into FrameQueue. The two faults are semantically different (a store export fault vs. an encode fault), carry different messages, and the terminal-poison rule is a property of the producer, not of a byte queue. Duplicating faulted: bool in each producer is the correct amount of duplication.
  • Do not unify the two producers behind a trait — they differ in fault and drain semantics. FrameQueue extracts the shared part and that is where it should stop.

Adjacent, optional

frame_target could return NonZeroUsize, which would delete both if target == 0 guards and make the two zero-target regression tests uncompilable — the right outcome, but it touches ~6 call sites and deserves its own commit.

Activity

  1. added
    rust
    prio:low
    on Aug 26, 2026
  2. moved this from Backlog to Ready in deCDNon Aug 26, 2026
  3. moved this from Ready to In progress in deCDNon Aug 31, 2026
  4. moved this from In progress to Done in deCDNon Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

prio:lowrust

Type

No type

Fields

Priority

None yet

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions