Skip to content

fix(command): re-arm READABLE on the read failures a channel can parse past - #1464

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/channel-readable-rearm
Sep 23, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/channel-readable-rearm

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

Closes #1445. Builds on #1449.

A malformed frame stranded socket bytes on an unschedulable session: the error
paths through read_message_nonblocking skipped the Ready::READABLE re-arm, so
a channel that could have parsed past the bad frame never got another tick.

Both halves, and the harder one is checked first

Disposition — command/src/channel.rs's rearms_readable is an exhaustive
match with no wildcard
over all 15 ChannelError variants, so a new variant
is a compile error rather than a silent default:

  • NothingRead, MessageLengthUnderDelimiter, InvalidProtobufMessage → re-arm
  • MessageTooLarge → does not, and the reason holds at source: it consumes
    nothing and sets Ready::ERROR

NoByteToRead already sets interest = Ready::EMPTY plus HUP, and readable()'s
ceiling path already removes Ready::READABLE, so neither needs a new arm.

Anchor — bin/src/command/sessions.rs gains if new_pending < old_pending { … continue; }
ahead of both existing anchors, so a tick that consumed bytes keeps the session
schedulable regardless of which anchor would otherwise have fired.

read_message_nonblocking now has no exit that bypasses the disposition.

The regression test was seen red on both halves

It asserts the session-level property the issue names — that both frames are
delivered on one tick — rather than which request came back:

# anchor removed (`if new_pending < old_pending`)
panicked at bin/src/command/sessions.rs:1148:9:
assertion `left == right` failed: both frames must be delivered on this tick
  left: 1 / right: 2

# rearms_readable disposition removed
panicked at bin/src/command/sessions.rs:1153:9:
assertion `left == right` failed: both frames must be delivered on this tick
  left: 1 / right: 2

# restored
test …::client_session_delivers_the_frame_behind_a_malformed_length_prefix ... ok

Both reds are the test's own assertion, not a production debug_assert!. The
panic line moving 1148 → 1153 is the five deleted anchor lines, which is what
confirms each break was the intended one.

An earlier draft asserted which request ready() returned, which coupled the
test to requests.pop() returning the last element — it would have gone red if
ready() were ever corrected to remove(0), the real fix for the pipelining loss
its own doc comment acknowledges. That assertion is gone; available_data() == 0
remains as a second, independent discriminator (16 bytes pending pre-fix, matching
the issue's measurement).

Single-sourcing

Three restatements of wants_to_tick's predicate were removed in favour of naming
the symbol — it sits eleven lines away. The stale clause "so the channel can
re-sync on the peer's next frame" is deleted rather than rebutted: the
three-part recovery argument now lives once, in rearms_readable's doc comment,
and the consume site, the extract_messages anchor, the test docs and the
CHANGELOG cite that symbol.

New surface worth naming

Re-arming on InvalidProtobufMessage keeps a peer that streams undecodable frames
schedulable, and each one logs an error!. Bounded by peer bandwidth and strictly
better than the pre-fix wedge — but it is a new log-volume surface, and
doc/configure_admin_ops.md already discusses the logging asymmetry.

Verification

CI's own per-crate commands, --no-default-features --features crypto-ring,opentelemetry,splice,simd:

check result
cargo +nightly fmt --all -- --check 0
cargo build / cargo clippy --all-targets -- -D warnings 0 / 0
cargo test -p sozu-lib 0 — 1012 passed
cargo test -p sozu 0 — 113 passed (the crate carrying sessions.rs)
cargo test -p sozu-e2e -- --skip tests::fuzz_tests:: 0 — 446 passed, 0 failed
sozu-sim udp + tcp, with RUSTFLAGS='--cfg tokio_unstable' 0 / 0 — 6 and 6 passed
3 × --ignored cells (ci.yml:182-184) 0 / 0 / 0 — 3, 1, 1 passed
check_doc_citations.py --root . / --base 0 / 0
git merge-tree --write-tree origin/main <head> 0, gate re-run inside the extracted tree: 0

CHANGELOG: one entry added under 🐛 Fixed, no duplicates, no heading drift.

Note the two sozu-sim cells build a 0-test binary without tokio_unstable
and exit 0 having run nothing — the counts above are with CI's own job env.

…e past

`Channel::read_message_nonblocking` (`command/src/channel.rs`) re-armed
`Ready::READABLE` only on its `NothingRead` path; every error reached the
caller through the `?` one line above it. `readable()` refuses to run at all
while that bit is missing, and it drops the bit itself whenever it fills
`front_buf` to a capacity it may not grow past -- correct backpressure, undone
by whichever parse frees room. So a frame the parser rejected and then
re-synchronised past re-aligned a buffer it could no longer refill, and the
peer's next frame, still in the socket, was stranded on a session
`wants_to_tick` (`bin/src/command/sessions.rs`) does not re-schedule for being
merely readable. No hangup, no error, nothing logged.

Two parse outcomes reach it. `MessageLengthUnderDelimiter` consumes exactly the
eight bytes of a declared length no writer can emit for any payload, precisely
so the stream re-aligns; `InvalidProtobufMessage` consumes the whole
undecodable frame, which it has done since #1428. Both leave a channel that
could parse the next frame if allowed to refill. The comment beside the first
said the consume let "the channel re-sync on the peer's next frame", which was
only ever true of the bytes already in `front_buf`; that clause is deleted
rather than qualified, and what remains cites `rearms_readable` for the refill
half instead of arguing with itself.

The re-arm is per-variant, not a blanket one before the `?`: a socket that just
failed a `read(2)`, or a peer this end has decided to drop, must not have its
readability re-asserted. `rearms_readable` gives all fifteen `ChannelError`
variants a disposition in a `match` with no wildcard arm, so a variant added
later fails to compile rather than inheriting someone else's answer. Those
three re-arm. `MessageTooLarge` does not: that branch consumes NOTHING -- it
must not, since the bytes behind the header may be payload rather than a fresh
delimiter and re-framing on them would decode peer-chosen bytes as a
control-plane request -- so the buffer is not on a boundary and refilling only
re-parses the same header, a busy loop rather than a recovery. The frame could
not complete either way: a length above `max_buffer_size` does not fit a buffer
bounded by `max_buffer_size`, however much more is read. Since #1428 the branch
additionally marks `Ready::ERROR` so the supervisor drops the peer.
`BufferFull` does not re-arm either: past #1436's compaction it means a buffer
at the ceiling whose capacity cannot hold a length prefix, where `readable()`
would read zero bytes and drop the bit again. Nor does `MismatchBufferSize`, an
invariant violation whose slice fails identically on every retry. The remaining
nine cannot reach this call site today and are dispositioned anyway.

Half the defect sat in `extract_messages` (`bin/src/command/sessions.rs`),
which returned on the very parse that re-synchronised the stream.
`Buffer::consume` advances `position` and shifts only past `capacity / 2`, so
retiring an eight-byte prefix moved neither the capacity nor the
`available_space()` that the loop's two anchors watch. A failed parse that
RETIRED bytes is now a third anchor and is checked first: it needs no spin
guard and resets the compaction guard, because `consume` is the sole writer of
`position`, so every `continue` retires at least one byte and the iteration
count is bounded by what the peer writes -- exactly the bound the `Ok` arm has
always had.

Both halves were measured necessary. Against the branch point the regression
test is red; with the re-arm alone it is red; with the anchor alone it is red;
only together is it green.

`client_session_delivers_the_frame_behind_a_malformed_length_prefix` is a
session-level test -- a `ClientSession` driven through `extract_messages` --
because a channel-level one proves the channel re-syncs and says nothing about
whether the session is ever ticked again. At `capacity == max_buffer_size ==
64`: a 40-byte frame, an 8-byte prefix declaring 3, and a 46-byte frame, in ONE
94-byte write, so 64 bytes buffer and 30 stay in the socket while the peer
waits for its response. It asserts that TWO frames come back, not which one:
`ClientSession::ready` hands the caller `requests.pop()`, and which of the two
that is, is a pre-existing pipelining question this changeset does not touch,
so an assertion naming a frame would go red on a fix to `ready` that left #1445
correctly fixed. Counting is equally session-level, and the drained
`available_data() == 0` beside it is a second, independent discriminator: 16
bytes were pending there before the fix, which is the issue's own measurement.
A readiness bit is not asserted at all, since none reachable from a
merely-readable channel would re-schedule that session.

`malformed_payload_is_consumed_so_the_channel_resyncs`'s doc comment said the
frame pipelined behind a malformed one "reaches the session only on its next
readable event" and called that residual deliberately untested and unfixed. It
now states what holds and points at the session-level test; the history is
dropped rather than rebutted in place. The same sentence in the #1428 CHANGELOG
entry is superseded by this changeset's entry rather than edited, so the two
remain separable.

Recorded, not fixed: re-arming on `InvalidProtobufMessage` keeps a peer that
streams undecodable frames schedulable, and each such frame logs an `error!`,
so this is a new log-volume surface -- bounded by the peer's bandwidth and
strictly better than the wedge it replaces, but new, and the logging asymmetry
it sits in is the one `doc/configure_admin_ops.md` §5.2 describes.

Reachability is unchanged from #1428: the command socket is a unix socket
chmod'd 0o600 and there is no control-plane TCP listener, so this is a
robustness fix rather than a remotely triggerable one, though the same parser
serves the master/worker socketpair. Only a malformed peer reaches it, which is
why the severity is below #1436's -- but surviving a malformed peer is what a
framing layer is for, and the comment beside this one claimed it already did.

Closes #1445

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the fix/channel-readable-rearm branch from cf0aea7 to 9c51573 Compare September 23, 2026 04:19
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 970b92c into main Sep 23, 2026
24 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/channel-readable-rearm branch September 23, 2026 05:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A malformed frame strands socket bytes on an unschedulable session: error paths skip the READABLE re-arm

1 participant