Skip to content

fix(command): compact the front buffer instead of declaring it full - #1449

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
fix/channel-buffer-full-shift
Sep 22, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
fix/channel-buffer-full-shift

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

Closes #1436.

Stacked on #1438 (fix(command): drop a peer declaring a frame length above the channel ceiling), which is the commit below this one. Review this PR's own commit, a14072de; #1438's commit disappears from the diff once it merges.

The defect

try_read_delimited_message returned ChannelError::BufferFull as soon as front_buf.available_space() hit zero at max_buffer_size, without consuming and without moving a readiness bit — the same shape #1428 removed from the two branches above it, with the same downstream consequence: extract_messages discards the error with a bare Err(_), and ClientSession::ready / WorkerSession::ready close only on readiness.is_error() || is_hup().

available_space() is capacity - end — it measures the free tail, not the free room. Buffer::consume compacts only once position passes capacity / 2, so decoding one frame of at most half the capacity out of a full buffer advances position short of that threshold and leaves zero space with capacity - frame bytes still pending.

At bin/config.toml's max_command_buffer_size = 163_840 that is a frame of at most 81_920 bytes followed by one larger than that — both correctly framed, both under the shared ceiling, sent by a conforming peer. Unlike #1428's oversize branch, no misbehaviour is required.

Why the wedge was permanent

All three front_buf mutators that could trigger a Buffer::shift are unreachable afterwards:

  • fill needs a successful read, but readable() sees no space, gets None from grow_size at the ceiling, and executes interest.remove(Ready::READABLE) — so every later readable() returns Err(Connection(None)).
  • consume needs a decoded frame or an under-delimiter length; that return path reaches neither.
  • try_shrink_front_buf is only called after a frame decodes, and both its early returns stop it anyway.

Not because it shrinks rather than shifts — Buffer::shrink calls shift unconditionally before its own size bail, so reaching it would have compacted. The session stayed registered for the life of the process, pinning its file descriptor and a full max_command_buffer_size of buffer, with nothing logged.

The fix

Compaction, not a close. The bytes are already buffered and the declared length is under this end's ceiling, so only the buffer's internal offsets stand in the way; one Buffer::shift hands back exactly the room the consumed frame left behind. Closing would drop a peer that did nothing wrong.

The changeset has two halves, and the review confirmed both are necessary:

  1. command/src/channel.rs — self.front_buf.shift() in the zero-space arm.
  2. bin/src/command/sessions.rs — a termination anchor in extract_messages, because a shift moves neither capacity nor available_space(), the two quantities the drain loop watched. One compaction retry per delivered message, retried_after_compaction reset only on Ok.

Bound: capacity strictly increasing and capped at max_buffer_size; grow_size returns None at the ceiling. Both continue paths are finite and peer-independent.

Proof

Regression test client_session_completes_a_compacted_frame_without_another_peer_write, seen red with only the anchor hunk reverted:

panicked at bin/src/command/sessions.rs:970:50
  left: Request { request_type: "Unallowed" }  right: Request { request_type: LoadState }

It asserts available_data() == 0 and !wants_to_tick(&client.channel) — the second is the load-bearing one, documenting why the drain loop rather than wants_to_tick had to change.

Session-level, not channel-level, deliberately. For any claim about whether a session is schedulable, the unit of proof is a ClientSession driven through extract_messages; a channel-level test proves the channel re-syncs and says nothing about wants_to_tick.

Reviewed before opening

An adversarial review built the adverse input rather than checking the test passed: an Err arm that frees space as a side effect (MessageLengthUnderDelimiter consuming 8 bytes past capacity/2). Measured, the new anchor drains more than the old capacity-only one — pending=16 against pending=24 on the same input — so it is strictly better and still bounded.

That probe also surfaced a genuine second wedge of the same family, scoped out and filed separately as #1445 rather than widened into this changeset: channel.rs:505's ? skips the READABLE re-arm at :510, stranding socket bytes on an unschedulable session. It is not this changeset's shape and widening the anchor would not fix it.

Exit codes read directly: build ×2 = 0, clippy ×2 = 0, fmt = 0, sozu-command-lib = 0, sozu = 0 (106 tests), citation gate = 0.

`try_read_delimited_message` returned `ChannelError::BufferFull` as soon as
`front_buf.available_space()` hit zero at `max_buffer_size`, without consuming
and without moving a readiness bit -- the shape #1428 removed
from the two branches above it, with the same downstream consequences:
`extract_messages` (`bin/src/command/sessions.rs`) discards the error with a
bare `Err(_)`, and `ClientSession::ready` / `WorkerSession::ready` close only on
`readiness.is_error() || is_hup()`.

`available_space()` is `capacity - end`: it measures the free tail, not the free
room. `Buffer::consume` (`command/src/buffer/growable.rs`) compacts only once
`position` passes `capacity / 2`, so decoding one frame of at most half the
capacity out of a buffer filled to capacity advances `position` short of that
threshold and leaves zero space with `capacity - frame` bytes still pending. At
`bin/config.toml`'s `max_command_buffer_size = 163_840` that is a frame of at
most 81_920 bytes followed by one larger than that, both correctly framed and
both under the shared ceiling.

The wedge was permanent: the three `front_buf` mutators that can trigger a
`Buffer::shift` are all unreachable afterwards. `fill` needs a successful read,
but `readable()` sees no space, gets `None` from `grow_size` at the ceiling and
executes `interest.remove(Ready::READABLE)`, so every later `readable()` returns
`Err(Connection(None))`; `consume` needs a decoded frame or an under-delimiter
length, neither of which that return path reaches; and `try_shrink_front_buf` is
only called after a frame decoded, with both of its own early returns stopping
it anyway -- `capacity <= initial_buffer_size`, which holds whenever the channel
was configured with `command_buffer_size == max_command_buffer_size`, and
`available_data() * 4 < initial_buffer_size`, which is false with that much
pending. Not because it shrinks rather than shifts: `Buffer::shrink` calls
`shift` unconditionally before its own size bail, so reaching it would have
compacted. The session stayed registered for the life of the process, pinning
its file descriptor and a full `max_command_buffer_size` of buffer, with nothing
logged -- reached by a conforming peer, unlike #1428's oversize
branch.

The recovery is compaction, not a close. The bytes are already buffered and the
declared length is under this end's ceiling, so only the buffer's internal
offsets stand in the way and one `Buffer::shift` hands back exactly the room the
consumed frame left behind. Closing would drop a peer that did nothing wrong
over an internal buffer-management detail, the conflation #1428's
fix was written to avoid.

The boundary between the two is the ordering of the guards, unchanged: a
declared length above `max_buffer_size` still returns `MessageTooLarge` and
still marks the channel errored before control reaches the zero-space arm, so
compaction cannot rescue a frame the ceiling can never admit, whatever layout it
is parsed from. Past the compaction, `BufferFull` survives only for a capacity
too small to hold the eight-byte length prefix at all -- a configuration that
can never frame a message -- which a new `debug_assert!` states. The compaction
sits ahead of the ceiling check, so a front buffer still below `max_buffer_size`
now compacts on its first zero-space parse and doubles on the next one instead
of doubling straight away; the frame still completes, one read-parse round later
against one fewer doubling.

Making the channel recoverable is not making the session recover, and the second
half of this changeset is in `extract_messages`. Its drain loop terminated on
`old_capacity == new_capacity`, exhaustive only while growth was the one way a
parse could make room for a frame it could not yet complete. A compaction frees
space without changing capacity, so the loop returned on precisely the parse
that made the room -- one iteration short of the `readable()` that completes the
frame -- leaving the rest unread in the socket with nothing to schedule another
tick: `wants_to_tick` keys only on `(writable && back_buf.available_data() > 0)
|| hup || error`, READABLE is not among them, mio is edge-triggered so the
readable edge was already spent, and there is no periodic sweep. For the
canonical peer -- write a request, wait for the response -- "recoverable on the
peer's next byte" means never. The anchor is now "the parse made room", by
growth or by compaction, with an explicit spin guard: freed space cannot be
trusted as progress on its own, since a parser that consumes nothing conserves
bytes trivially, so at most ONE compaction-driven retry is granted between two
delivered messages, reset on every `Ok`. The loop can therefore only continue by
delivering a message or by growing capacity, and capacity is capped at
`max_buffer_size`. One extra pass is also all a compaction ever needs: it leaves
`available_space() == capacity - pending` and the ceiling guard has already
proven the declared length fits `max_buffer_size`.

Pinned by `a_frame_within_the_ceiling_is_rescued_by_compaction` and, for the
session half, by
`client_session_completes_a_compacted_frame_without_another_peer_write` -- a
`ClientSession` at capacity 64 whose peer writes 72 bytes in a single write and
then waits, which delivered only the first frame before this change. Both were
seen red first. `a_frame_above_the_ceiling_still_closes_from_the_same_layout`
drives the oversize path from that identical wedged layout and is green on both
sides by design: it pins the guard ordering the fix must not disturb.
`doc/configure_admin_ops.md` 5.2 now draws the same ceiling-versus-layout line
and states why the frame must complete within the tick.

Closes #1436.

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the fix/channel-buffer-full-shift branch from b78b7a6 to 950ea9e Compare September 22, 2026 20:03
@FlorentinDUBOIS
FlorentinDUBOIS merged commit f66e502 into main Sep 22, 2026
24 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the fix/channel-buffer-full-shift branch September 22, 2026 21:10
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.

command: a BufferFull at the ceiling is neither consumed nor signalled, so the command-channel session wedges permanently on a conforming peer

1 participant