Skip to content

fix(mux): deliver the whole response to a half-closed client - #1789

Merged
FlorentinDUBOIS merged 10 commits into
mainfrom
fix/mux-tls-half-close-truncation
Oct 2, 2026
Merged

FlorentinDUBOIS merged 10 commits into
mainfrom
fix/mux-tls-half-close-truncation

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

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

Fix:

  • Mux::ready_inner: a frontend HUP keeps the session only while input is unread (Connection::has_unread_input) or a stream whose request was received whole is open (Mux::frontend_exchange_in_flight); it closes once that response is complete. Pending output is flushed without close_notify. A full hang-up is never in flight and closes an H1 session at once (an H2 session with output pending still waits for a timeout, fix(mux): an HTTP/2 client that resets mid-download keeps its session until a timeout #1792): ERROR, or the new Ready::WRITE_CLOSED bit (sozu-command-lib), raised for mio's is_write_closed (EPOLLHUP/EPOLLERR) and never for a half-close; ERROR alone misses a reset whose error sozu's own read or write consumed, which epoll reports as EPOLLHUP without EPOLLERR. A request left incomplete at the client's EOF closes at once.
  • ConnectionH1::writable: a failed write to the client closes the session, and the connection then reports nothing left to flush.
  • TlsHandshake: reads past a HUP when a read is due, closes if the handshake is still incomplete afterwards (such a client can never send Finished), never counts HUP alone as loop work; FIN or reset during the handshake logs at debug.
  • upgraded_frontend_events: HUP survives the upgrade to the mux.
  • ConnectionH1::readable: a request whose stream was already answered (Unlinked, e.g. a 408 on an incomplete head) is never linked to a backend.

What changes, by client behavior:

Before After
H1 over TLS, half-close after a complete request session closed on the HUP; response cut or never sent response delivered whole, then close_notify and close
H1 over TLS, Finished + request + FIN together (TLS 1.3) closed during the handshake, request unread request read and answered, then close
H1 plaintext, half-close after a complete request session closed on the HUP; response cut or never sent response delivered whole, then close
H2 over TLS, half-close session closed on the HUP, after flushing only what was queued streams whose request was complete are answered, then the session closes
H2 plaintext not applicable: plaintext listeners serve H1 only unchanged
Half-close leaving the request incomplete (H1/H2, TLS/plaintext) closed on the HUP closed as soon as the client's EOF is read; the backend connection is closed, not left waiting
Half-close during the TLS handshake (before Finished) closed on the HUP closed once the bytes sent are read (no spin)
Reset (RST), with or without an open stream, whether epoll reports it (EPOLLERR) or sozu's own read/write consumes it first (EPOLLHUP alone), H1 closed on the HUP unchanged: closed at once
Same reset on H2 with output pending waits for a timeout (no H2 write path treats a failed write as fatal) unchanged; tracked in #1792
Reset mid-download, consumed by sozu's write closed on the HUP closed at the failed write
Frontend write failing with a non-reset error (H1) retried until the loop budget closed at the failed write
Client that close()s with a FIN only (no RST) and a complete request closed on the HUP indistinguishable from a half-close at the socket level: served until the client's RST, raised by sozu's next write, makes it a reset
Idle keep-alive connection, half-close closed unchanged: the EOF is read and the session closes
Lingering frontend drained to EOF unchanged

Sessions remain bounded by the frontend, backend and linger timeouts in every case.

Tests:

  • e2e (tls_tests.rs), TLS and plaintext: half-close after the request and mid-response with 8 MiB (red on main: 0 bytes, 290748 bytes); keep-alive close after the response; incomplete request + FIN closes at once (red on the previous head: closed after 10 s, backend left waiting); reset with a linked request closes without spinning (red on the previous head: http.infinite_loop.error 1).
  • e2e e2e_session_tls_client_fin_not_truncated no longer sleeps before half-closing (red on main: 0 bytes).
  • unit (rustls.rs): partial ClientHello + FIN, full ClientHello + FIN, with and without WRITABLE settle in a few passes (red: 100000 passes).
  • e2e test_rr_client_reset_mid_download_{tls,plain} (12 runs each): client reads ~280 KB of 8 MiB, resets; the backend connection must close within 2 s (red on 245fd69: still open after 3 s).
  • unit (command/src/ready.rs, real epoll): a half-close is HUP without WRITE_CLOSED; a reset is ERROR and WRITE_CLOSED; a reset consumed by a write is WRITE_CLOSED without ERROR.
  • unit (mux/mod.rs): a_hang_up_without_error_closes_a_waiting_request, a_failed_write_to_the_client_closes_the_session, a_lingering_frontend_reads_the_clients_last_bytes_before_closing (renamed from the fix(mux): a frontend hang-up spins the session loop to its iteration budget #1774 test; asserts no unread byte at the close), an_answered_request_is_never_linked.

Fixes #1779

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]>
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]>
Bring in #1784; no conflict.

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

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit b4473f1 into main Oct 2, 2026
51 of 52 checks passed
FlorentinDUBOIS added a commit that referenced this pull request Oct 2, 2026
Resolve lib/src/protocol/rustls.rs: this branch and #1789 each added
tests after a_full_handshake_read_reads_again. Keep both: the EINTR
handshake tests, then #1789's ClientHello-then-FIN tests. The code merged
cleanly, so the EINTR retries in handshake_read and handshake_write sit
beside #1789's routine-close handling and HUP-during-handshake close.
Reflow the handshake paragraph of doc/lifetime_of_a_session.md, which the
automatic merge left with one overlong line.

Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS added a commit that referenced this pull request Oct 2, 2026
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 added a commit that referenced this pull request Oct 2, 2026
## Summary

The TLS handshake still treated an interrupted syscall (`EINTR`) as fatal:

- `handshake_read` (`lib/src/protocol/rustls.rs`) sent an `Interrupted` error from `read_tls` to its generic arm, `HandshakeReadFault::ReadTls`, and `TlsHandshake::readable` closed the session;
- the write pump of `TlsHandshake::writable` sent an `Interrupted` error from `write_tls` to its generic arm, which logged `Could not perform handshake` and closed the session.

Both now retry the syscall at once, as `flush_tls` (#1795) and `rustls_socket_read` / the plain socket paths (#1802) do in `lib/src/socket.rs`. Only the syscall is retried. Readiness handling, the `ShortReadProbe` (it records only a successful read, so `was_short` stays exact) and the `READ_RESET`/`WRITE_RESET` counters are unchanged. The handshake write moves into `handshake_write`, which is generic over the transport like `handshake_read`, so a test can interrupt it.

With #1802 merged, this was the last socket path that still treated `EINTR` as fatal. `origin/main` is merged into this branch, first at `11f12b76`, then at `82058124` (after #1789 and #1804). The first merge separates the two CHANGELOG entries with a blank line and replaces #1802's sentence "The TLS handshake pump … keeps its mapping" with one saying the pump now retries the same way. The second merge keeps both sets of tests in `lib/src/protocol/rustls.rs`, where this branch and #1789 each added tests at the same spot. The code merged cleanly.

Fixes #1799

## Tests

- `an_interrupted_handshake_read_is_retried`: the first `recv` returns `Interrupted`. The test asserts that the retry reads the whole ClientHello short, READABLE drops, and the server queues its flight.
- `an_interrupted_handshake_write_is_retried`: the first write returns `Interrupted`. The test asserts that the retry writes the flight and the client processes it.

I wrote both tests before the retry arms, and both failed: `an interrupted read must be retried, not reported: ReadTls(Kind(Interrupted))` and `an interrupted write must be retried, not reported: Kind(Interrupted)`. After the fix, both pass.

## Validation (on the merge commit `07b036e8`)

- `cargo +nightly fmt --check` -> exit 0
- `cargo +1.93.1 clippy --all-features --all-targets --locked -- -D warnings` -> exit 0
- `cargo +1.93.1 test -p sozu-lib --lib --all-features --locked` -> `test result: ok. 1570 passed; 0 failed`
- `python3 .github/scripts/check_doc_citations.py --root . --base origin/main` -> exit 0
- `cargo test -p sozu-e2e --features opentelemetry,splice,simd tls -- --test-threads=4` -> `test result: ok. 44 passed; 0 failed`
- `cargo test -p sozu-e2e --features opentelemetry,splice,simd -- --test-threads=4 half_close client_fin max_connections` -> `test result: ok. 12 passed; 0 failed`




Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS added a commit that referenced this pull request Oct 2, 2026
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



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(mux): a half-closed TLS H1 client loses the end of a large response

1 participant