Skip to content

fix(h1): linger before closing a connection whose request is still arriving - #1742

Merged
FlorentinDUBOIS merged 3 commits into
mainfrom
fix/h1-lingering-close
Oct 1, 2026
Merged

FlorentinDUBOIS merged 3 commits into
mainfrom
fix/h1-lingering-close

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

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:

  • The backend stream is ended right away, which disconnects the backend (H1 keep-alive after an early response reuses both connections mid-request (request smuggling, upstream desync) #1721).
  • Once the response and any TLS close_notify are flushed, shutdown(SHUT_WR): the FIN follows the response.
  • What the client still sends is read from the raw socket into a 16 KiB stack buffer and dropped. No allocation, nothing parsed or forwarded.
  • The connection closes on the client's EOF, on a socket error, after 4 MiB (LINGER_MAX_BYTES), or at the listener's request_timeout counted from the end of the response, whichever comes first. ConnectionH1::arm_timeout is a no-op while lingering, so no caller moves the deadline, including the re-arm after Mux::timeout_inner's write pass. Drained bytes count as frontend bytes in. Mux::timeout_inner closes a lingering frontend before any per-stream timeout handling.
  • A frontend HUP while lingering reads to the EOF before closing, rather than closing with bytes unread.
  • A soft stop lets a lingering session run until its own deadline.

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 the CHANGELOG.md Unreleased / Fixed section.

Tests

  • e2e 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.
  • unit 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.
  • unit a_lingering_close_stops_at_its_byte_budget_and_its_deadline.
  • unit a_linger_started_by_a_timeout_write_keeps_its_own_deadline and a_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 returned Continue instead of CloseSession.
  • unit a_silent_client_is_closed_at_the_linger_deadline.
  • Each unit test fails under the mutation named in its doc comment.
  • a_deferred_tls_close_completes_the_response_once: once close_notify is flushed the connection now lingers (Continue) instead of returning CloseSession. The test also asserts the FIN and the CloseSession that 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_upload closes its client before the soft stop, so the stop does not wait for the linger deadline.

Stacked on #1737.

…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
FlorentinDUBOIS merged commit 8e703a7 into main Oct 1, 2026
28 checks passed
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]>
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.

fix(h1): close without lingering after a response that completed before its request

1 participant