Support graceful shutdown after receiving GOAWAY(NO_ERROR)
BE
8d ago+709+709+012345678901234567890123456789-11-11-01234567890123456789
Fixes #1199. Covers the receive side of #1181 (sending-side graceful shutdown is out of scope). Companion mitmproxy PR: mitmproxy/mitmproxy#8580.
Problem
On GOAWAY(NO_ERROR), h2 transitions to CLOSED and rejects subsequent frames (e.g., the PING or in-flight response frames in a standard RFC 9113 §6.8 two-stage shutdown).
Design
- Graceful shutdown on
GOAWAY(NO_ERROR): When an open connection receivesGOAWAY(NO_ERROR),state_machine.stateremainsCLOSED(preserving existing reuse checks likehttpcore's) whileH2Connection.is_in_graceful_shutdownbecomesTrue. - Keep open streams alive: Frames for existing streams keep flowing, the outbound buffer is preserved,
PING/SETTINGSare still acknowledged, and normal client/server role rules remain enforced via a shadow of the open state (_graceful_shutdown_state). The peer may still open streams (includingPUSH_PROMISE). - Block new local streams:
send_headers(new stream ID) andpush_streamcall_check_graceful_shutdownand raiseProtocolErrorbefore mutating state, leaving graceful shutdown intact for open streams. - Terminal closure: An error
GOAWAY, protocol error, orclose_connection()ends graceful shutdown (leavingstatehard-CLOSED). A later gracefulGOAWAYnever reopens a closed connection. Once closed (not in graceful shutdown), leftover non-GOAWAYframes are ignored rather than raisingProtocolError. - Application boundary:
ConnectionTerminatedstill fires for everyGOAWAY(checkH2Connection.is_in_graceful_shutdownto distinguish graceful shutdown from terminal closure). Handling streams abovelast_stream_idand enforcing non-increasinglast_stream_idremain the application's responsibility.
Notes
- Builds on #1324 (
@adarshx01's commit and authorship preserved; connections in graceful shutdown are excluded from its post-close ignore guard). Thanks also to@zanieb,@dimaqq, and@neoLsHfor the design discussion in #1181. - Passes
pytest(100% line and branch coverage),ruff,mypy --strict-bytes, andsphinx-build.