Skip to content

perf(mux-h2): send the final GOAWAY and the TLS close_notify in one write #1607

Closed

Description

@FlorentinDUBOIS

Observation

Closing an HTTP/2 frontend connection over TLS costs two writev(2) calls back to back: one for the final GOAWAY, one for the TLS close_notify.

Release build of b19d94f8, one worker, python http.server backend, 20 sequential curl --http2 requests each on its own TLS connection, LOAD1 < 8, fdtrace (LD_PRELOAD) and intentrace -p:

per connection fdtrace intentrace
writev 6.90 6.00
epoll_wait 8.00 5.00
total 33.80 42.10

The close of every connection whose client sent a GOAWAY ends with:

recv    fd=16 ret=48
writev  fd=16 ret=39 len=39   <- GOAWAY (17-byte frame in one TLS 1.3 record)
writev  fd=16 ret=24 len=24   <- close_notify alert
shutdown / close

No epoll_wait separates the two writes: they belong to the same pass.

Where the two writes come from

  • The GOAWAY: ConnectionH2::goaway serialises it into zero (lib/src/protocol/mux/h2.rs), and the next H2Shell::writable flushes it through flush_zero_to_socket → FrontRustls::socket_write, which hands the plaintext to rustls and immediately runs write_tls (lib/src/socket.rs).
  • The close_notify: the H2State::GoAway arm then disconnects, and H2Shell::close calls drain_tls_close_notify (lib/src/protocol/mux/shared.rs), i.e. send_close_notify and a second write_tls.

rustls's write_tls sends every queued record in one write_vectored (rustls-0.23.45/src/vecbuf.rs, ChunkVecBuffer::write_to), so queueing the alert after the GOAWAY plaintext and before the flush would put both records in one writev.

Proposal

When the zero buffer being flushed ends in the final GOAWAY of a server connection that closes right after it, hand rustls the GOAWAY, queue close_notify behind it, and flush once. The close_notify stays mandatory (RFC 8446 §6.1) and still follows the GOAWAY on the wire; only the number of writes changes. Expected: -1 writev per H2 TLS connection closed this way.

HAProxy does not merge them: h2_release (src/mux_h2.c) flushes the mux buffer and then calls conn_xprt_shutw, whose ssl_sock_shutw (src/ssl_sock.c) issues its own SSL_shutdown.

Out of scope: H2 backends and the EOF-as-HUP of #1605

#1605 records a backend EOF read with the last bytes as HUP for H1 backends only. For H2 backends there is nothing measurable to gain, with or without #1606:

  • A graceful backend close (GOAWAY, then FIN) never reads the EOF: handle_goaway_frame → goaway() clears expect_read, and force_disconnect's Position::Client arm writes Ready::HUP itself, so the backend closes in the same pass.
  • H2 reads are exact-sized (H2ReadTarget::Fill { amount }), so under perf(socket): stop reading on a short read instead of reading until EAGAIN #1606 a complete frame is a full read (Continue), not a short one, and the EOF is met by the next header read in the same pass exactly as on main.
  • What remains is a FIN without GOAWAY that lands between the epoll_wait and the recv: at most one epoll_wait per backend H2 connection, never per request.

Activity

  1. added a commit that references this issue on Sep 27, 2026
    b74d5d9
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