Skip to content

Fix: HTTP/2 backpressure when the flow control window is exhausted - #398

Open
mandarinloop wants to merge 2 commits into
pgjones:mainfrom
mandarinloop:fix/h2-flow-control-backpressure
Open

mandarinloop wants to merge 2 commits into
pgjones:mainfrom
mandarinloop:fix/h2-flow-control-backpressure

Conversation

@mandarinloop

@mandarinloop mandarinloop commented Nov 18, 2026 •

Copy link
Copy Markdown

Part of #390 (the backpressure part; its other two points aren't addressed here).

StreamBuffer.pop resumed a producer blocked over the high water mark based on the size of the chunk it popped, not on what was left. With a slow client the flow control window limits pops to little or nothing, so every pop resumed the producer and the app's send() never waited: the whole body ended up buffered. We hit this in production behind Azure Container Apps' Envoy ingress, which talks HTTP/2 to the app over cleartext (h2c): a streamed download of ~330 MiB
got the server killed at its 512 MiB memory limit.

  • Resume the producer only once the buffer is below the low water mark. The existing push/pop test never let the push run, so it passed either way; it now does, and a new test pins the boundary.
  • Clear the resume signal before waiting, so a signal left over from an earlier drain doesn't let the next push over the high water mark through.

Testing

  • Each commit's new test fails without that commit and passes with it.
  • tox (py310 to py314, format, pep8, mypy) and h2spec (asyncio and Trio) show no new failures. the existing ones (test_http2_websocket, mypy, flaky Trio h2spec checks) fail the same way on main.
  • End to end: an h2c server streaming 4 MiB chunks to a client that grants 1 MiB/s of window grows from 26 to 268 MiB on main and stays at ~26 MiB with this branch.

It was resumed based on the size of the last pop, not what remained
buffered. With a slow client the flow control window keeps pops small,
so the buffer grew without limit.

This branch has not been deployed

No deployments
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.

1 participant