Repository navigation
perf(tls): drop the EAGAIN recv of the handshake, its upgrade and split record reads - #1611
Merged
Merged
Conversation
…it record reads Closes #1609 Three causes, established with symbolized backtraces of every recv on a release build of b74d5d9: - handshake: TlsHandshake::readable called read_tls until WouldBlock, and a TLS 1.3 server wants to read again once its flight is queued, so the ClientHello and the client Finished were each followed by an EAGAIN. The read loop moves to handshake_read, which reuses the ShortReadProbe of #1606 (now pub(crate)) and stops on a short read, dropping READABLE. - upgrade: upgrade_handshake armed the mux frontend with READABLE unconditionally, so every TLS session began with a recv on a socket the handshake had just proved empty. upgraded_frontend_events arms it only when the handshake still held a READABLE edge or rustls holds plaintext or a close_notify; WRITABLE stays unconditional. - between two reads: the H2 path reads one frame header then one payload per call, and the call that emptied a record's plaintext issued an EAGAIN, once per request when multiplexed. FrontRustls::recv_memory carries the short-read/EAGAIN proof across calls; HttpsSession::update_readiness clears it on every event delivered for the frontend token, whatever the state, and HUP or ERROR turns it off for good so the EOF behind a short read is still read. Contract unchanged from #1606: mio is edge-triggered, a byte arriving after the proof raises an event that is pending or already delivered. TCP urgent data remains the known limit. Measured (release, one worker, 20 requests, fdtrace): H2 one connection per request EAGAIN 56-60 -> 0, total 31.75-32.90 -> 29.45-30.15 per connection; multiplexed EAGAIN 21-22 -> 0, total 14.85-15.00 -> 13.70-14.30 per request. intentrace -p multiplexed 15.45 -> 14.50. Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS
requested review from
Wonshtrum and
llenotre
as code owners
September 27, 2026 04:42
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 #1609
Causes (established, not guessed)
Every
recvon the frontend socket was captured with backtraces (LD_PRELOADfd tracer, release build ofb74d5d97) and symbolized withaddr2line. The EAGAINrecvs fall into three call sites, told apart by their frames:rustls.rs:159←TlsHandshake::readablesocket.rs:857←h2.rs:8354←mux/mod.rs:2307←https.rs:1128(theself.ready()re-entered afterupgrade())https.rs:1122(normal event path)read_tlsran untilWouldBlock. A TLS 1.3 serverwants_read()again as soon as its flight is queued (rustls-0.23.45/src/common_state.rs:674-684), so both the ClientHello (1575 bytes) and the clientFinished(80 bytes) were followed by an EAGAIN.upgrade_handshakeinsertedREADABLE | WRITABLEinto the new mux frontend without looking at the socket, right after the handshake got EAGAIN on it. That is a read with no event.socket_readper call. Take an 88-byte record: call 1 reads it with a shortrecvand returnsContinue, because its buffer is full and plaintext remains. Call 2 is served from plaintext. Call 3 finds the plaintext empty, and sincedrainedwas a local, it issues anotherrecv, which answers EAGAIN. The trace shows noepoll_waitbetween the shortrecvand the EAGAIN.Changes
handshake_read(lib/src/protocol/rustls.rs) is generic overRead. It wraps the stream in the existingShortReadProbe(nowpub(crate), withnew/was_short, not duplicated) and stops on a short read, dropping READABLE, the same way EAGAIN does.upgraded_frontend_events(lib/src/https.rs):!wants_read(), meaning rustls holds plaintext (an H2 preface that shared a segment with theFinished) or aclose_notify.event.insert(READABLE)in the handshake'sUpgradebranch is removed, because it defeated this check.RecvMemoryinFrontRustls(lib/src/socket.rs):recvanswered short or EAGAIN, and no event has been delivered since". It seedsdrainedinrustls_socket_read, so a later call serves the plaintext and then answersWouldBlockwithout arecv.HttpsSession::update_readinessclears it for every event on the frontend token, in every state (Mux and WebSocket/Pipe). That is the only path events take into a session (server.rs:3943).Connection::socket_handler_mutandPipe::front_socket_handler_mut.Why it is safe
EPOLLET), and "an event will be generated upon each receipt of a chunk of data" (man 7 epoll). A byte that arrives after the proof raises an event. Either that event is still pending in epoll, and the nextepoll_waitdelivers it and re-arms READABLE, or it has already been delivered, which cleared the memory. The memory is cleared on delivery rather than inferred from READABLE: a delivery that lands while the mux still holds READABLE is invisible to the mux.update_readiness_after_read). The handshake closes on HUP before reading anything (TlsHandshake::ready), so it needs no exception.recvstops before a TCP urgent mark even when bytes are queued behind it. A peer that sends OOB data stalls until its next send, in the handshake as in the established session since perf(socket): stop reading on a short read instead of reading until EAGAIN #1606. TLS never uses urgent data, and HAProxy and tokio behave the same way.Prior art
ssl_sock_handshake(src/ssl_sock.c:6462) returns onSSL_ERROR_WANT_READand subscribes for the next receive (src/ssl_sock.c:6676-6682). Its BIO (ha_ssl_read,src/ssl_sock.c:372) reads throughraw_sock_to_buf, which stops onret < try(src/raw_sock.c:315-317).MidHandshake::poll(tokio-rustls-0.26.4/src/common/handshake.rs:89-91) loopshandshake(), which callsread_tlsuntilPending(src/common/mod.rs:161-170). ThatPendingcosts norecvafter a short read, because tokio clears readiness when0 < n < len(tokio-1.53.1/src/io/poll_evented.rs:211-213).Measurement
Release builds of
b74d5d97and this branch,crypto-ring,opentelemetry,splice,simd, one worker, loopback python backend, 20 requests per pass, three passes per cell, LOAD1 < 3.recvepoll_waitintentraceoutput is stripped of ANSI codes and counted on the syscall name. The intentrace total for one connection per request does not move. Under ptrace the worker is slow enough that the client's next flight lands during the pass. The removedrecvthen found data instead of EAGAIN, and anepoll_waitnow collects it: this is the same trade #1606 made. With the lighter fd tracer the saving is about 2.5 syscalls per connection. Without a tracer, and on a real network where the round trip is far longer than one pass, the removedrecvanswers EAGAIN.Tests (each seen red, then green)
a_short_handshake_read_stops_without_an_eagain,a_full_handshake_read_reads_again(ClientHello padded past 4096 bytes: one full read, then one short) anda_client_hello_in_three_segments_completes_on_a_loopback_socket(real socket + mio). All three are red with the probe check disabled and the forcedevent.insert(READABLE)restored.the_upgraded_frontend_reads_only_when_a_read_is_due: red against an unconditionalREADABLE | WRITABLE.frames_sharing_one_record_cost_one_recv(formerly…_plus_the_final_eagain, 2 → 1 reads, 0 EAGAIN),an_event_after_a_short_read_lets_the_next_call_readanda_hup_delivered_after_a_short_read_lets_the_next_call_read_the_eof. Each guard was removed in turn:event_deliveredemptied: 6 red.a_large_body_over_tls_arrives_intactnow pins 0 EAGAIN instead of 1. No assertion was loosened.Docs
CHANGELOG.md.doc/lifetime_of_a_session.md§2.2 and §4.doc/h2_mux_internals.md.https.rsadditions are re-anchored to the same text:doc/configure_admin_ops.md,doc/lifetime_of_a_session.md,lib/src/protocol/kawa_h1/LIFECYCLE.md.Signed-off-by: Florentin Dubois [email protected]