Repository navigation
fix(h1): linger before closing a connection whose request is still arriving - #1742
Merged
Merged
Conversation
FlorentinDUBOIS
force-pushed
the
fix/h1-lingering-close
branch
from
October 1, 2026 11:33
3e5985e to
0e0b101
Compare
…riving When an H1 response completes before its request was received whole, the client connection used to be closed as soon as the response was flushed. A close with request bytes still unread, or followed by more, makes the kernel send a reset that discards the unread part of the response. Stage the close as RFC 9112 §9.6 recommends: once the response and any TLS close_notify are flushed, shut the write side down, then read and drop what the client still sends until its EOF, 4 MiB, or the listener's request_timeout, whichever comes first. Fixes #1738 Signed-off-by: Florentin Dubois <[email protected]>
…d request ConnectionH1::arm_timeout is now a no-op while lingering. Mux::timeout_inner re-arms the frontend after its write pass, and that pass can itself start the linger (a 408/503/504 answer, or a pending flush completing), which replaced the request_timeout deadline with the frontend timeout. A request of which no byte arrived (the 408 to a silent client) has nothing in flight to drain: it closes at once, as before. A partly received request, including one sozu answers itself, still lingers. Drained bytes count as frontend bytes in. Refs #1738 Signed-off-by: Florentin Dubois <[email protected]>
Move the linger timeout helper and its tests above the doc comment of an_early_wheel_delivery_does_not_run_the_timeout_body, and re-wrap the LIFECYCLE.md section 8.4 paragraph. Refs #1738 Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
force-pushed
the
fix/h1-lingering-close
branch
from
October 1, 2026 15:13
0e0b101 to
e8f28ec
Compare
FlorentinDUBOIS
added a commit
that referenced
this pull request
Oct 1, 2026
Fixes #1764 ## Problem Once an H2 frontend has flushed its final GOAWAY, it stops reading and closes the session at once. The client may still be sending frames it wrote before it read the GOAWAY (RFC 9113 §6.8). Closing with those frames unread, or receiving more after the close, makes the kernel send a reset. The reset discards response bytes still queued in sozu's socket, so the client can lose the end of a response it was otherwise served in full. ## Fix The close now uses the lingering close from #1742. The H1 and H2 frontends share `Linger`, `LINGER_MAX_BYTES` and `drain_discard`, which move to `lib/src/protocol/mux/shared.rs`. H1 behaviour is unchanged. It applies when a `writable` pass that began after a final GOAWAY(NO_ERROR) decides `CloseSession` and the client has not hung up: graceful drain, soft stop, or the reply to the client's own GOAWAY. In that case `H2Shell::linger_instead_of_closing`: - Flushes TLS `close_notify` first. If it is still pending, the linger is `Linger::Pending`, and the next pass resumes it even though the state is `Error` by then. - Then calls `shutdown(SHUT_WR)`, so the FIN follows everything sent. - Then reads the raw socket and drops what it reads, until the client's EOF, 4 MiB, or the listener's `request_timeout`, whichever comes first. Other points: - `ConnectionH2::arm_timeout` and `set_timeout_duration` do not move the linger deadline. `Mux::timeout_inner` closes a lingering frontend first. - A GOAWAY that carries an error code (protocol error, flood) does not linger. - In `Mux::shutting_down_inner`, the H2 graceful-shutdown budget is checked before the linger, and the lingering `readable`/`writable` passes adopt the pass's clock, so an armed budget closes a lingering session when it elapses first. A drain started by the client's own GOAWAY does not arm that budget, so its linger runs to `request_timeout`. Docs: `LIFECYCLE.md` §8.1, `doc/h2_mux_internals.md`, `doc/configure.md` (`request_timeout`), and the CHANGELOG. Pinned code blocks in `doc/h2_mux_internals.md` are re-anchored to the shifted `h2.rs` lines. ## Tests The red baseline for the first test is `fix/h1-lingering-close` (#1742, since merged), because it is the first tree that has `Linger`. - unit `an_h2_frontend_lingers_after_its_final_goaway_so_the_client_reads_everything` (`mod.rs`): a slow-reading client keeps sending after the final GOAWAY. - Red on an archive of the base with only the test file copied in: the client read 65509 of 1048576 bytes, then got `ConnectionReset`. - Green with the fix: everything arrives, followed by a clean EOF, and the session closes on the client's EOF. - unit `a_linger_deferred_behind_close_notify_starts_on_the_next_pass` (`h2.rs`, TLS): fails with `got CloseSession` when the `Linger::Pending` assignment is removed (checked in a copy of the tree). - `the_final_goaway_and_close_notify_leave_in_one_tls_write`: behavior change. The pass that flushes the final GOAWAY now starts the lingering close (`Continue` with `is_lingering()`) instead of returning `CloseSession`. Its write-count and record-order assertions are unchanged. - unit `a_soft_stop_closes_a_lingering_h2_session_once_its_budget_elapses` (`mod.rs`): a 1 s budget armed 3 s ago and a linger started 100 ms into it. Red on the first commit with only the test added: `shutting_down()` kept the session. Green once the lingering passes adopt the clock. 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.
Fixes #1738
Problem
When an H1 response completes before its request was received whole, the client connection is closed as soon as the response is flushed (#1721). The close is immediate. A socket closed with request bytes still unread, or one that receives more after the close, makes the kernel send a reset that discards the part of the response that is still queued.
Fix
RFC 9112 §9.6 staged close, in
ConnectionH1(lib/src/protocol/mux/h1.rs), whenever the close follows a response while part of the request has arrived but not all of it. This includes the answers sozu generates itself (400, 401, 413, 503, 504). A request of which no byte arrived, such as the 408 to a silent client, closes at once as before:close_notifyare flushed,shutdown(SHUT_WR): the FIN follows the response.LINGER_MAX_BYTES), or at the listener'srequest_timeoutcounted from the end of the response, whichever comes first.ConnectionH1::arm_timeoutis a no-op while lingering, so no caller moves the deadline, including the re-arm afterMux::timeout_inner's write pass. Drained bytes count as frontend bytes in.Mux::timeout_innercloses a lingering frontend before any per-stream timeout handling.There is no new setting. The time bound reuses
request_timeout.Docs:
lib/src/protocol/mux/LIFECYCLE.md§8.4,doc/configure.md(request_timeout,http.close.request_incomplete), and theCHANGELOG.mdUnreleased / Fixed section.Tests
test_h1_early_response_survives_the_rest_of_the_upload: the client keeps uploading while it reads a large early 413. The last body byte is withheld, so the request never completes. With the fix reverted, the test fails 3 runs out of 3: the client receives only part of the response body and then a reset. With the fix it passes.a_lingering_close_half_closes_then_drains_to_the_client_eof: checks the FIN, the drained byte count, that the deadline does not move, and the close on EOF.a_lingering_close_stops_at_its_byte_budget_and_its_deadline.a_linger_started_by_a_timeout_write_keeps_its_own_deadlineanda_408_to_a_silent_client_closes_without_lingering. Both fail without the second commit: the deadline was 60 s instead of 5 s, and the 408 returnedContinueinstead ofCloseSession.a_silent_client_is_closed_at_the_linger_deadline.a_deferred_tls_close_completes_the_response_once: onceclose_notifyis flushed the connection now lingers (Continue) instead of returningCloseSession. The test also asserts the FIN and theCloseSessionthat follows the client's EOF.a_fin_after_epoll_wait_closes_the_backend_on_the_next_round_at_the_latest: the fixture used to stage a request that was never parsed, i.e. an incomplete one, and that path now ends the backend stream at once. It now stages what the test is about: a complete request whose client asked to close. Assertions are unchanged.try_h1_early_response_mid_uploadcloses its client before the soft stop, so the stop does not wait for the linger deadline.Stacked on #1737.