diff --git a/CHANGES/10055.bugfix.rst b/CHANGES/10055.bugfix.rst new file mode 120000 index 00000000000..b1f45d8b887 --- /dev/null +++ b/CHANGES/10055.bugfix.rst @@ -0,0 +1 @@ +10076.bugfix.rst \ No newline at end of file diff --git a/CHANGES/10076.bugfix.rst b/CHANGES/10076.bugfix.rst new file mode 100644 index 00000000000..c577366bbe8 --- /dev/null +++ b/CHANGES/10076.bugfix.rst @@ -0,0 +1 @@ +Fixed invalid method logging unexpected being logged at exception level on subsequent connections -- by :user:`bdraco`. diff --git a/aiohttp/web_protocol.py b/aiohttp/web_protocol.py index d83c289a40b..0bffc849817 100644 --- a/aiohttp/web_protocol.py +++ b/aiohttp/web_protocol.py @@ -212,6 +212,7 @@ def __init__( ): super().__init__(loop) + # _request_count is the number of requests processed with the same connection. self._request_count = 0 self._keepalive = False self._current_request: Optional[_Request] = None @@ -713,11 +714,7 @@ def handle_error( Returns HTTP response with specific status code. Logs additional information. It always closes current connection. """ - if ( - self._manager - and self._manager.requests_count == 1 - and isinstance(exc, BadHttpMethod) - ): + if self._request_count == 1 and isinstance(exc, BadHttpMethod): # BadHttpMethod is common when a client sends non-HTTP # or encrypted traffic to an HTTP port. This is expected # to happen when connected to the public internet so we log diff --git a/aiohttp/web_server.py b/aiohttp/web_server.py index 15541d80dc1..e2f6c350f9a 100644 --- a/aiohttp/web_server.py +++ b/aiohttp/web_server.py @@ -76,6 +76,8 @@ def __init__( self._loop = asyncio.get_running_loop() self._connections: Dict[RequestHandler[_Request], asyncio.Transport] = {} self._kwargs = kwargs + # requests_count is the number of requests being processed by the server + # for the lifetime of the server. self.requests_count = 0 self.request_handler = handler self.request_factory = request_factory or self._make_request # type: ignore[assignment] diff --git a/tests/test_web_server.py b/tests/test_web_server.py index 07d6c420f32..12e72b865da 100644 --- a/tests/test_web_server.py +++ b/tests/test_web_server.py @@ -97,6 +97,20 @@ async def handler(request: web.BaseRequest) -> NoReturn: # be probing for TLS/SSL support which is # expected to fail logger.debug.assert_called_with("Error handling request", exc_info=exc) + logger.debug.reset_mock() + + # Now make another connection to the server + # to make sure that the exception is logged + # at debug on a second fresh connection + cli2 = await aiohttp_client(server) + resp = await cli2.get("/path/to") + assert resp.status == 500 + assert resp.headers["Content-Type"].startswith("text/plain") + # BadHttpMethod should be logged as debug + # on the first request since the client may + # be probing for TLS/SSL support which is + # expected to fail + logger.debug.assert_called_with("Error handling request", exc_info=exc) async def test_raw_server_logs_invalid_method_without_loop_debug(