Skip to content

fix(mux): stop spinning on a blocked TLS write when the client stops reading - #1787

Merged
FlorentinDUBOIS merged 2 commits into
mainfrom
fix/mux-tls-backpressure-spin
Oct 2, 2026
Merged

FlorentinDUBOIS merged 2 commits into
mainfrom
fix/mux-tls-backpressure-spin

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

When a TLS HTTP/1.1 client stops reading a large response, ConnectionH1::writable re-raised its WRITABLE event after a write that answered WouldBlock, because rustls still held the records the kernel refused. Mux::ready_inner then retried that write on every inner iteration until MAX_LOOP_ITERATIONS, counting http.infinite_loop.error.

Fix: signal a pending write only when the write did not block; the kernel's next writable edge resumes the flush. Plain sockets are unaffected.

Test: e2e test_tls_h1_stalled_reader_does_not_exhaust_loop_budget (http.infinite_loop.error stays 0 while the client stalls, and the whole 8 MiB body arrives once it reads again).

Fixes #1780

…reading

When a TLS HTTP/1.1 client stopped reading a large response, rustls
kept the records the kernel refused and ConnectionH1::writable re-raised
its WRITABLE event because socket_wants_write() was still true, right
after the write had answered WouldBlock and cleared it. Mux::ready_inner
then called that write again on every inner iteration, each answering
WouldBlock, until MAX_LOOP_ITERATIONS counted http.infinite_loop.error.

The pending write is now signalled only when the write did not block,
so the session waits for the kernel's next writable edge. Plain sockets
are unaffected: they never report a pending write.

Fixes #1780

Signed-off-by: Florentin Dubois <[email protected]>
`ConnectionH1::writable` now signals a pending TLS write only when the
write answered `SocketResult::Continue`, at both flush sites. An `Error`
re-raised WRITABLE and retried the failing write.

A TLS write that meets a socket error other than a reset now sets
`FrontRustls::peer_reset`, so the records rustls still holds stop counting
as a pending write: they can never be delivered, and they kept an H1
session's close on the hang-up delayed behind them.

The e2e stalled-reader test shrinks the client's SO_RCVBUF before the
handshake, so the response always overflows it whatever the host's TCP
buffer sizes. LIFECYCLE.md scopes the WRITABLE rule to ConnectionH1 and
names the H2 follow-up (#1788); the CHANGELOG also records the end of
the H1 shutdown-tick spin in `drive_frontend_shutdown_io`.

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the fix/mux-tls-backpressure-spin branch from 26f48a3 to 16603d3 Compare October 2, 2026 00:11
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 6959377 into main Oct 2, 2026
28 checks passed
FlorentinDUBOIS added a commit that referenced this pull request Oct 2, 2026
Brings in #1787 and #1782; the only conflicts were pinned h2.rs line
citations in doc/h2_mux_internals.md, repointed.

Also fixes the merged reset-stream handling:

- An empty DATA frame without END_STREAM on a stream this endpoint reset
  never reached handle_data_frame, so it skipped the empty-DATA flood
  counter (CVE-2019-9518) and fit any window allowance for free. The
  was_reset_locally branch now counts it toward that limit like a live
  stream does.
- The DATA and header-block allowances are held for at most 512 reset
  streams: a stream refused or reset untracked stays in rst_sent and
  never enters the ring its allowances are dropped with.
- The docs say "the stream's initial receive window", state how empty
  DATA counts, and note that a backend connection's allowances are
  bounded by the backend's own concurrency, not the emitted-RST cap.

Refs #1783

Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS added a commit that referenced this pull request Oct 2, 2026
…s reading (#1790)

Fixes #1788

`ConnectionH2::ensure_tls_flushed` re-raised WRITABLE whenever rustls still held records, regardless of what the last socket write answered. After a write the kernel refused with `WouldBlock`, `Mux::ready_inner` re-ran the write pass on every inner iteration until `MAX_LOOP_ITERATIONS` (`http.infinite_loop.error`). The same re-raise kept `drive_frontend_shutdown_io` looping on a backpressured TLS H2 session.

The connection now records whether the latest socket write of a pass answered `WouldBlock`; if so, no synthetic WRITABLE is raised and the event is consumed (also on the empty-buffer TLS flushes, whose status was discarded). The kernel's next writable edge resumes the flush. Close decisions (`h2_close`) still read whether rustls holds records, so GOAWAY/close_notify records are flushed before the connection closes.

Notes:
- Test behaviour change: the two stalled control-frame drain tests now stall on a zero-length write the kernel did not refuse (the case the re-arm still exists for); the real-rustls GoAway/Error tests assert that a refused pass queues no event and that the pass triggered by the kernel edge flushes and closes.
- `SocketResult::Error` is not treated as refused here; #1787 makes a TLS socket error mark the transport dead, which removes the pending records.
- Overlaps #1787 on the CHANGELOG "Fixed" head; the `connect_with_small_receive_buffer` helper appended to `e2e/src/tests/h2_utils.rs` is byte-identical in both, and the two branches merge cleanly.

Tests: `test_tls_h2_stalled_reader_does_not_exhaust_loop_budget` (e2e, red on main: 2 loop-budget errors; green: 0, full 32 MiB body received once the client reads again) and `a_refused_goaway_flush_waits_for_the_kernel_edge_then_closes` (unit).



Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS added a commit that referenced this pull request Oct 2, 2026
…n every TLS write error (#1795)

Fixes #1794

Follow-up to #1787 and #1790.

- `flush_tls` retries a write interrupted by `EINTR` (as `proxy_protocol/relay.rs` and `udp.rs` do) instead of answering `Error` and marking the channel dead. `flush_tls` is now generic over `io::Write` so the retry is unit-tested.
- `FrontRustls::socket_write` and `socket_write_vectored` set `peer_reset` on every `SocketResult::Error` they return (one assignment per function, replacing the five per-arm ones). The rustls writer failure and the loop-budget arms returned `Error` without it, so the records kept reading as pending and the session waited for its timeout. Those two arms cannot be driven from a test with rustls 0.23 (its plaintext writer does not fail, and every loop iteration makes progress or stops); they are covered by the shared assignment.
- Tests, each seen red with its line removed: one per TLS write path (empty flush, vectored whole write, vectored partial write, empty vectored flush), the EINTR retry, and on H2 `a_refused_control_frame_drain_leaves_writable_to_the_kernel` (`(0, WouldBlock)` sibling of the two control-drain tests, red without the refusal record in `consume_output_flush`) and `a_refused_stream_write_leaves_writable_to_the_kernel` (red without the one in `handle_write`, which the `SkipFlush` finalize path depends on).
- Docs: `a_rustls_frontend_in_{error_state,goaway}_re_arms_until_its_records_drain` renamed to `..._stays_open_until_its_records_drain`; the CHANGELOG/LIFECYCLE sentence about those tests now says the test drains the records and the next pass closes; LIFECYCLE item 12 covers `ConnectionH2`; the `force_disconnect_over_a_real_rustls_frontend_*` assertion explains why it still re-signals (no refused write recorded in a pass); `peer_reset` and `doc/lifetime_of_a_session.md` no longer say "any other error but WouldBlock".



Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS added a commit that referenced this pull request Oct 2, 2026
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]>
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): http.infinite_loop.error increments when a TLS H1 client stops reading a large response

1 participant