Repository navigation
fix(h2): give a final GOAWAY with an error code a short linger - #1873
Merged
Merged
Conversation
A connection ended by a protocol error or a flood flushed its final GOAWAY and closed at once: `ConnectionH2::lingers_instead_of_closing` allowed the lingering close of #1764 only after a GOAWAY(NO_ERROR). The rest of what the client had already sent was still unread in the kernel receive queue, so Linux answered the close with a reset and dropped the bytes still queued in the socket, the GOAWAY included. `test_h2_outbound_flood_from_rst_stream` shows it in release builds: the glitch budget decides GOAWAY(ENHANCE_YOUR_CALM) after the 2001st refused stream and the GOAWAY is handed to the socket, but the client reads a variable prefix of the RST_STREAM(REFUSED_STREAM) frames, then ECONNRESET, and never the GOAWAY. The same test passed in dev builds. Every final GOAWAY now lingers, but `ConnectionH2::linger_budget` keeps the #1764 bounds (4 MiB, `request_timeout`) for NO_ERROR only. After an error code the linger only has to deliver the GOAWAY, so it gets two named constants in `lib/src/protocol/mux/shared.rs`, and no config key: - `ERROR_GOAWAY_LINGER_TIMEOUT` = 1 s, capped at `request_timeout`, fixed when the linger is decided and not moved by incoming bytes. Once the drain starts, the GOAWAY and `close_notify` are already in the kernel and the write side is shut down; keeping the receive queue empty while the client reads them takes a few round trips, which 1 s covers even at ~300 ms RTT, a tenth of the default `request_timeout`. - `ERROR_GOAWAY_LINGER_MAX_BYTES` = 256 KiB. Sozu sets no `SO_RCVBUF`, so a frontend starts at Linux's default receive buffer (`net.ipv4.tcp_rmem[1]`, 128 KiB), and autotuning stops growing it once reading stops at the GOAWAY: 256 KiB is that queue plus as much again in flight. A peer that sends more is ignoring the GOAWAY. An error GOAWAY can leave streams open (a flood, a flow-control or protocol error, a SETTINGS timeout), unlike a final GOAWAY(NO_ERROR), which waits for the stream table to empty. None will be answered, so the pass that starts such a linger releases them first, in the order of `ngx_http_v2_finalize_connection`: `Mux::release_streams_and_backends_for_error_linger` emits their access logs (`session close`, as the immediate close did), recycles them, and shuts down, accounts for and drops every backend connection. It reuses `Mux::log_and_recycle_open_streams` and `Mux::close_backend_connections`, extracted unchanged from `Mux::close`, which calls them too; at the final close they find nothing, so no gauge or ledger entry is charged twice. `Mux::ready_inner` calls it after the frontend write, before the `pending_links` pass, which skips every recycled stream, so nothing is re-linked; and at its top, for a linger started by a timeout or a soft stop, neither of which holds the proxy. Recycled slots keep their buffers until the session drops, at the latest at the 1 s deadline. Tests: - `an_error_goaway_lingers_under_the_short_budget_and_a_graceful_one_keeps_its_own` goes through `ConnectionH2::goaway`. It fails when `linger_budget` returns the graceful pair for every GOAWAY (left 4194304, right 262144), and when the NO_ERROR-only condition from main is restored (the error GOAWAY gets `CloseSession`). - `an_error_goaway_linger_releases_its_streams_and_backends_when_it_starts` runs one `Mux::ready` pass. It fails when both release calls are removed. - `test_h2_outbound_flood_from_rst_stream` (unchanged): exit 101 at 95094cd in release, 1634 RST_STREAMs and 0 GOAWAYs; passes 5/5 in release and dev with 2001 RST_STREAMs and 1 GOAWAY. Docs: LIFECYCLE.md section 8.1, doc/h2_mux_internals.md (pinned h2.rs blocks re-anchored), doc/configure.md (`request_timeout`) and CHANGELOG.md, including a note on the #1764 entry. Fixes #1861 Signed-off-by: Florentin Dubois <[email protected]>
This was referenced Oct 5, 2026
FlorentinDUBOIS
added a commit
that referenced
this pull request
Oct 6, 2026
…#1875) `Mux::release_streams_and_backends_for_error_linger` was gated by `Connection::lingers_after_error_goaway`, which only recognised a linger already draining. On TLS, a GOAWAY carrying an error code that leaves streams open goes out without `close_notify` (`ConnectionH2::output_flush_closes_connection` wants an empty stream table), so the writable pass that decides the close queues the alert in `H2Shell::linger_instead_of_closing` and leaves the linger `Linger::Pending`. When the kernel then refuses the alert's record, `kernel_refused_write` withholds the re-arm, `Mux::ready_inner`'s loop ends with the linger still pending, the release is skipped, and the `pending_links` pass can dial a backend for a stream queued by the same read pass. The next pass, once the linger drains, closed it again. The gate now treats a pending or draining linger after a non-graceful final GOAWAY as decided, so the release runs right after the frontend write that decides it, before `pending_links`. It stays idempotent and `Mux::close` finds nothing left to account for. A graceful linger and H1 are unchanged. The regression test drives a real `FrontRustls` frontend through `Mux::ready` with a full send queue; before the fix the `pending_links` pass reaches the stub proxy's routing view and panics. LIFECYCLE.md §8.1, doc/h2_mux_internals.md and the CHANGELOG describe the decided linger instead of the draining one. Refs: #1861 Refs: #1873 Signed-off-by: Florentin Dubois <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A connection ended by a protocol error or a flood flushed its final GOAWAY and closed at once:
ConnectionH2::lingers_instead_of_closinglingered only after GOAWAY(NO_ERROR) (#1764). Unread client bytes were still in the kernel receive queue, so Linux answered the close with a RST and dropped the queued bytes, GOAWAY included.test_h2_outbound_flood_from_rst_streamshows it in release: the client reads a prefix of RST_STREAM frames, then ECONNRESET, never the GOAWAY.request_timeout).ERROR_GOAWAY_LINGER_TIMEOUT= 1 s (capped atrequest_timeout, fixed, not moved by incoming bytes) andERROR_GOAWAY_LINGER_MAX_BYTES= 256 KiB (twice Linux's default receive buffer; Sōzu sets noSO_RCVBUF). Named constants, no config key.Mux::release_streams_and_backends_for_error_linger, nginxngx_http_v2_finalize_connectionorder), so nothing re-links or holds a backend during the drain. The loops are extracted unchanged fromMux::close; accounting is charged once.lib/src/protocol/mux/LIFECYCLE.md§8.1,doc/h2_mux_internals.md,doc/configure.mdand CHANGELOG.Fixes #1861
Verification
test_h2_outbound_flood_from_rst_stream, unchanged, release--no-default-features --features crypto-ring: at 95094cd exit 101 (1634 RST_STREAM, 0 GOAWAY); with the fix 5/5 runs 2001 RST_STREAM + 1 GOAWAY, release and dev.an_error_goaway_lingers_under_the_short_budget_and_a_graceful_one_keeps_its_ownandan_error_goaway_linger_releases_its_streams_and_backends_when_it_starts, each seen red (graceful budget for every GOAWAY; NO_ERROR-only condition; release calls removed) and green.cargo test -p sozu-lib --no-default-features --features crypto-ring(1611 passed), clippy-D warnings,cargo +nightly fmt --check,check_doc_citations.py, and all 314 H2 e2e tests in dev: green.