Skip to content

Commit d301d53

Browse files
committed
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.
1 parent 9508df6 commit d301d53

2 files changed

Lines changed: 2 additions & 7 deletions

File tree

gunicorn/asgi/message.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ async def parse(cls, cfg, unreader, peer_addr, req_number=1):
139139
async def _parse(self):
140140
"""Parse the request from the unreader."""
141141
buf = bytearray()
142-
await self._read_into(buf, stop=True)
142+
await self._read_into(buf)
143143

144144
# Handle proxy protocol if enabled and this is the first request
145145
mode = self.cfg.proxy_protocol
@@ -174,12 +174,10 @@ async def _parse(self):
174174

175175
self._set_body_reader()
176176

177-
async def _read_into(self, buf, stop=False):
177+
async def _read_into(self, buf):
178178
"""Read data from unreader and append to bytearray buffer."""
179179
data = await self.unreader.read()
180180
if not data:
181-
if stop:
182-
raise StopIteration()
183181
raise NoMoreData(bytes(buf))
184182
buf.extend(data)
185183

gunicorn/asgi/protocol.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,6 @@ async def _handle_connection(self):
186186
peername,
187187
self.req_count
188188
)
189-
except StopIteration:
190-
# No more data, close connection
191-
break
192189
except NoMoreData:
193190
# Client disconnected
194191
break

0 commit comments

Comments
 (0)