Repository navigation
perf(socket): serve buffered TLS plaintext before calling read_tls - #1596
Merged
Merged
Conversation
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 26, 2026 20:19
FlorentinDUBOIS
force-pushed
the
perf/rustls-read-plaintext-first
branch
from
September 26, 2026 20:23
b8d7719 to
a76edbd
Compare
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
force-pushed
the
perf/rustls-read-plaintext-first
branch
from
September 26, 2026 21:54
6829200 to
4b11f3f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1588
What
FrontRustls::socket_readcalledread_tlson 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 arecv(2)that could only answerEAGAIN.The read pass moves into the private
rustls_socket_readhelper (generic overRead, so tests can count reads) and now:while !wants_read(), unchanged gate);read_tls+process_new_packetsonly if the caller's buffer is still short;read_tlsthat returns bytes without completing a record produces no plaintext, so the pass reads again untilEAGAIN, EOF or a full buffer.Why it is safe
EAGAINstill ends the call withSocketResult::WouldBlock(the drain ran first, so the buffer is not full), andupdate_readinessdrops READABLE until the next event. A full buffer still answersContinue, so READABLE stays and the next call drains before it reads.close_notify: the drain stays gated on!wants_read(), which is false onceclose_notifyis received (rustls-0.23.45src/common_state.rs:674-684); the reader answersOk(0), andread_tlsthen returnsOk(0)without arecv(src/conn.rs:768-770), which maps toClosedas before.UnexpectedEoffrom the reader stays unreachable under that gate.recv.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, whoseSSL_read(src/ssl_sock.c:7599) serves already-decrypted records without touching the BIO, and its "EAGAIN this turn" memory is the poller'sfd_cant_recv/fd_recv_ready(src/raw_sock.c:262,:351), not the TLS layer.Sticky
ErrorcontractDraining first opened a gap: when
process_new_packetsfails on a corrupt record (or a fatal alert), rustls has already appended the plaintext of the valid records before it. Without a guard, the nextsocket_readserved that plaintext as(n, Continue), and since the mux only drops READABLE on a frontendError, frames from a dead session could be parsed once the peer sent again.FrontRustls::tls_fatalis now set on that failure, and every later call answers(0, Error)without draining. The failing call still returns what it had copied before reachingread_tls((n, Error)). Tests:a_fatal_tls_error_stays_an_error_and_serves_no_buffered_plaintext(red ona76edbd8:left: (9, Continue), right: (0, Error)) andplaintext_served_before_the_fatal_error_is_returned_with_it.One ordering change (please review)
EOF and
close_notifyare now answeredClosedby the first call that finds no plaintext left, after the frames that preceded them. Before, a call that began withread_tlscould answer(n, Closed)alongside the first of those frames:update_readinessthen 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 bareclose_notifyraises nothing). This is a reading of the code, not a measurement. A FIN cannot be seen earlier without therecvthis change removes;close_notifycould be (viaprocess_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
EAGAINanswers (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.intentrace -p,recvfrom/reqintentrace -p, syscalls/reqrecv/reqrecvEAGAIN (20 req)intentrace -p,recvfrom/reqintentrace -p, syscalls/reqrecv/reqrecvEAGAIN (20 req)Tests
socket::rustls_read_tests(in-memory TLS 1.3 pair, counting transport): several H2 frames in one record (1recv+ the finalEAGAIN, was 9), a large H1-style buffer, a fragmented record,close_notifyafter the frames, a FIN withoutclose_notify(staysClosed, neverError), a reset, and the two sticky-Errorcases above. Written against the old order first: the frame test failed withleft: 9, right: 2, the fragmented one withleft: 10, right: 3, and the two close tests withClosedinstead ofContinueon frame block 1 (the ordering change above).Signed-off-by: Florentin Dubois [email protected]