From 60cd56f5833724fc42f27d50005bfe05e826cc9f Mon Sep 17 00:00:00 2001 From: Emmanuel Evbuomwan Date: Wed, 19 Jun 2024 14:18:01 +0200 Subject: [PATCH] chore, rapu: handle `asyncio.TimeoutError` - we also prevent reraising `asyncio.CanclledError` as it is not necessary within this context, this could cause the app to crash or other unexpected behaviour. --- karapace/rapu.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/karapace/rapu.py b/karapace/rapu.py index daf4fa868..bfccebfe9 100644 --- a/karapace/rapu.py +++ b/karapace/rapu.py @@ -393,14 +393,12 @@ async def _handle_request( ) headers = {"Content-Type": "application/json"} resp = aiohttp.web.Response(body=body, status=status.value, headers=headers) - except (ConnectionError, aiohttp.ClientError) as connection_error: - # TCP level connection errors, e.g. TCP reset, client closes connection. - self.log.debug("Connection error.", exc_info=connection_error) + except (ConnectionError, aiohttp.ClientError, asyncio.CancelledError, asyncio.TimeoutError) as exc: + # TCP level connection errors and timeouts, e.g. TCP reset, client closes connection, task takes too long. + error_msg = "Unexpected connection or timeout error" + self.log.debug(error_msg, exc_info=exc) # No response can be returned and written to client, aiohttp expects some response here. - resp = aiohttp.web.Response(text="Connection error", status=HTTPStatus.SERVICE_UNAVAILABLE.value) - except asyncio.CancelledError: - self.log.debug("Client closed connection") - raise + resp = aiohttp.web.Response(text=error_msg, status=HTTPStatus.SERVICE_UNAVAILABLE.value) except Exception as ex: # pylint: disable=broad-except self.stats.unexpected_exception(ex=ex, where="rapu_wrapped_callback") self.log.exception("Unexpected error handling user request: %s %s", request.method, request.url)