Skip to content

HTTP/2 h2c: the read path drains the session at the socket and loses response bytes #219

Closed

Description

@EdmondDantes

What the code does

http2_feed ends with an eager drain that writes nghttp2's output at the socket
with send(2), bypassing the connection's outbound queue
(src/http2/http2_strategy.c:938 on main):

char drain_buf[16384];
while (http2_session_want_write(self->session)) {
    const ssize_t n = http2_session_drain(self->session, drain_buf, sizeof(drain_buf));
    if (n <= 0) { break; }
    ...
    const ssize_t sent = send(fd, drain_buf, (size_t)n, MSG_NOSIGNAL);
    if (sent != (ssize_t)n) { break; }
}

http2_session_drain takes the bytes out of nghttp2 (nghttp2_session_mem_send),
so the session no longer holds them. When send returns EAGAIN or a short count,
the loop breaks and those bytes are discarded.

Two things go wrong, and neither is limited to control frames. The loop runs from
the plaintext read callback, and by then conn->out_in_flight is often set by a
response writev the h2c emitter submitted (http2_strategy.c:1549 skips the
emitter while a writev is in flight, so nghttp2 keeps the rest of the response
queued). The drain then pulls DATA frames of that same response:

  1. Whatever the socket refuses is lost. The response ends mid-frame and the peer
    waits for bytes that are not coming.
  2. Whatever the socket accepts is written ahead of the writev in flight and of the
    coalesce tail behind it — the overtake Pipelined HTTP/1 responses come back out of order when a large one follows a small one #209 and A streamed HTTP/1 response overtakes a buffered one still in the coalesce tail #211 closed for HTTP/1.

The two error-path sends at :860 and :871 (bad-preface GOAWAY, error drain)
bypass the same queue.

Reproduction

A raw h2c client that grants a large window, then acks every 16 KiB while the
server streams 8 MiB (128 × 64 KiB write()), on the build at 52dbeb6:

body ack every received complete
8 MiB 16 KiB 2621440 B no
3 MiB 16 KiB 2686976 B no
8 MiB 256 KiB 8388608 B yes

The handler reports writing all 128 chunks and end() returns. Frequent
WINDOW_UPDATEs are what trigger it: each one wakes the read callback, and the
drain runs while the response writev is in flight. Every client that refills its
window per frame — curl, nghttp, browsers — takes this path.

A marked fprintf at the break confirms the cause:

PROBE-DROP n=16384 sent=-1    errno=11 in_flight=1 tail=0
PROBE-DROP n=16384 sent=11008 errno=2  in_flight=1 tail=0

errno=11 is EAGAIN; the second line wrote 11008 of 16384 bytes and dropped the
remaining 5376 from the middle of a DATA frame.

What is missing

The connection already has one serialised outbound path and one emitter that uses
it. The read path needs to reach the wire through them rather than past them.

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