Skip to content

Commit 9f933bf

Browse files
authoredFeb 20, 2025··
[PR #10434/ed84464 backport][3.12] Fix inappropriate "break in finally" (#10475)
1 parent 8b39002 commit 9f933bf

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed
 

‎CHANGES/10434.bugfix.rst

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Avoid break statement inside the finally block in :py:class:`~aiohttp.web.RequestHandler`
2+
-- by :user:`Cycloctane`.

‎aiohttp/web_protocol.py

+15-13
Original file line numberDiff line numberDiff line change
@@ -608,26 +608,28 @@ async def start(self) -> None:
608608

609609
except asyncio.CancelledError:
610610
self.log_debug("Ignored premature client disconnection")
611+
self.force_close()
611612
raise
612613
except Exception as exc:
613614
self.log_exception("Unhandled exception", exc_info=exc)
614615
self.force_close()
616+
except BaseException:
617+
self.force_close()
618+
raise
615619
finally:
616620
if self.transport is None and resp is not None:
617621
self.log_debug("Ignored premature client disconnection.")
618-
elif not self._force_close:
619-
if self._keepalive and not self._close:
620-
# start keep-alive timer
621-
if keepalive_timeout is not None:
622-
now = loop.time()
623-
close_time = now + keepalive_timeout
624-
self._next_keepalive_close_time = close_time
625-
if self._keepalive_handle is None:
626-
self._keepalive_handle = loop.call_at(
627-
close_time, self._process_keepalive
628-
)
629-
else:
630-
break
622+
623+
if self._keepalive and not self._close and not self._force_close:
624+
# start keep-alive timer
625+
close_time = loop.time() + keepalive_timeout
626+
self._next_keepalive_close_time = close_time
627+
if self._keepalive_handle is None:
628+
self._keepalive_handle = loop.call_at(
629+
close_time, self._process_keepalive
630+
)
631+
else:
632+
break
631633

632634
# remove handler, close transport if no handlers left
633635
if not self._force_close:

‎tests/test_web_server.py

+18
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,24 @@ async def handler(request):
233233
logger.debug.assert_called_with("Ignored premature client disconnection")
234234

235235

236+
async def test_raw_server_does_not_swallow_base_exceptions(
237+
aiohttp_raw_server: AiohttpRawServer, aiohttp_client: AiohttpClient
238+
) -> None:
239+
class UnexpectedException(BaseException):
240+
"""Dummy base exception."""
241+
242+
async def handler(request: web.BaseRequest) -> NoReturn:
243+
raise UnexpectedException()
244+
245+
loop = asyncio.get_event_loop()
246+
loop.set_debug(True)
247+
server = await aiohttp_raw_server(handler)
248+
cli = await aiohttp_client(server)
249+
250+
with pytest.raises(client.ServerDisconnectedError):
251+
await cli.get("/path/to", timeout=client.ClientTimeout(10))
252+
253+
236254
async def test_raw_server_cancelled_in_write_eof(aiohttp_raw_server, aiohttp_client):
237255
async def handler(request):
238256
resp = web.Response(text=str(request.rel_url))

0 commit comments

Comments
 (0)
Please sign in to comment.