From f1e3f90c5e9d721c4905dd07769c3ecec33af67d Mon Sep 17 00:00:00 2001 From: Anuraag Agrawal Date: Thu, 22 Jan 2026 12:51:36 +0900 Subject: [PATCH] Drain request body in WSGI on request error Signed-off-by: Anuraag Agrawal --- src/connectrpc/_server_sync.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/connectrpc/_server_sync.py b/src/connectrpc/_server_sync.py index c711fb8..0773a82 100644 --- a/src/connectrpc/_server_sync.py +++ b/src/connectrpc/_server_sync.py @@ -554,8 +554,17 @@ def _request_stream( read_max_bytes: int | None = None, ) -> Iterator[_REQ]: reader = EnvelopeReader(request_class, codec, compression, read_max_bytes) - for chunk in _read_body(environ): - yield from reader.feed(chunk) + try: + for chunk in _read_body(environ): + yield from reader.feed(chunk) + except ConnectError: + if environ.get("SERVER_PROTOCOL", "").startswith("HTTP/1"): + # In HTTP/1, the request body should be drained before returning. Generally it's + # best for the application server to handle this, but gunicorn is a famous + # server that doesn't do so, so we go ahead and do it ourselves. + for _ in _read_body(environ): + pass + raise def _response_stream(