Skip to content

perf(tls): remaining EAGAIN recv in the TLS handshake loop and after the handshake upgrade #1609

Closed

Description

@FlorentinDUBOIS

Problem

After #1596 and #1606 two TLS read sites still issue a recv(2) that can only answer EAGAIN.

  1. Handshake read loop — TlsHandshake::readable (lib/src/protocol/rustls.rs:151-159 at 7c820e57) calls read_tls again until it answers WouldBlock, even after a recv that returned fewer bytes than the 4096 rustls offered (rustls-0.23.45/src/msgs/deframer/buffers.rs:208,220). After the ClientHello (≈1.5 KB) and after the client Finished, rustls still wants_read() (TLS 1.3 servers may send application data once their own flight is out), so each of these two short reads is followed by an EAGAIN. perf(socket): stop reading on a short read instead of reading until EAGAIN #1606 already stops the mux read path on a short read through ShortReadProbe (lib/src/socket.rs); the handshake does not use it.
  2. Mux read of an established TLS session — rustls_socket_read (lib/src/socket.rs:815 at 7c820e57), called from the H2 readable pass (lib/src/protocol/mux/h2.rs:8314), still answers EAGAIN although the records are ~100 bytes, so "a read that filled the 4096 bytes rustls offered" does not explain it. The mechanism has to be established before any change: an EAGAIN between two calls, which perf(socket): stop reading on a short read instead of reading until EAGAIN #1606 kept on purpose because a new edge may arrive between them, or a readable call made without a fresh event (a forced read, e.g. after the handshake upgrade).

Measurement (release, 7c820e57, one worker, loopback backend, 20 requests)

recv on the frontend TLS socket, LD_PRELOAD interposer with backtraces:

scenario handshake EAGAIN mux EAGAIN
H2, one connection per request (20 connections) 37 (≈1.85 / connection) ≈1.6 / connection
H2 multiplexed (1 connection, 20 requests) — ≈1.05 / request

Prior art

  • HAProxy drives the handshake with SSL_do_handshake and returns on SSL_ERROR_WANT_READ (src/ssl_sock.c, ssl_sock_handshake); its transport stops on a short read (src/raw_sock.c, fd_cant_recv when ret < try).
  • tokio-rustls drives the handshake one read_tls per poll and returns Pending on WouldBlock (tokio-rustls/src/common/handshake.rs).

Proposal

  • Reuse ShortReadProbe in the handshake loop: stop after a short recv, drop READABLE, rely on the edge-triggered re-arm (same contract as perf(socket): stop reading on a short read instead of reading until EAGAIN #1606: HUP already seen ⇒ keep reading to EOF). A ClientHello split over several segments must still complete.
  • Establish the cause of (2) with symbolized backtraces and fix only that cause, without removing a legitimate wake-up.

Activity

  1. added a commit that references this issue on Sep 27, 2026
    6b1db0c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions