Repository navigation
fix(command): compact the front buffer instead of declaring it full - #1449
Merged
Merged
Conversation
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 22, 2026 15:12
FlorentinDUBOIS
force-pushed
the
fix/channel-buffer-full-shift
branch
from
September 22, 2026 16:45
a14072d to
b78b7a6
Compare
`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
force-pushed
the
fix/channel-buffer-full-shift
branch
from
September 22, 2026 20:03
b78b7a6 to
950ea9e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_messagereturnedChannelError::BufferFullas soon asfront_buf.available_space()hit zero atmax_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_messagesdiscards the error with a bareErr(_), andClientSession::ready/WorkerSession::readyclose only onreadiness.is_error() || is_hup().available_space()iscapacity - end— it measures the free tail, not the free room.Buffer::consumecompacts only oncepositionpassescapacity / 2, so decoding one frame of at most half the capacity out of a full buffer advancespositionshort of that threshold and leaves zero space withcapacity - framebytes still pending.At
bin/config.toml'smax_command_buffer_size = 163_840that 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_bufmutators that could trigger aBuffer::shiftare unreachable afterwards:fillneeds a successful read, butreadable()sees no space, getsNonefromgrow_sizeat the ceiling, and executesinterest.remove(Ready::READABLE)— so every laterreadable()returnsErr(Connection(None)).consumeneeds a decoded frame or an under-delimiter length; that return path reaches neither.try_shrink_front_bufis only called after a frame decodes, and both its early returns stop it anyway.Not because it shrinks rather than shifts —
Buffer::shrinkcallsshiftunconditionally 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 fullmax_command_buffer_sizeof 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::shifthands 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:
command/src/channel.rs—self.front_buf.shift()in the zero-space arm.bin/src/command/sessions.rs— a termination anchor inextract_messages, because a shift moves neithercapacitynoravailable_space(), the two quantities the drain loop watched. One compaction retry per delivered message,retried_after_compactionreset only onOk.Bound: capacity strictly increasing and capped at
max_buffer_size;grow_sizereturnsNoneat 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:It asserts
available_data() == 0and!wants_to_tick(&client.channel)— the second is the load-bearing one, documenting why the drain loop rather thanwants_to_tickhad to change.Session-level, not channel-level, deliberately. For any claim about whether a session is schedulable, the unit of proof is a
ClientSessiondriven throughextract_messages; a channel-level test proves the channel re-syncs and says nothing aboutwants_to_tick.Reviewed before opening
An adversarial review built the adverse input rather than checking the test passed: an
Errarm that frees space as a side effect (MessageLengthUnderDelimiterconsuming 8 bytes pastcapacity/2). Measured, the new anchor drains more than the old capacity-only one —pending=16againstpending=24on 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 theREADABLEre-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.