Wait for positive HTTP/2 flow-control credit when stream window goes negative#935
Merged
Kludex merged 5 commits intoMay 15, 2026
Merged
Conversation
`_wait_for_outgoing_flow` previously waited only while the available
flow was exactly zero. Per RFC 7540 §6.9.2, when a peer sends a
SETTINGS frame that reduces INITIAL_WINDOW_SIZE, every existing
stream send window is adjusted by the delta and may become negative.
With the `while flow == 0` guard, a negative window passed straight
through and h2 raised `LocalProtocolError("Cannot send N bytes, flow
control window is -M")` on the next send_data call.
Switching the guard to `while flow <= 0` keeps the stream parked
until WINDOW_UPDATE frames restore positive credit.
Refs: encode/httpcore#1082
Refs: encode/httpx#3601
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…ow-control-window
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
_wait_for_outgoing_flowinhttpcore2's HTTP/2 implementation to wait while the available flow is non-positive, not just exactly zero.INITIAL_WINDOW_SIZE, existing stream send windows are adjusted by the delta and may become negative. With the previouswhile flow == 0guard, a negative value would pass straight through and h2 would raiseLocalProtocolError("Cannot send N bytes, flow control window is -M")on the nextsend_datacall.while flow <= 0keeps the stream parked untilWINDOW_UPDATEframes restore positive credit.Refs: encode/httpcore#1082, encode/httpx#3601.
Test plan
test_http2_connection_with_negative_flow_control_windowadded in bothtests/httpcore2/_async/test_http2.pyandtests/httpcore2/_sync/test_http2.py. Reproduces the bug via a mock stream that sends a window-reducing SETTINGS frame after the default 65535-byte window is exhausted, then providesWINDOW_UPDATEcredit to finish the upload.tests/httpcore2/_async/test_http2.pyandtests/httpcore2/_sync/test_http2.pypass — 33/33.scripts/checkpasses (format / mypy / ruff).🤖 Generated with Claude Code