Skip to content

Drain unread HTTP/1 request bodies after early responses - #400

Open
anish-yen wants to merge 4 commits into
pgjones:mainfrom
anish-yen:gh-397-drain-early-http1
Open

anish-yen wants to merge 4 commits into
pgjones:mainfrom
anish-yen:gh-397-drain-early-http1

Conversation

@anish-yen

Copy link
Copy Markdown

Fixes #397. Thanks to @stepan-romankov for the report and reproducer.

When an HTTP/1 application finishes its response before consuming the request body, _maybe_recycle() currently closes the connection because the peer is still in SEND_BODY. A large upload can then fail before the client reads the response.

This keeps parsing and discarding the unread body after closing the completed application stream. Once h11 reaches EndOfMessage, the connection can start its next request. The existing keep-alive timeout bounds clients that stop sending. Explicit connection closure, shutdown and malformed input still close the connection.

Tests cover fixed-length and chunked bodies, a pipelined next request, explicit closure, shutdown, malformed chunks, and drain timeouts under asyncio and Trio.

Validation on Python 3.12:

  • 39 targeted protocol/keep-alive tests pass. The three drain/reuse regression cases fail against unchanged main.
  • Full suite: 203 passed, 2 failed. The same two HTTP/2 WebSocket failures occur on unchanged main with this environment's h2 dependency (Ordinary CONNECT MUST NOT include :scheme or :path).
  • flake8, black and changed-file isort checks pass. mypy reports the same 12 existing errors as unchanged main.
  • Local real-network checks: three 8 MiB uploads per worker (asyncio and Trio) returned 401 and allowed a following request. Unchanged main raises httpx.ReadError. An incomplete body is closed at the configured keep-alive timeout on both workers.

AI assistance was used to develop the patch and tests and run these checks.

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.

HTTP/1.1: a response sent before the request body is read closes the connection, and the client can lose the response

1 participant