Skip to content

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

Closed

Description

@FlorentinDUBOIS

try_read_delimited_message (command/src/channel.rs) has a third peer-length rejection branch
besides the two in #1428. It returns without consuming and without moving a readiness
bit, exactly like the oversize branch did — but unlike that one, the peer that triggers it has done
nothing wrong, and the correct recovery is not a close.

All line numbers below are at 595920e91c4528f0554410b904b45d543c88e467.

The branch

command/src/channel.rs:626-632, reached when the declared length is in range but the whole frame
is not buffered yet:

if self.front_buf.available_space() == 0 {
    if self.front_buf.capacity() >= self.max_buffer_size {
        return Err(ChannelError::BufferFull {
            capacity: self.front_buf.capacity(),
            max: self.max_buffer_size,
        });
    }

No consume, no error!, no readiness change. Same shape as #1428's oversize branch.

Reachability, step by step

Buffer::consume (command/src/buffer/growable.rs:86-94) compacts the buffer only when the read
cursor has passed the halfway mark:

pub fn consume(&mut self, count: usize) -> usize {
    let cnt = cmp::min(count, self.available_data());
    self.position += cnt;
    if self.position > self.capacity / 2 {
        self.shift();
    }
    cnt
}

So, with capacity C already at max_buffer_size:

  1. Buffer::fill (command/src/buffer/growable.rs:97-105) shifts whenever a read fills the buffer
    to its end, so once the front buffer is full we have position == 0, end == C,
    available_data() == C.
  2. The parser decodes a frame of length L <= C / 2 and calls front_buf.consume(L)
    (command/src/channel.rs:609). position becomes L, which is not > C / 2, so no shift.
  3. Now available_space() == C - end == 0 while available_data() == C - L.
  4. The next frame declares M with C - L < M <= C. It passes both guards — it is at least
    delimiter_size() and at most max_buffer_size — but buffer.len() == C - L < M, so the decode
    branch at :596 is skipped and control falls through to :626, where available_space() == 0
    and capacity() >= max_buffer_size both hold. Err(BufferFull).

With bin/config.toml's max_command_buffer_size = 163840 that is a frame of at most 81 920 bytes
followed by one larger than 81 920 — ordinary LoadState territory, from a peer whose every frame
is correctly formed and under the shared ceiling.

Nothing can recover it

The buffer holds C - L pending bytes at offset L, and a single Buffer::shift
(command/src/buffer/growable.rs:121-135) would move them to offset 0 and hand back L bytes of
space — enough for the frame to complete. But no code path can reach that shift again:

  • The only three front_buf mutators that can trigger it are fill (:375, :509), consume
    (:573, :609) and shrink (:800).
  • fill needs a successful read. Channel::readable (:315-337) sees available_space() == 0,
    grow_size returns None at the ceiling, and it executes self.interest.remove(Ready::READABLE)
    (:335) and breaks. Every later readable() returns Err(Connection(None)).
  • consume needs either a decoded frame or an under-delimiter length; the parse returns at :628
    before reaching either.
  • try_shrink_front_buf (:786-801) early-returns unless available_data() * 4 < initial_buffer_size, which is false with C - L bytes pending — and it calls Buffer::shrink,
    not shift, anyway.

Downstream is #1428's story verbatim: extract_messages (bin/src/command/sessions.rs:577-607)
discards the error with the bare Err(_) at :590 and returns immediately because the capacity did
not change; ClientSession::ready (:165) and WorkerSession::ready (:549) close only on
readiness.is_error() || is_hup(); wants_to_tick (:612-615) keys on the same bits, so the
session is not even re-scheduled. The session is wedged permanently, holding its fd and a full
max_command_buffer_size buffer, with nothing logged.

Why the #1428 fix deliberately does not cover it

#1428's oversize branch now logs and marks the channel Ready::ERROR, because a length above the
ceiling can never be satisfied by a buffer bounded at that ceiling — no recovery exists, so dropping
the peer is the only exit.

That reasoning does not transfer here. The frame can complete: the bytes are already buffered, the
declared length is under the ceiling, and a compaction makes room. Marking the channel errored would
drop a conforming peer over an internal buffer-management detail — the precise conflation #1428's
fix was written to avoid. The fix belongs in the buffer management, not in the close path: compact
the front buffer when the declared frame fits the capacity but not the remaining space, and only
then consider the frame unsatisfiable.

What I verified, and what I did not

Verified by reading command/src/channel.rs, command/src/buffer/growable.rs and
bin/src/command/sessions.rs at 595920e9, and by tracing every front_buf mutator in the file.

Not verified: no unit test drives a real Channel pair into this state yet, and there is no
end-to-end reproduction against a running sōzu master. The step-2 precondition (fill always
compacts when it reaches the end of the buffer) is read from Buffer::fill, not measured. Found
while fixing #1428; filed separately because the recovery is different.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions