Skip to content

Commit 74a2727

Browse files
authored
Drain request body in WSGI on request error (#93)
Signed-off-by: Anuraag Agrawal <anuraaga@gmail.com>
1 parent eb09e84 commit 74a2727

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

src/connectrpc/_server_sync.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,8 +554,17 @@ def _request_stream(
554554
read_max_bytes: int | None = None,
555555
) -> Iterator[_REQ]:
556556
reader = EnvelopeReader(request_class, codec, compression, read_max_bytes)
557-
for chunk in _read_body(environ):
558-
yield from reader.feed(chunk)
557+
try:
558+
for chunk in _read_body(environ):
559+
yield from reader.feed(chunk)
560+
except ConnectError:
561+
if environ.get("SERVER_PROTOCOL", "").startswith("HTTP/1"):
562+
# In HTTP/1, the request body should be drained before returning. Generally it's
563+
# best for the application server to handle this, but gunicorn is a famous
564+
# server that doesn't do so, so we go ahead and do it ourselves.
565+
for _ in _read_body(environ):
566+
pass
567+
raise
559568

560569

561570
def _response_stream(

0 commit comments

Comments
 (0)