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.
This commit is contained in:
Benoit Chesneau 2026-02-07 09:00:46 +01:00
parent 9508df658d
commit d301d53758
2 changed files with 2 additions and 7 deletions

View File

@ -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)

View File

@ -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