Skip to content

perf(tls): drop the EAGAIN recv of the handshake, its upgrade and split record reads - #1611

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
perf/tls-eagain
Sep 27, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
perf/tls-eagain

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

Closes #1609

Causes (established, not guessed)

Every recv on the frontend socket was captured with backtraces (LD_PRELOAD fd tracer, release build of b74d5d97) and symbolized with addr2line. The EAGAIN recvs fall into three call sites, told apart by their frames:

site frames H2, 20 connections (3 runs) multiplexed, 20 requests (3 runs)
handshake loop rustls.rs:159 ← TlsHandshake::readable 30–33 1–2
read forced at upgrade socket.rs:857 ← h2.rs:8354 ← mux/mod.rs:2307 ← https.rs:1128 (the self.ready() re-entered after upgrade()) 20 (one per connection) 1
between two calls same, but ← https.rs:1122 (normal event path) 5–9 19
  1. Handshake. read_tls ran until WouldBlock. A TLS 1.3 server wants_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 client Finished (80 bytes) were followed by an EAGAIN.
  2. Upgrade. upgrade_handshake inserted READABLE | WRITABLE into the new mux frontend without looking at the socket, right after the handshake got EAGAIN on it. That is a read with no event.
  3. Between two calls. This is the one perf(socket): stop reading on a short read instead of reading until EAGAIN #1606 kept on purpose. The H2 path reads a 9-byte header, then its payload, one socket_read per call. Take an 88-byte record: call 1 reads it with a short recv and returns Continue, because its buffer is full and plaintext remains. Call 2 is served from plaintext. Call 3 finds the plaintext empty, and since drained was a local, it issues another recv, which answers EAGAIN. The trace shows no epoll_wait between the short recv and the EAGAIN.

Changes

  • handshake_read (lib/src/protocol/rustls.rs) is generic over Read. It wraps the stream in the existing ShortReadProbe (now pub(crate), with new/was_short, not duplicated) and stops on a short read, dropping READABLE, the same way EAGAIN does.
  • upgraded_frontend_events (lib/src/https.rs):
    • WRITABLE is always set.
    • READABLE is set only if the handshake still held a READABLE edge, or if !wants_read(), meaning rustls holds plaintext (an H2 preface that shared a segment with the Finished) or a close_notify.
    • The event.insert(READABLE) in the handshake's Upgrade branch is removed, because it defeated this check.
  • RecvMemory in FrontRustls (lib/src/socket.rs):
    • It records "a recv answered short or EAGAIN, and no event has been delivered since". It seeds drained in rustls_socket_read, so a later call serves the plaintext and then answers WouldBlock without a recv.
    • HttpsSession::update_readiness clears 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).
    • HUP or ERROR turns it off for good.
    • At the upgrade it is seeded from what the handshake proved.
    • Two short accessors are added: Connection::socket_handler_mut and Pipe::front_socket_handler_mut.

Why it is safe

  • Same contract as perf(socket): stop reading on a short read instead of reading until EAGAIN #1606. mio is edge-triggered (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 next epoll_wait delivers 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.
  • HUP already seen ⇒ read to EOF. A FIN that came with the data is delivered as HUP, which turns the memory off, so the next call reads the EOF (update_readiness_after_read). The handshake closes on HUP before reading anything (TlsHandshake::ready), so it needs no exception.
  • The handshake never blocks. A ClientHello split across several segments completes. Each short read drops READABLE, no event is left pending, and the next segment raises the edge. This is pinned on a real loopback socket with mio.
  • OOB caveat (unchanged). recv stops 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

  • HAProxy: ssl_sock_handshake (src/ssl_sock.c:6462) returns on SSL_ERROR_WANT_READ and subscribes for the next receive (src/ssl_sock.c:6676-6682). Its BIO (ha_ssl_read, src/ssl_sock.c:372) reads through raw_sock_to_buf, which stops on ret < try (src/raw_sock.c:315-317).
  • tokio-rustls: MidHandshake::poll (tokio-rustls-0.26.4/src/common/handshake.rs:89-91) loops handshake(), which calls read_tls until Pending (src/common/mod.rs:161-170). That Pending costs no recv after a short read, because tokio clears readiness when 0 < n < len (tokio-1.53.1/src/io/poll_evented.rs:211-213).

Measurement

Release builds of b74d5d97 and this branch, crypto-ring,opentelemetry,splice,simd, one worker, loopback python backend, 20 requests per pass, three passes per cell, LOAD1 < 3.

scenario tool total per conn/req recv EAGAIN (per 20) epoll_wait
H2, 1 conn/request fdtrace 31.75–32.90 → 29.45–30.15 8.65–9.35 → 5.65–6.05 56–60 → 0 8.05–8.20 → 8.60–8.85
H2, 1 conn/request intentrace 41.00–41.05 → 41.00–41.10 5.00 → 4.00 n/a 4.90–4.95 → 5.90–6.00
H2 multiplexed fdtrace 14.85–15.00 → 13.70–14.30 3.95–4.15 → 2.90–3.15 21–22 → 0 4.05–4.25 → 4.10–4.30
H2 multiplexed intentrace 15.45 → 14.50 3.10 → 2.10 n/a 3.10 → 3.15

intentrace output 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 removed recv then found data instead of EAGAIN, and an epoll_wait now 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 removed recv answers 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) and a_client_hello_in_three_segments_completes_on_a_loopback_socket (real socket + mio). All three are red with the probe check disabled and the forced event.insert(READABLE) restored.
  • the_upgraded_frontend_reads_only_when_a_read_is_due: red against an unconditional READABLE | 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_read and a_hup_delivered_after_a_short_read_lets_the_next_call_read_the_eof. Each guard was removed in turn:
    • memory seed off: 3 red;
    • clear-on-event off: 3 red;
    • HUP guard off: 2 red;
    • event_delivered emptied: 6 red.
  • Existing socket tests that add bytes, or a FIN, between two calls now deliver the event those bytes raise, which is what the event loop does. a_large_body_over_tls_arrives_intact now 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.
  • Citations shifted by the https.rs additions 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]

…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
FlorentinDUBOIS merged commit 6b1db0c into main Sep 27, 2026
27 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the perf/tls-eagain branch September 27, 2026 05:37
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(tls): remaining EAGAIN recv in the TLS handshake loop and after the handshake upgrade

1 participant