Let HTTP/2 requests in flight complete after a graceful `GOAWAY`
Summary
A server that shuts down gracefully sends GOAWAY with NO_ERROR, and may still complete the streams up to last_stream_id (RFC 9113, section 6.8). Load balancers do this when they rotate connections. Today a request that still waits for response frames on that connection fails:
httpx2.RemoteProtocolError: <ConnectionTerminated error_code:0, last_stream_id:1, additional_data:None>
A caller that retries a POST after this error can run it twice. Discussion: #1362.
The exchange today and with this change:
sequenceDiagram
participant C as Client
participant S as Server
C->>S: request on stream 1
S->>C: HEADERS :status 200
S->>C: GOAWAY NO_ERROR,<br/>last_stream_id=1
alt Today
rect rgba(248, 81, 73, 0.15)
Note over C: raises RemoteProtocolError
S--xC: DATA "Hello, world!"<br/>(never read)
end
else With this change
rect rgba(63, 185, 80, 0.15)
Note over C: keeps reading,<br/>stream 1 is covered
S->>C: DATA "Hello, world!"
Note over C: returns 200 with the body
end
end
The cause has two layers:
h2closes its state machine on everyGOAWAY, and frames that arrive later raiseProtocolError. python-hyper/h2#1346 keeps the open streams alive and addsH2Connection.is_in_graceful_shutdown.httpcore2raisesRemoteProtocolErrorwhen a stream at or belowlast_stream_idneeds another network read after theGOAWAY. This PR changes that part.
What changes
_receive_events: a request keeps reading when theGOAWAYcovers its stream andh2reports a graceful shutdown._wait_for_outgoing_flowpasses its stream ID to_receive_events, so an upload that waits for flow control keeps going in the same case.
_wait_for_outgoing_flow also passes retry=False. An upload on a stream above last_stream_id then fails with RemoteProtocolError, as it does today. It must not raise ConnectionNotAvailable there: the pool would send the request again, and part of its body is already consumed.
What stays the same:
- A
GOAWAYwith an error code fails the requests that it covers withRemoteProtocolError. - The connection takes no new requests. It closes when its last response is closed.
- With a version of
h2that has nois_in_graceful_shutdown, a covered request fails as before.
Two costs:
- A covered request now keeps waiting for the server, and its read timeout applies to each read. Before, it failed when the
GOAWAYarrived. - The connection counts against
max_connectionsuntil its last response is closed.
Not changed here: last_stream_id=0 still fails the request instead of a retry, and a request that has not sent its headers when another stream reads the GOAWAY still gets RemoteProtocolError.
Tests
test_http2_connection_with_graceful_goaway: the response completes after theGOAWAY, and the connection takes no further request.test_http2_connection_with_graceful_goaway_in_two_stages: a secondGOAWAYwith the reallast_stream_id.test_http2_connection_with_graceful_goaway_then_disconnect: the server disconnects before it completes the request.test_http2_connection_with_graceful_goaway_during_upload: an upload that waits for flow control completes.test_http2_connection_with_goaway_during_unprocessed_upload: an upload on a stream abovelast_stream_idraisesRemoteProtocolError, not theConnectionNotAvailablethat makes the pool retry. It passes with and without this change.test_http2_connection_with_goaway_error: aGOAWAYwith an error code. It passes with and without this change.test_connection_pool_with_http2_graceful_goaway: two concurrent requests, theGOAWAYcovers one. That one completes, and the pool retries the other on a new connection.
The graceful tests are skipped when h2 has no is_in_graceful_shutdown. With h2 from python-hyper/h2#1346 the full suite passes with 100% coverage, and ruff, mypy and the unasync check pass. Without the source change, the five graceful tests fail.
In the load test of the discussion (1,000 POSTs, a GOAWAY after every 150 requests on a connection), 300 requests fail on main and 0 fail with this change.
Before this can merge
This needs the h2 release that contains python-hyper/h2#1346. Until the lock file has that version, the graceful tests are skipped and the coverage of the test files is below 100%. I will update the lock file when the release is out. I can also raise the minimum h2 version and drop the getattr, if you prefer that.
Checklist
- I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
- I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
- I've updated the documentation accordingly. (No page describes
GOAWAYhandling, so there is nothing to update.)
AI Disclaimer
This PR was developed with the assistance of Claude. I've reviewed and verified the changes.