Skip to content

perf(socket): serve buffered TLS plaintext before calling read_tls - #1596

Merged
FlorentinDUBOIS merged 2 commits into
mainfrom
perf/rustls-read-plaintext-first
Sep 26, 2026
Merged

FlorentinDUBOIS merged 2 commits into
mainfrom
perf/rustls-read-plaintext-first

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Sep 26, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #1588

What

FrontRustls::socket_read called read_tls on every entry, before looking at the plaintext rustls had already decrypted. The H2 read path asks for one 9-byte frame header and then one payload at a time, and one TLS record usually carries several frames, so every frame block after the first of a record cost a recv(2) that could only answer EAGAIN.

The read pass moves into the private rustls_socket_read helper (generic over Read, so tests can count reads) and now:

  1. drains the plaintext rustls already holds (while !wants_read(), unchanged gate);
  2. calls read_tls + process_new_packets only if the caller's buffer is still short;
  3. loops: a read_tls that returns bytes without completing a record produces no plaintext, so the pass reads again until EAGAIN, EOF or a full buffer.

Why it is safe

  • Readiness (edge-triggered mio): every EAGAIN still ends the call with SocketResult::WouldBlock (the drain ran first, so the buffer is not full), and update_readiness drops READABLE until the next event. A full buffer still answers Continue, so READABLE stays and the next call drains before it reads.
  • close_notify: the drain stays gated on !wants_read(), which is false once close_notify is received (rustls-0.23.45 src/common_state.rs:674-684); the reader answers Ok(0), and read_tls then returns Ok(0) without a recv (src/conn.rs:768-770), which maps to Closed as before. UnexpectedEof from the reader stays unreachable under that gate.
  • Several frames in one record: served from the plaintext buffer without a recv.
  • Partial record: handled by the loop, as before.

Prior art: tokio-rustls reads TLS only while wants_read() and then serves plaintext (tokio-rustls-0.26.4/src/common/mod.rs:198-212); HAProxy gets the same from OpenSSL, whose SSL_read (src/ssl_sock.c:7599) serves already-decrypted records without touching the BIO, and its "EAGAIN this turn" memory is the poller's fd_cant_recv/fd_recv_ready (src/raw_sock.c:262, :351), not the TLS layer.

Sticky Error contract

Draining first opened a gap: when process_new_packets fails on a corrupt record (or a fatal alert), rustls has already appended the plaintext of the valid records before it. Without a guard, the next socket_read served that plaintext as (n, Continue), and since the mux only drops READABLE on a frontend Error, frames from a dead session could be parsed once the peer sent again. FrontRustls::tls_fatal is now set on that failure, and every later call answers (0, Error) without draining. The failing call still returns what it had copied before reaching read_tls ((n, Error)). Tests: a_fatal_tls_error_stays_an_error_and_serves_no_buffered_plaintext (red on a76edbd8: left: (9, Continue), right: (0, Error)) and plaintext_served_before_the_fatal_error_is_returned_with_it.

One ordering change (please review)

EOF and close_notify are now answered Closed by the first call that finds no plaintext left, after the frames that preceded them. Before, a call that began with read_tls could answer (n, Closed) alongside the first of those frames: update_readiness then dropped READABLE, and the remaining frames stayed in rustls's buffer with no new event to fetch them (a FIN also raises HUP, which closes the session; a bare close_notify raises nothing). This is a reading of the code, not a measurement. A FIN cannot be seen earlier without the recv this change removes; close_notify could be (via process_new_packets().peer_has_closed), but only by restoring the stranding, so this PR does not.

No "EAGAIN seen this turn" memory

After the change, 13-18 of the 92-97 remaining EAGAIN answers (20 H2 requests) repeat one on the same socket within a single wakeup; the sampled one followed a re-read that found 127 fresh bytes, so such a memory would delay data to save them, and it would need the mux to reset it on every READABLE event. Left out.

Measurement

Release build (optimized + debuginfo), one worker, 20 requests to a local backend, each pass started at LOAD1 < 8. Before = b5169d44, after = this branch.

scenario instrument before after
H2, one connection per request intentrace -p, recvfrom/req 16.00 6.00
intentrace -p, syscalls/req 62.05 51.90-52.05
fd tracer, recv/req 20.70-20.90 11.35-12.30
fd tracer, recv EAGAIN (20 req) 262-264 92-97
H2, 20 requests multiplexed intentrace -p, recvfrom/req 9.35 4.10
intentrace -p, syscalls/req 25.05 19.80
fd tracer, recv/req 10.00-11.45 5.55-6.00
fd tracer, recv EAGAIN (20 req) 126-133 37

Tests

socket::rustls_read_tests (in-memory TLS 1.3 pair, counting transport): several H2 frames in one record (1 recv + the final EAGAIN, was 9), a large H1-style buffer, a fragmented record, close_notify after the frames, a FIN without close_notify (stays Closed, never Error), a reset, and the two sticky-Error cases above. Written against the old order first: the frame test failed with left: 9, right: 2, the fragmented one with left: 10, right: 3, and the two close tests with Closed instead of Continue on frame block 1 (the ordering change above).

Signed-off-by: Florentin Dubois [email protected]

FrontRustls::socket_read called read_tls on every entry, before looking
at the plaintext rustls had already decrypted. The H2 read path asks for
one 9-byte frame header and then one payload at a time, and one TLS
record usually carries several frames, so every frame block after the
first of a record cost a recv(2) that could only answer EAGAIN.

The pass, moved into the private rustls_socket_read helper, now drains
the buffered plaintext first and calls read_tls only while the caller's
buffer is still short, the order tokio-rustls follows. An EAGAIN still
ends the call with WouldBlock and a full buffer still answers Continue,
so the edge-triggered readiness contract is unchanged. EOF and
close_notify are now answered Closed by the first call that finds no
plaintext left, after the frames that preceded them.

Measured on a release build, 20 requests, H2 one connection per request:
recvfrom per request 16.00 -> 6.00 (intentrace), EAGAIN 262-264 -> 92-97
(fd tracer). H2 multiplexed: recvfrom per request 9.35 -> 4.10, EAGAIN
126-133 -> 37.

The helper is generic over Read so rustls_read_tests can count every
recv the pass issues.

Closes #1588

Signed-off-by: Florentin Dubois <[email protected]>
Draining buffered plaintext before read_tls opened a gap: when
process_new_packets fails on a corrupt record, rustls has already
appended the plaintext of the valid records that preceded it. The next
socket_read drained that plaintext and answered (n, Continue), so the
mux, which only drops READABLE on a frontend Error, would parse frames
from a session that had already failed once the peer sent again.

FrontRustls gains tls_fatal. It is set when process_new_packets fails,
and every later socket_read answers (0, Error) without draining. The
failing call still returns the plaintext it had copied before reaching
read_tls.

Also re-anchors the kawa_h1 LIFECYCLE.md citation of https.rs, shifted
by one line by the new field.

Refs #1588

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the perf/rustls-read-plaintext-first branch from 6829200 to 4b11f3f Compare September 26, 2026 21:54
@FlorentinDUBOIS
FlorentinDUBOIS merged commit 97a9799 into main Sep 26, 2026
23 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the perf/rustls-read-plaintext-first branch September 26, 2026 22:01
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.

perf(socket): FrontRustls::socket_read issues one EAGAIN recv per H2 frame while plaintext is already buffered

1 participant