Skip to content

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

Merged
FlorentinDUBOIS merged 1 commit into
mainfrom
perf/h2-close-syscalls
Sep 27, 2026
Merged

FlorentinDUBOIS merged 1 commit into
mainfrom
perf/h2-close-syscalls

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

Closes #1607

What changes

Closing an H2 frontend over TLS on its final GOAWAY cost two writev(2) back to back, in the same pass:

recv    fd=16 ret=48            <- the client's GOAWAY
writev  fd=16 ret=39 len=39     <- our GOAWAY: H2Shell::flush_zero_to_socket -> FrontRustls::socket_write -> write_tls
writev  fd=16 ret=24 len=24     <- close_notify: H2Shell::close -> drain_tls_close_notify -> write_tls
close

FrontRustls::socket_write runs write_tls right after handing rustls the plaintext, so the GOAWAY is on the wire before anyone can queue the alert behind it.

  • New SocketHandler::socket_write_then_close(buf). Default body: socket_write, then socket_close if the whole of buf was taken, which is what every other handler did already. FrontRustls overrides it: writer().write(buf), then send_close_notify() only if all of buf was absorbed, then one write_tls, which rustls turns into one write_vectored over both records (rustls-0.23.45/src/vecbuf.rs, ChunkVecBuffer::write_to).
  • H2Shell::flush_zero_to_socket uses it when ConnectionH2::zero_flush_closes_connection() holds: Position::Server, H2State::GoAway (only goaway() sets it, after serialising the final GOAWAY), expect_write == Some(Zero) (the zero-buffer resume, not a GOAWAY deferred behind a half-written stream frame, nor a WINDOW_UPDATE/RST_STREAM batch), no stream, no pending WINDOW_UPDATE or RST_STREAM, close_notify_sent unset. The writable arm of GoAway then closes; H2Shell::close finds nothing left to drain. close_notify_sent is set once the whole buffer was taken, so neither initiate_close_notify nor drain_tls_close_notify queues a second alert.

The close_notify stays mandatory (RFC 8446 §6.1) and still follows the GOAWAY on the wire; only the number of writes changes. rustls encrypts application data straight into its send buffer once the handshake is over, which it always is on a connection the mux drives, so the alert queued after the plaintext is the next record.

HAProxy (0ceb8c65f) keeps two writes on this path: h2c_send_goaway_error (src/mux_h2.c:2504) puts the GOAWAY in the mux buffer, the timeout path flushes it with xprt->snd_buf (src/mux_h2.c:5540-5548), and h2_release (src/mux_h2.c:1607) then calls conn_xprt_shutw (src/mux_h2.c:1658), whose ssl_sock_shutw (src/ssl_sock.c:8001) issues its own SSL_shutdown (src/ssl_sock.c:8041). There is no h2_shutw in that tree: h2_do_shutw (src/mux_h2.c:5963) and h2_shut (src/mux_h2.c:6090) handle a stream, not the connection.

Measurements

Release build, one worker, python http.server backend, 20 sequential curl --http2 requests each on its own TLS connection, LOAD1 < 8, b19d94f8 before, this branch after. fdtrace = LD_PRELOAD fd tracer; intentrace = intentrace -p, ANSI stripped, anchored on the syscall name.

Initial measurement on b19d94f8: fdtrace writev 6.90/conn, epoll_wait 8.00/conn, total 33.80/conn; intentrace writev 6.00, epoll_wait 5.00, total 42.10.

per 20 connections / per connection before (4 fdtrace runs) after (3 fdtrace runs)
close writes (len=39, len=24, len=63) 35–40 20
merged len=63 writes 0 17–19
writev / conn 6.85–7.15 6.05–6.30
epoll_wait / conn 8.00–8.30 8.00–8.40
intentrace writev / conn 6.00 (2 runs) 6.00

The connections without a merged write closed on the client's HUP (ev=5:2005) before any GOAWAY was read: sozu sends the 24-byte alert alone there, before and after. Under intentrace -p every connection takes that path (wrote 39 Bytes: 0 on all three runs, wrote 24 Bytes: 20), because ptrace slows the worker enough for curl's GOAWAY, close_notify and FIN to all be queued when it wakes; so intentrace cannot see this change. writev also varies with how the response split (181 + 70 against 229, on 0 to 6 connections per run).

H2 backends: nothing done for the EOF-as-HUP of #1605

#1605 records a backend EOF read with the last bytes as HUP for H1 backends. 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 calls goaway(), which clears expect_read, and ConnectionH2::force_disconnect's Position::Client arm sets readiness.event = Ready::HUP itself, so the backend is closed in the same pass.
  • H2 reads are exact-sized (H2ReadTarget::Fill { amount }: 9 header bytes, then the payload). perf(socket): stop reading on a short read instead of reading until EAGAIN #1606's plain_socket_read answers Continue when sz == buf.len(), so a complete frame is not a short read and the next header read meets (0, Closed) in the same pass, exactly as on main. perf(socket): stop reading on a short read instead of reading until EAGAIN #1606 moves nothing here.
  • What remains is a FIN without GOAWAY landing between the epoll_wait and the recv: at most one epoll_wait per backend H2 connection, never per request, and not measurable with an H1 backend.

Tests (seen red first)

the_final_goaway_and_close_notify_leave_in_one_tls_write (h2.rs): over a real FrontRustls on a loopback socket, drives goaway(NoError), the writable pass (which must answer CloseSession) and close, and counts every write_tls holding a record (crate::socket::tls_writes, a #[cfg(test)] thread-local counter in the new flush_tls helper every FrontRustls write goes through). Then it reads the wire raw and checks the records in order (39 bytes, then 24), and decrypts them: the 17-byte GOAWAY with NO_ERROR, then a clean end of stream, which rustls reports only after close_notify.

  • Red with the zero_flush_closes_connection branch dropped: left: 2, right: 1 on the write count.
  • Red with send_close_notify queued before writer().write: records [24, 39].

Checks

cargo build --locked --no-default-features --features crypto-ring,opentelemetry,splice,simd, cargo +nightly fmt --all -- --check, clippy with the same features --all-targets -- -D warnings, tests for sozu-lib (1217 passed), sozu, sozu-command-lib, sozu-e2e -- --skip tests::fuzz_tests:: (449 passed), sozu-sim test and clippy under --cfg tokio_unstable, RUSTDOCFLAGS="-D warnings" cargo doc --no-deps --all-features --locked, and check_doc_citations.py (--self-test, --root ., --base b19d94f8…, --audit --root .): all exit 0. 14 line citations in doc/ and LIFECYCLE.md re-anchored to the same text in the same function.

Rebase note for #1606: this branch touches socket.rs on the write side only (flush_tls at the five write_tls sites, the trait method, the FrontRustls override); its hunks start at old lines 65, 582, 931, 972, 1124, 1157, 1192 and 1249, none overlapping #1606's (192, 608–733 on the read path, 1263). mux/mod.rs is untouched.

Signed-off-by: Florentin Dubois [email protected]

…rite

Closing an H2 frontend over TLS on its final GOAWAY cost two writev(2)
back to back in the same pass: the GOAWAY, flushed by
H2Shell::flush_zero_to_socket through FrontRustls::socket_write, which
runs write_tls at once, then the close_notify from H2Shell::close.

When the zero buffer holds that final GOAWAY on a server connection with
no stream, WINDOW_UPDATE or RST_STREAM left to write after it
(ConnectionH2::zero_flush_closes_connection), the flush goes through the
new SocketHandler::socket_write_then_close. FrontRustls hands rustls the
GOAWAY, queues the alert behind it and flushes both records with one
write_tls, which rustls sends with one write_vectored. The close_notify
is still sent (RFC 8446 section 6.1) and still follows the GOAWAY; only
the number of writes changes. The trait's default body writes and then
calls socket_close, so other handlers behave as before.

Release build, 20 curl --http2 requests each on its own TLS connection,
LD_PRELOAD fd tracer: close writes 35-40 -> 20 per 20 connections,
writev 6.85-7.15 -> 6.05-6.30 per connection, epoll_wait unchanged.

The test the_final_goaway_and_close_notify_leave_in_one_tls_write counts
the TLS writes from the GOAWAY to close over a real FrontRustls (1; 2
without the change) and reads the records off the wire in order.

Closes #1607

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS merged commit b74d5d9 into main Sep 27, 2026
28 checks passed
@FlorentinDUBOIS
FlorentinDUBOIS deleted the perf/h2-close-syscalls branch September 27, 2026 02:48
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(mux-h2): send the final GOAWAY and the TLS close_notify in one write

1 participant