Skip to content

fix(h2): drive the h2c read-path drain through the connection emit (#219) - #220

Merged
EdmondDantes merged 4 commits into
mainfrom
219-h2c-read-path-drain
Aug 22, 2026
Merged

EdmondDantes merged 4 commits into
mainfrom
219-h2c-read-path-drain

Conversation

@EdmondDantes

Copy link
Copy Markdown
Contributor

Closes #219.

The h2c read path ended http2_feed by draining nghttp2 at the socket with
send(2). http2_session_drain takes the bytes out of the session, so a short
write or an EAGAIN discarded them: the response ended mid-frame and the peer
waited for bytes that were not coming. What the socket did accept was written
ahead of the writev the emitter had already handed to the reactor — the overtake
#209 and #211 closed for HTTP/1.

Both halves are reachable from any client that refills its flow-control window
per frame, because each WINDOW_UPDATE wakes the read callback while the response
writev is in flight. Against main, a 3 MiB body read by a client acking every
16 KiB arrives as 2686976 bytes with no END_STREAM.

The read path now calls http2_session_emit, the same emit the response path
uses: it queues through the connection and skips while a writev is in flight,
because the completion re-drives it. That also removes the second drain API from
this path — the eager loop used nghttp2_session_mem_send while the emitter uses
nghttp2_session_send, and a partially consumed mem_send slice was stranded
where the emitter could not reach it.

The two error-path sends go through the queue as well: the bad-preface GOAWAY
template and the GOAWAY nghttp2 queues on a protocol error. The destroy that
follows defers while a write is in flight, so those bytes still leave.
MSG_NOSIGNAL has no call site left and goes with them.

Evidence: tests/phpt/server/h2/057-h2-window-refill-body-intact.phpt reads back
3 MiB with a distinct byte per chunk and fails against main on all three of its
lines — end_stream: no, bytes: 2686976 of 3145728, body: CORRUPT — twice out
of two runs. 466 phpt, 442 passed, 0 failed, 0 warned, 24 skipped; ctest 16 of 16.

@github-actions

github-actions Bot commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Total lines: 82.76% → 82.72% (-0.04 pp)

File Baseline Current Δ Touched
src/core/http_connection.c 77.53% 77.84% +0.31 pp ●
src/core/http_connection_tls.c 73.46% 73.74% +0.28 pp
src/http1/http_parser.c 83.68% 84.06% +0.38 pp
src/http2/http2_session.c 88.76% 86.76% -2.00 pp ●
src/http2/http2_strategy.c 84.41% 84.36% -0.04 pp ●
src/http3/http3_callbacks.c 83.07% 83.49% +0.43 pp
src/http3/http3_listener.c 78.08% 76.88% -1.20 pp
src/websocket/ws_session.c 89.85% 90.41% +0.55 pp

❌ Regression in touched files (> 1.0 pp drop)

  • src/http2/http2_session.c dropped -2.00 pp

Add [coverage-drop-ok] to a commit message in this PR to override.

@EdmondDantes

Copy link
Copy Markdown
Contributor Author

Adversarial review of the diff turned up four more defects, all reachable through this change. Fixed here rather than filed.

Two drain cursors over one session. The error path still used http2_session_drain (nghttp2_session_mem_send) while everything else on h2c now goes through nghttp2_session_send. nghttp2 states the rule twice in nghttp2.h: the assigned pointer is valid only until the next call, and the caller must send all of it before the next chunk. http2_session_drain deliberately parks a partial slice in send_pending, which the emit never reads — so a protocol error raised while a handler was still streaming queued 239 bytes of a DATA frame and left the rest behind, which is the truncation shape this PR exists to remove. The generic error case takes the emit now. The bad-preface template stays on the direct queue, because nghttp2 has nothing queued there and mem_send is never called.

A write completion finalised a deferred destroy before asking the session for its last frames. http_send_batched_finish destroyed and returned, reaching http2_conn_notify_emit only otherwise. With the read path no longer writing at the socket, that call is the only way h2c output reaches the wire after a skipped emit — so a response whose final DATA(END_STREAM) was still inside nghttp2 lost it. The emit runs first now, and the destroy waits for the write it may have started.

A session that finished while a write was in flight was never torn down. http2_feed reads want_read/want_write after an emit that may have done nothing; a queued GOAWAY keeps want_write at 1, so feed returned 0 where main returned −1. The verdict is read only from feed, and a peer that has been told GOAWAY sends nothing more to read, so the connection was held to the read deadline. It now asks for the teardown and lets the write completion finalise it.

The emit handed its buffer and body references to the writev context without detaching them. A bailout in the submit — it allocates — then ran the caller's h2_emit_state_cleanup over all three: the record array freed twice, the buffer and the references freed under the completion that owns them.

A chained submit that never reached the reactor left the in-flight state set. Pre-existing, and this change makes the error path lean on the deferred destroy it stranded.

Separately, and found while measuring: one PING was answered with two ACKs — #222, fixed here with h2/058.

Two findings are left open because they are pre-existing and reach past this path: nothing arms a write deadline for the queued h2c path, so a peer that stops reading without closing wedges out_in_flight (and now the read path with it); and the TLS hybrid emit alternates mem_send and session_send on one session, which is the same two-cursor violation on the TLS side. Both want their own change and their own test.

467 phpt, 443 passed, 0 failed, 0 warned, 24 skipped; ctest 16 of 16.

@EdmondDantes

Copy link
Copy Markdown
Contributor Author

The second reviewer caught a regression I introduced in the previous commit, and it was visible in my own GOAWAY probe before that — I read past it.

Moving the emit ahead of the deferred destroy was wrong, and is reverted. A bad connection preface answered GOAWAY and then the SETTINGS the session had queued before BAD_CLIENT_MAGIC fired: 75 bytes where main sends 17, to a peer that is often not an HTTP/2 speaker at all. The same reorder also let the re-drive free the connection under its own caller — a WRITEV_EX submit that fails synchronously completes from inside it, and that completion re-enters http_send_batched_finish and destroys. The re-drive is the last statement again, so a destroy inside it returns into nothing.

What the reorder was for is done differently now. http2_session_emit_now runs the same pump but queues behind a write in flight instead of skipping, and the two teardown paths use it: the protocol-error GOAWAY, and the verdict at the end of the feed that a peer told GOAWAY will never come back to re-read. The response path keeps http2_session_emit, which skips and is re-driven, so it still gathers a whole nghttp2_session_send pass into one writev rather than copying into the tail.

The third case — a deferred destroy discarding the response's own last frames — is filed as #225 rather than fixed here: it is main's behaviour too, its reachability is unproven, and both obvious fixes are the ones this comment just reverted.

h2/059 is new and pins the bad-preface reply at one GOAWAY frame. It passes against main as well — the path simply had no test, which is how the regression got as far as it did.

Two more from the same review, filed and not fixed here: #223 (a queued plaintext write has no deadline, so a peer that stops reading holds the connection past every sweep) and #224 (the TLS hybrid emit alternates the same two drain cursors). And #226, which is main's and not this path's: under h2load -n 400 -c 20 -m 8 with a 1 MiB body in each direction, write() fails its own static return-type check and the server takes a fatal — 217 of 400 errored on 52dbeb6, 157 of 400 here, reproduced on both.

468 phpt, 444 passed, 0 failed, 0 warned, 24 skipped; ctest 16 of 16. The bad-preface and protocol-error wires are byte-identical to main again.

@EdmondDantes
EdmondDantes merged commit 9f4e771 into main Aug 22, 2026
8 checks passed
@EdmondDantes
EdmondDantes deleted the 219-h2c-read-path-drain branch August 22, 2026 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant