Skip to content

fix(h2): linger before closing after the final GOAWAY - #1768

Merged
FlorentinDUBOIS merged 2 commits into
mainfrom
fix/h2-goaway-lingering-close
Oct 1, 2026
Merged

FlorentinDUBOIS merged 2 commits into
mainfrom
fix/h2-goaway-lingering-close

Conversation

@FlorentinDUBOIS

@FlorentinDUBOIS FlorentinDUBOIS commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1764

Problem

Once an H2 frontend has flushed its final GOAWAY, it stops reading and closes the session at once. The client may still be sending frames it wrote before it read the GOAWAY (RFC 9113 §6.8). Closing with those frames unread, or receiving more after the close, makes the kernel send a reset. The reset discards response bytes still queued in sozu's socket, so the client can lose the end of a response it was otherwise served in full.

Fix

The close now uses the lingering close from #1742. The H1 and H2 frontends share Linger, LINGER_MAX_BYTES and drain_discard, which move to lib/src/protocol/mux/shared.rs. H1 behaviour is unchanged.

It applies when a writable pass that began after a final GOAWAY(NO_ERROR) decides CloseSession and the client has not hung up: graceful drain, soft stop, or the reply to the client's own GOAWAY. In that case H2Shell::linger_instead_of_closing:

  • Flushes TLS close_notify first. If it is still pending, the linger is Linger::Pending, and the next pass resumes it even though the state is Error by then.
  • Then calls shutdown(SHUT_WR), so the FIN follows everything sent.
  • Then reads the raw socket and drops what it reads, until the client's EOF, 4 MiB, or the listener's request_timeout, whichever comes first.

Other points:

  • ConnectionH2::arm_timeout and set_timeout_duration do not move the linger deadline. Mux::timeout_inner closes a lingering frontend first.
  • A GOAWAY that carries an error code (protocol error, flood) does not linger.
  • In Mux::shutting_down_inner, the H2 graceful-shutdown budget is checked before the linger, and the lingering readable/writable passes adopt the pass's clock, so an armed budget closes a lingering session when it elapses first. A drain started by the client's own GOAWAY does not arm that budget, so its linger runs to request_timeout.

Docs: LIFECYCLE.md §8.1, doc/h2_mux_internals.md, doc/configure.md (request_timeout), and the CHANGELOG. Pinned code blocks in doc/h2_mux_internals.md are re-anchored to the shifted h2.rs lines.

Tests

The red baseline for the first test is fix/h1-lingering-close (#1742, since merged), because it is the first tree that has Linger.

  • unit an_h2_frontend_lingers_after_its_final_goaway_so_the_client_reads_everything (mod.rs): a slow-reading client keeps sending after the final GOAWAY.
    • Red on an archive of the base with only the test file copied in: the client read 65509 of 1048576 bytes, then got ConnectionReset.
    • Green with the fix: everything arrives, followed by a clean EOF, and the session closes on the client's EOF.
  • unit a_linger_deferred_behind_close_notify_starts_on_the_next_pass (h2.rs, TLS): fails with got CloseSession when the Linger::Pending assignment is removed (checked in a copy of the tree).
  • the_final_goaway_and_close_notify_leave_in_one_tls_write: behavior change. The pass that flushes the final GOAWAY now starts the lingering close (Continue with is_lingering()) instead of returning CloseSession. Its write-count and record-order assertions are unchanged.
  • unit a_soft_stop_closes_a_lingering_h2_session_once_its_budget_elapses (mod.rs): a 1 s budget armed 3 s ago and a linger started 100 ms into it. Red on the first commit with only the test added: shutting_down() kept the session. Green once the lingering passes adopt the clock.

Once an H2 frontend had flushed its final GOAWAY it closed at once,
while the client could still be sending frames it wrote before reading
the GOAWAY. A close with those frames unread, or followed by more,
makes the kernel reset the connection and discard response bytes still
queued in the socket.

A close after a final GOAWAY(NO_ERROR) now uses the lingering close of
the H1 frontend (RFC 9113 §6.8, RFC 9112 §9.6): close_notify, then
shutdown(SHUT_WR), then read and drop what the client still sends until
its EOF, 4 MiB, or the listener's request_timeout. A GOAWAY carrying an
error does not linger, and the H2 graceful-shutdown budget still closes
a lingering session during a soft stop.

Fixes #1764

Signed-off-by: Florentin Dubois <[email protected]>
The lingering readable and writable passes returned before the code
that adopts the mux's clock snapshot, so graceful_shutdown_deadline_elapsed
compared against the instant the linger began, and a soft stop's H2
graceful-shutdown budget never closed a lingering session. Adopt the
snapshot on both lingering early returns, and state the linger's actual
bounds.

Refs #1764

Signed-off-by: Florentin Dubois <[email protected]>
@FlorentinDUBOIS
FlorentinDUBOIS force-pushed the fix/h2-goaway-lingering-close branch from 52c5f9b to ca1ffc2 Compare October 1, 2026 17:07
@FlorentinDUBOIS
FlorentinDUBOIS merged commit e222276 into main Oct 1, 2026
28 checks passed
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.

fix(h2): close without lingering after the final GOAWAY

1 participant