fix(network): stop silently dropping packets when the send buffer is full

NetState.Send had three failure modes once the send buffer filled, none of
them visible:

  - No writable space: GetSendBuffer returned false and the packet was
    dropped with no log and no disconnect. The client stayed connected
    while quietly missing game state.

  - Some space but not enough: NetworkCompression.Compress hit its guard
    and returned 0, so CommitWrite(0) ran and the packet was dropped the
    same way.

  - One to three bytes writable: Compress computes
    safeOutputLength = (nuint)output.Length - 4, which underflows for
    those sizes. The hot loop's bounds check never trips and it writes
    past the span, corrupting the in-flight region of the ring buffer.
    Reachable because callers only check for non-zero space.

Compress now refuses an output too small to bound, and Send reports
exhaustion instead of dropping: it logs and disconnects with the bytes
needed, writable, awaiting acknowledgement, and total capacity. Those
numbers separate a slow client holding the buffer from a buffer genuinely
too small for the shard, which is the case that warrants raising
network.sendBufferSize.

Tests cover the underflow via sentinel bytes around the output window and
were confirmed to fail without the guard.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Kamron Batman 2026-07-27 22:55:37 -07:00
parent 80aff11a46
commit ee600d02ac
No known key found for this signature in database
GPG key ID: 7D81DF26D9A5D94A
3 changed files with 134 additions and 2 deletions

View file

@ -73,7 +73,9 @@ public static class NetworkCompression
public static int Compress(ReadOnlySpan<byte> input, Span<byte> output)
{
if (input.Length > DefiniteOverflow)
// output.Length < 4 underflows safeOutputLength below (nuint), defeating the hot loop's
// bounds check. Reachable whenever the send buffer is nearly full.
if (input.Length > DefiniteOverflow || output.Length < 4)
{
return 0;
}