Skip to content

fix(socket): retry interrupted TLS writes and mark the channel dead on every TLS write error - #1795

Merged
FlorentinDUBOIS merged 4 commits into
mainfrom
fix/mux-tls-write-error-followups
Oct 2, 2026
Merged

FlorentinDUBOIS merged 4 commits into
mainfrom
fix/mux-tls-write-error-followups

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

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".

…n every TLS write error

flush_tls now retries a write the kernel interrupted (EINTR), as the
relay and the UDP path do, instead of answering Error and marking the
channel dead. FrontRustls::socket_write and socket_write_vectored set
peer_reset on every Error they return rather than only in the socket
error arms: a rustls writer failure and the write loop's budget left the
records reading as pending, so a session could only close on its
timeout.

Tests cover each write path's error, the EINTR retry, and the H2 refusal
records of consume_output_flush and handle_write (the WouldBlock
siblings of the control-frame drain tests and a refused stream write).
The real-rustls GoAway/Error tests are renamed to what they assert, and
LIFECYCLE item 12, the session lifetime doc and the CHANGELOG are
corrected.

Fixes #1794

Signed-off-by: Florentin Dubois <[email protected]>
a_refused_control_frame_drain_leaves_writable_to_the_kernel, added on this
branch, queues its RST_STREAM with ConnectionH2::enqueue_rst(id, error).
#1756 gave enqueue_rst a third RstOrigin argument and updated the existing
drain tests, so once main was merged in this new test no longer compiled:
the two changes touched different lines and merged without a textual
conflict.

Pass RstOrigin::Local, as the sibling drain test just above it does: the
reset is one sozu queues itself, not one a peer provoked, and the test only
needs one queued RST_STREAM to drain.

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit dc25d2f into main Oct 2, 2026
23 checks passed
FlorentinDUBOIS added a commit that referenced this pull request Oct 2, 2026
Bring in #1795; no conflict.

Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS added a commit that referenced this pull request Oct 2, 2026
…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]>
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]>
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(socket): retry interrupted TLS writes and mark the channel dead on every TLS write error

1 participant