From d301d53758364f8d3dc1f7313c293af3865afc17 Mon Sep 17 00:00:00 2001 From: Benoit Chesneau Date: Sat, 7 Feb 2026 09:00:46 +0100 Subject: [PATCH] fix: resolve RuntimeError when StopIteration raised in ASGI coroutine In Python 3.7+, PEP 479 converts StopIteration raised inside coroutines to RuntimeError. Changed _read_into() to raise NoMoreData instead, which is already properly handled by the protocol layer. --- gunicorn/asgi/message.py | 6 ++---- gunicorn/asgi/protocol.py | 3 --- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/gunicorn/asgi/message.py b/gunicorn/asgi/message.py index 86d97c77..73a6e7da 100644 --- a/gunicorn/asgi/message.py +++ b/gunicorn/asgi/message.py @@ -139,7 +139,7 @@ class AsyncRequest: async def _parse(self): """Parse the request from the unreader.""" buf = bytearray() - await self._read_into(buf, stop=True) + await self._read_into(buf) # Handle proxy protocol if enabled and this is the first request mode = self.cfg.proxy_protocol @@ -174,12 +174,10 @@ class AsyncRequest: self._set_body_reader() - async def _read_into(self, buf, stop=False): + async def _read_into(self, buf): """Read data from unreader and append to bytearray buffer.""" data = await self.unreader.read() if not data: - if stop: - raise StopIteration() raise NoMoreData(bytes(buf)) buf.extend(data) diff --git a/gunicorn/asgi/protocol.py b/gunicorn/asgi/protocol.py index 04320986..356bd507 100644 --- a/gunicorn/asgi/protocol.py +++ b/gunicorn/asgi/protocol.py @@ -186,9 +186,6 @@ class ASGIProtocol(asyncio.Protocol): peername, self.req_count ) - except StopIteration: - # No more data, close connection - break except NoMoreData: # Client disconnected break