#15144·gvisor

gVisor with small segments, in-window data is dropped without an ACK

Author: everaniumCreated Sep 30, 2026Updated Sep 30, 2026
Labelstype: bug

Description

Hello, additional bug, verified by me in real custom VPN solution, under RFC 9293 §3.10.7.4.

Summary and description:

With payloads below the per-segment overhead (about 630 bytes: segSize + PacketBufferStructSize + the headers), the memory accounted for received, unread segments (receiveMemUsed) exceeds the receive buffer before the advertised window closes. From then on segmentQueue.enqueue refuses every segment with payload. In-window data, its retransmission, a one-byte window probe. In the dispatcher, counts it (SegmentQueueDropped) and sends no ACK, until the application reads.

The advertised window is (bufSize - used) >> 1 (wndFromSpace): the halving assumes the per-segment overhead is at most the payload. For small payloads it is not, so the sender is allowed more than the memory admits. Empty segments are still admitted, so a Linux probe (empty, at snd_una - 1) is answered while a FreeBSD or Windows probe (one byte at snd_una) is not.

Where it matters:

Senders with a small MSS: a 576-byte-MTU path (IPv4's minimum, MSS 536), old or embedded stacks, some tunnels inside tunnels, a middlebox clamping MSS to 536. Rare on today's internet (MSS 1220-1460). Found by a unit test with several MSS values.

Patch:

small_segments_test.tar.gz

Steps to reproduce

2 MB receive buffer, application not reading, the sender filling the advertised window in one burst:

MSS Result
700 window closes cleanly at 1 MB, probes answered
600 23 520 bytes inside the advertised window refused; no ACK for them, their retransmission or a one-byte probe
536 82 192 bytes refused, same

Expected result:

RFC 9293 §3.10.7.4: a segment that cannot be accepted is acknowledged (<SEQ=SND.NXT><ACK=RCV.NXT><CTL=ACK>), and a window the receiver advertises can be sent into.

Actual result:

No ACK. The sender retransmits on RTO with backoff and gives up (FreeBSD after 12 retransmissions, Linux after tcp_retries2); a later window update alone does not recover the connection, the refused data must be retransmitted first.

Affected versions:

Reproduced with gvisor.dev/gvisor v0.0.0-20260921164205-6b569e77c779 and with the go branch at v0.0.0-20260925204757-f6e2c02e9739, Linux amd64 (test); FreeBSD 15 sender (field), Go 1.27.1.

Reproducer:

go
// sendSmallSegment sends a segment with n bytes of payload at seq and returns
// the ACK number and the window of the ACK for it.
func sendSmallSegment(t *testing.T, c *context.Context, seq seqnum.Value, n seqnum.Size) (seqnum.Value, seqnum.Size) {
        t.Helper()
        c.SendPacket(make([]byte, n), &context.Headers{
                SrcPort: context.TestPort,
                DstPort: c.Port,
                Flags:   header.TCPFlagAck,
                SeqNum:  seq,
                AckNum:  c.IRS.Add(1),
                RcvWnd:  30000,
        })
        b := c.GetPacketWithTimeout(time.Second)
        if b == nil {
                t.Fatalf("no ACK for the segment at %d; SegmentQueueDropped = %d", seq, c.EP.Stats().(*tcp.Stats).ReceiveErrors.SegmentQueueDropped.Value())
        }
        defer b.Release()
        h := header.TCP(header.IPv4(b.AsSlice()).Payload())
        return seqnum.Value(h.AckNumber()), seqnum.Size(h.WindowSize()) << c.RcvdWindowScale
}

// TestSmallSegmentsBufferFullAcked tests that a segment within the window is
// acknowledged when it is refused because the receive buffer is full (RFC 9293
// section 3.10.7.4). The initial window is half the buffer, which segments
// smaller than the segment overhead fill up before the window closes.
func TestSmallSegmentsBufferFullAcked(t *testing.T) {
        c := context.New(t, e2e.DefaultMTU)
        defer c.Cleanup()
        c.CreateConnected(context.TestInitialSequenceNumber, 30000, 32<<10)

        const mss = 536
        seq := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
        for edge := seq.Add(mss); seq.Add(mss).LessThanEq(edge); seq = seq.Add(mss) {
                if ack, wnd := sendSmallSegment(t, c, seq, mss); edge.LessThan(ack.Add(wnd)) {
                        edge = ack.Add(wnd)
                }
        }
}

// TestSmallSegmentsWindowClosesBeforeBufferFull tests that the window
// advertised for segments smaller than the segment overhead closes before
// the receive buffer is full, so that no segment within it is refused.
func TestSmallSegmentsWindowClosesBeforeBufferFull(t *testing.T) {
        c := context.New(t, e2e.DefaultMTU)
        defer c.Cleanup()
        c.CreateConnectedWithRawOptions(context.TestInitialSequenceNumber, 30000, 128<<10, []byte{header.TCPOptionWS, 3, 0, header.TCPOptionNOP})

        // Read the first segment, so that the ACK for the next one moves the
        // right edge of the initial window to what the buffer holds.
        const mss = 536
        seq := seqnum.Value(context.TestInitialSequenceNumber).Add(1)
        sendSmallSegment(t, c, seq, mss)
        seq = seq.Add(mss)
        if _, err := c.EP.Read(io.Discard, tcpip.ReadOptions{}); err != nil {
                t.Fatalf("c.EP.Read: %s", err)
        }

        var wnd seqnum.Size
        for edge := seq.Add(mss); seq != edge; {
                n := min(mss, seq.Size(edge))
                var ack seqnum.Value
                ack, wnd = sendSmallSegment(t, c, seq, n)
                if seq = seq.Add(n); ack != seq {
                        t.Fatalf("got ACK %d for the segment ending at %d within the window ending at %d", ack, seq, edge)
                }
                if edge.LessThan(ack.Add(wnd)) {
                        edge = ack.Add(wnd)
                }
        }
        if wnd != 0 {
                t.Fatalf("got window %d, want 0", wnd)
        }
}

Before the fix (fails):

--- FAIL: TestSmallSegmentsBufferFullAcked (1.01s)
    tcp_test.go:2840: no ACK for the segment at 31342; SegmentQueueDropped = 1
--- FAIL: TestSmallSegmentsWindowClosesBeforeBufferFull (1.03s)
    tcp_test.go:2870: no ACK for the segment at 123534; SegmentQueueDropped = 1

After the fix (passes):

--- PASS: TestSmallSegmentsBufferFullAcked (0.01s)
--- PASS: TestSmallSegmentsWindowClosesBeforeBufferFull (0.02s)
--- PASS: TestReceiveBufferAutoTuningApplicationLimited (0.08s)

Suggested fix:

Two changes, which together make the small-segment cases behave as the large-segment ones do (the window closes cleanly, probes are answered).

  1. Advertise only the window the memory can back. readyToRead records the payload and segMemSize of the largest segment delivered so far (as Linux measures scaling_ratio on segments of at least rcv_mss), and selectWindowLocked bounds wndFromSpace(available) by (available / segMemSize + 1) * payload while any memory is free -- the segments enqueue actually admits. For a segment with the overhead below its payload the ratio is above 1/2 and nothing changes; below, the window is what the memory holds, and a sender of uniform segments reaches a zero window with the memory exactly full, whatever its MSS.

  2. Acknowledge what still has to be refused. When segmentQueue.enqueue finds the memory exhausted and the segment carries data, it drops the data (pkt.Data().CapLength(0), dataMemSize re-read), marks the segment and queues it. At the cost of an empty segment, which is admitted regardless of memory, instead of returning false. handleRcvdSegment answers a marked segment as it answers one outside the window: TS.Recent updated #15143 (RFC 7323 §4.3 rule 2), an ACK with the current RCV.NXT and window, nothing else taken from it.

SegmentQueueDropped still counts it. This is what Linux does when tcp_data_queue drops for memory (ICSK_ACK_NOMEM): the sender learns the receiver is alive and full instead of backing off into a retransmission timeout.

Measured with the same 2 MB buffer: MSS 600 closes the window at about 1 024 256 bytes with 2 097 424 bytes of segment memory held, MSS 536 at about 965 632 bytes with 2 097 528, nothing refused, probes answered; MSS 700 and above close at 1 048 320 bytes as before. Bulk transfer between two stacks with the application reading is unchanged at MSS 1448 and at 524 (the memory never fills while the reader keeps up).

The fix changes one existing test, TestReceiveBufferAutoTuningApplicationLimited: it expected no ACK for a segment refused for memory (its own comment says "some may have been dropped due to segment overheads"); upstream-test.diff lets it accept the zero-window ACK. The fix and the test diff go together.

runsc version

bash
Not applicable: gVisor's netstack used as a Go library (pkg/tcpip), no runsc

docker version (if using docker)

bash

uname

Linux linux 7.2.6-arch2-1 #1 SMP PREEMPT_DYNAMIC Mon, 14 Sep 2026 22:41:30 +0000 x86_64 GNU/Linux

kubectl (if using Kubernetes)

bash

repo state (if built from source)

No response

runsc debug logs (if available)

bash