Skip to content

fix(mux): close an HTTP/2 session whose client write failed - #1793

Merged
FlorentinDUBOIS merged 15 commits into
mainfrom
fix/mux-h2-reset-mid-download
Oct 2, 2026
Merged

FlorentinDUBOIS merged 15 commits into
mainfrom
fix/mux-h2-reset-mid-download

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

Stacked on #1789.

An HTTP/2 frontend whose client resets its connection while a response is being sent keeps its session until a timeout: the delayed close waits for the pending output, and no H2Shell write path treats a write answering Error or Closed as fatal.

Fix: a failed write to an H2 client marks the connection (H2Shell::record_write), which then reports no pending write, and H2Shell::writable closes the session, as an H1 frontend does since #1789.

Test: e2e test_rr_h2_client_reset_mid_download (12 runs): an H2 client with a large window reads ~280 KB of an 8 MiB response, then resets; the backend connection must close within 2 s (red on the #1789 head).

Fixes #1792

A client that half-closes after its request (shutdown(SHUT_WR)) has
stopped sending, not receiving (RFC 9293 §3.6), but its FIN reaches
sozu as a frontend HUP: Ready::from(&Event) maps EPOLLRDHUP to it.
Mux::ready_inner treated that HUP as a hang-up. On entry it closed the
session, or queued close_notify and closed once the queued output was
flushed, so a TLS response still arriving from the backend was cut, or
never sent when the FIN came with the request. TlsHandshake::ready
likewise closed on a HUP that arrived with the client's TLS 1.3
Finished and request, and upgraded_frontend_events dropped a HUP seen
during the handshake.

A frontend HUP now closes the session only once no stream is open and
no read is due (Mux::frontend_exchange_in_flight). Output already
pending is still flushed, without close_notify. A lingering frontend
gets READABLE in the inner loop as on entry, since a read in the same
pass may have met the EOF before the linger started. The handshake
reads before acting on a HUP, and the HUP survives the upgrade, so the
session closes once its response is complete. A real hang-up is found
by the next write. ConnectionH1::readable no longer links a request
whose stream was already answered (Unlinked), such as a 408 from
Mux::timeout_inner sent while the request head was incomplete.

a_hup_during_the_flush_that_starts_a_linger_drains_before_closing
asserted that the linger drain, specifically, read the client's last
bytes. On a half-close the request reader now reads them first, so the
test asserts the guarantee itself: the frontend socket holds no unread
byte at the close, which keeps the close a FIN and not a reset. The
other assertions (the session closes, the 408 arrives, the connection
ends with a FIN) are unchanged. The new check still fails on the bug
the test was written for: on the code before this change, with
!self.frontend.is_lingering() dropped from the in-loop HUP check, it
reports 4 bytes left unread.

e2e_session_tls_client_fin_not_truncated no longer sleeps 200 ms
before half-closing.

Fixes #1779

Signed-off-by: Florentin Dubois <[email protected]>
…shake

Keeping a half-closed session open exposed three cases where nothing was
left to serve and the session spun or waited instead of closing.

A reset reaches the mux as ERROR beside the HUP. The half-close check
kept the session for its open stream, nothing cleared the ERROR, and the
exit check of Mux::ready_inner never breaks on ERROR: the loop ran to
MAX_LOOP_ITERATIONS. Mux::frontend_exchange_in_flight now treats ERROR
as a hang-up whatever is in flight.

Once the client's EOF is read, a request it left incomplete can never
complete. An open stream now keeps the session only when its request was
received whole; otherwise the session closes at once, as before the
half-close change, instead of waiting for the backend. Input still unread
(Connection::has_unread_input: a read due, or a read parked on buffer
pressure) keeps it until read.

TlsHandshake::ready read past a HUP when a read was due, but a short
read leaves the EOF unread and rustls may stop wanting to read after the
ClientHello: the HUP stayed with nothing to act on it and the loop ran to
MAX_LOOP_ITERATIONS. A client whose FIN arrived before its handshake
completed can never send its Finished, so TlsHandshake::readable now
closes such a handshake, and TlsHandshake::drive no longer counts HUP
alone as work. A FIN or a reset during the handshake logs at debug.

A lingering frontend now counts as unread input, from the READABLE
ConnectionH1::start_linger raises until its drain reads the EOF, so the
in-loop insert of READABLE and the !is_lingering() guard are removed.
The #1774 unit test is renamed
a_lingering_frontend_reads_the_clients_last_bytes_before_closing and pins
that role. an_answered_request_is_never_linked pins the relink guard of
ConnectionH1::readable.

Tests: e2e reset with a linked request and incomplete request plus FIN,
over TLS and plaintext; plaintext half-close counterparts of the TLS
ones; unit partial ClientHello plus FIN, full ClientHello plus FIN, with
and without WRITABLE.

Refs #1779

Signed-off-by: Florentin Dubois <[email protected]>
try_tls_connection_close_large_response read responses_sent before
joining the backend thread, which bumps it only after its write_all
returns. Under load the client could read the whole response first, and
the test failed with "backend sent 0 responses" although the body was
delivered whole. The counters are now read once the thread is joined.

Signed-off-by: Florentin Dubois <[email protected]>
Ready::from(&Event) folds mio's is_read_closed (EPOLLRDHUP, a
half-close) and is_write_closed (EPOLLHUP or EPOLLERR, a hang-up) into
one HUP bit, and the half-close check told them apart by ERROR alone.
When sozu's own read or write meets a client's reset before epoll
reports it, that call consumes the error and epoll then reports EPOLLHUP
without EPOLLERR. The session read it as a half-close and kept its
request in flight; ConnectionH1::writable also returned Continue on the
failed write. A client that reset mid-download kept its session for
about 28 seconds.

Two mechanisms, both needed:

- Ready::WRITE_CLOSED, a new bit in sozu-command-lib that
  Ready::from(&Event) raises for is_write_closed and a half-close never
  raises. Mux::frontend_exchange_in_flight treats it, like ERROR, as a
  hang-up whatever is in flight, except on a lingering frontend, whose
  write side sozu shut itself. This covers the hang-up with no write
  pending to fail, such as a request waiting for its backend.
- A failed write to the client closes the session
  (ConnectionH1::client_write_failed), and the connection then reports no
  pending write, so the delayed close does not wait for a flush that can
  never happen. This closes at the write itself, and also ends a session
  whose socket keeps failing with a non-reset error.

Tests: e2e rr_client_reset_mid_download over TLS and plaintext, 12 runs
each (red on the previous head); a_half_close_is_hup_but_not_write_closed,
a_reset_is_error_and_write_closed and
a_reset_consumed_by_a_write_is_write_closed_without_error on a real
epoll; unit a_hang_up_without_error_closes_a_waiting_request and
a_failed_write_to_the_client_closes_the_session.

Refs #1779

Signed-off-by: Florentin Dubois <[email protected]>
upgraded_frontend_events kept only HUP from the handshake's readiness,
so a hang-up seen during the handshake reached the mux as a half-close.
It now keeps WRITE_CLOSED as well.

display_ready never rendered WRITE_CLOSED, so a trace could not tell a
half-close from a hang-up. Readiness traces now carry a fifth column,
`C` for WRITE_CLOSED: a hang-up reads `---HC`, a half-close `---H-`.

The real-epoll probes in command/src/ready.rs and
a_failed_write_to_the_client_closes_the_session are gated on Linux,
whose readiness words they check.

The docs and the CHANGELOG now say a full hang-up closes an H1 session
at once; an H2 session with output pending still waits for a timeout,
tracked in #1792.

Refs #1779

Signed-off-by: Florentin Dubois <[email protected]>
Three edits of the previous commit left their last line run on into the
text that followed it, in Mux::frontend_exchange_in_flight's comment,
doc/lifetime_of_a_session.md and lib/src/protocol/mux/LIFECYCLE.md, and
doubled a parenthesis in the latter. Text unchanged otherwise.

Refs #1779

Signed-off-by: Florentin Dubois <[email protected]>
test_max_connections opens five connections over the limit; each sends
a complete request and waits in the accept queue. The test then
reconnects those clients, and Client::connect drops the old socket with
a plain close, a FIN. Since #1779 a complete request followed by a FIN
is a half-close that sozu serves, so once the limit let them in, the
queued requests reached the backend and the final
`!backend.accept(100)` saw one connection more than the test accounts
for.

The test now resets those connections (Client::reset: SO_LINGER zero,
then close) before reconnecting, which is what a client that really
goes away does, and sozu forwards nothing for them. Every assertion is
unchanged, `!backend.accept(100)` included, and the test still enforces
the limit: disabling SessionManager::check_limits fails it at
`assert!(i < 15)`, and a decr that releases two slots fails it at
`assert!(i >= 2)`.

Refs #1779

Signed-off-by: Florentin Dubois <[email protected]>
An HTTP/2 frontend whose client reset its connection while a response
was being sent kept its session until a timeout. A full hang-up is no
longer in flight since #1779, so Mux::ready_inner takes the delayed
close, which waits for the pending output; but no H2Shell write path
treated a write answering Error or Closed as fatal, and that output
could never leave.

H2Shell::record_write marks the connection when a write to the client
fails, at each socket write (flush_tls_records,
flush_output_to_socket, write_streams). The connection then reports no
pending write, and H2Shell::writable returns CloseSession, so the
session closes at once, as an H1 frontend does since #1779.

Test: e2e test_rr_h2_client_reset_mid_download, 12 runs: an H2 client
with a large window reads about 280 KB of an 8 MiB response, then
resets; the backend connection must close within 2 s. Red on 00d1ff4.

The pinned code blocks of doc/h2_mux_internals.md follow the lines the
new field and helper moved.

Fixes #1792

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the fix/mux-h2-reset-mid-download branch from 774ac5e to 62a2b69 Compare October 2, 2026 06:52
Resolve the conflict in lib/src/protocol/mux/h1.rs, where the no-stream
TLS flush of `ConnectionH1::writable` gained `client_write_failed` here
and the `status == SocketResult::Continue` guard on main
(#1787): keep both, closing on a failed client write
first, then queueing a synthetic WRITABLE only after `Continue`.

Adapt `a_tls_flush_that_did_not_continue_leaves_writable_to_the_kernel`
(#1787) to this branch's semantics: on a server-side
connection an `Error` flush now closes the session through
`client_write_failed` and stops reporting output, while every arm still
asserts that WRITABLE is not re-raised. Its red recipe now drops the
`Continue` guard, which the `WouldBlock` arms catch. Reword the matching
comment of the stream flush accordingly.

Separate this branch's CHANGELOG entry from the previous one with a
blank line, as between every other entry.

Signed-off-by: Florentin Dubois <[email protected]>
…nload

Bring in the updated base branch, which itself now carries origin/main.

Resolve the conflict in `H2Shell::flush_tls_records`
(lib/src/protocol/mux/h2.rs): main records every write status through
`self.core.note_write_status(status)` (#1790), and this
branch through `self.record_write(status)`. Keep both, in that order:
the first drops a refused flush's WRITABLE edge, the second remembers a
failed write to the client.

Re-pin the `readable()`, `writable()` and `flush_output_to_socket()`
code blocks of doc/h2_mux_internals.md to their lines in the merged
h2.rs.

Separate this branch's CHANGELOG entry from the previous one with a
blank line, as between every other entry.

Signed-off-by: Florentin Dubois <[email protected]>
Bring in #1784; no conflict.

Signed-off-by: Florentin Dubois <[email protected]>
…nload

Bring in the base branch's merge of origin/main (#1784).
Re-pin the `readable()`, `writable()` and `flush_output_to_socket()`
code blocks of doc/h2_mux_internals.md to their lines in the merged
h2.rs; `H2Shell::flush_tls_records` still calls
`self.core.note_write_status(status)` then `self.record_write(status)`.

Signed-off-by: Florentin Dubois <[email protected]>
Bring in #1795; no conflict.

Signed-off-by: Florentin Dubois <[email protected]>
…nload

Bring in the base branch's merge of origin/main (#1795);
no conflict, and the doc/h2_mux_internals.md pins still resolve.

Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS added a commit that referenced this pull request Oct 2, 2026
Fixes #1799

#1795 made `flush_tls` retry an interrupted write (`EINTR`). The other socket paths in `lib/src/socket.rs` still answered `SocketResult::Error` for it, which closes a healthy session on the pipe path and, with #1793, on the H1 and h2c mux paths. They now retry the call at once:

- `plain_socket_read` (plain `socket_read`);
- `plain_socket_write` (plain `socket_write`), inside the existing `MAX_LOOP_ITERATIONS` budget;
- `plain_socket_write_vectored` (plain `socket_write_vectored`);
- the `read_tls` call of `rustls_socket_read` (`FrontRustls::socket_read`): rustls propagates the transport's `Interrupted` from `read_tls`; the retry is tight around it, so the plaintext drain and the short-read probe are unaffected.

The plain write bodies move from `tcp_socket_write` / `tcp_socket_write_vectored` into `plain_socket_write` / `plain_socket_write_vectored`, generic over the transport like `plain_socket_read`, so tests can script an interrupted write. Counters are unchanged and stay in the bodies; the logging (same tiers and messages) stays in the `tcp_*` wrappers through `PlainWriteFault`.

Tests (each red with its retry arm removed, green with it):
- `an_interrupted_plain_write_is_retried`
- `an_interrupted_plain_vectored_write_is_retried`
- `an_interrupted_plain_read_is_retried`
- `an_interrupted_tls_read_is_retried`

`CHANGELOG.md` and `doc/lifetime_of_a_session.md` updated.

Not in this PR: the TLS handshake pump (`handshake_read` and the handshake `write_tls` arm in `lib/src/protocol/rustls.rs`) keeps the same mapping.



Signed-off-by: Florentin Dubois <[email protected]>
Bring in the squash of this branch's base, #1789, with
#1775, #1798 and #1802.

The squash carries the same changes as the base branch head this branch
had merged, so lib/src/protocol/mux/mod.rs, lib/src/protocol/mux/LIFECYCLE.md,
doc/lifetime_of_a_session.md, e2e/src/tests/tls_tests.rs and CHANGELOG.md
are resolved by keeping this branch's edits to that content over main:
the net change of this branch against origin/main is the same as it was
against the base branch head. The CHANGELOG keeps one copy of the
#1779 entry and one blank line between entries.

Re-pin the `readable()`, `writable()` and `flush_output_to_socket()`
code blocks of doc/h2_mux_internals.md to their lines in the merged
h2.rs.

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 1d7f708 into main Oct 2, 2026
48 of 52 checks passed
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(mux): an HTTP/2 client that resets mid-download keeps its session until a timeout

1 participant