From ed70d2254e39e4b105cf707112a639b96f48da52 Mon Sep 17 00:00:00 2001 From: Debanjum Date: Mon, 7 Apr 2025 18:45:32 +0530 Subject: [PATCH] Suppress spurious RequestAborted ASGI errors on the admin panel Unsure why this error triggers on every request to the Django admin panel these days but all the requests are completing fine and the client is clearly not aborting the request when the RequestAborted exception is raised. Suppress these errors for now via middleware to prevent them from unnecessarily cluttering up the server logs and confusing folks. Related #1152 --- src/khoj/configure.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/khoj/configure.py b/src/khoj/configure.py index 98fb41a0..e6612631 100644 --- a/src/khoj/configure.py +++ b/src/khoj/configure.py @@ -13,7 +13,7 @@ from asgiref.sync import sync_to_async from django.conf import settings from django.db import close_old_connections, connections from django.utils.timezone import make_aware -from fastapi import Response +from fastapi import Request, Response from starlette.authentication import ( AuthCredentials, AuthenticationBackend, @@ -26,7 +26,7 @@ from starlette.middleware.authentication import AuthenticationMiddleware from starlette.middleware.base import BaseHTTPMiddleware from starlette.middleware.httpsredirect import HTTPSRedirectMiddleware from starlette.middleware.sessions import SessionMiddleware -from starlette.requests import HTTPConnection +from starlette.requests import ClientDisconnect, HTTPConnection from starlette.types import ASGIApp, Receive, Scope, Send from khoj.database.adapters import ( @@ -355,8 +355,19 @@ def configure_middleware(app, ssl_enabled: bool = False): super().__init__(app) self.app = app + class SuppressClientDisconnectMiddleware(BaseHTTPMiddleware): + async def dispatch(self, request: Request, call_next): + try: + return await call_next(request) + except ClientDisconnect: + logger.debug("Client disconnected before response completion.") + # Return a minimal response to potentially satisfy the ASGI server + # and prevent further error logging. + return Response(status_code=499) + if ssl_enabled: app.add_middleware(HTTPSRedirectMiddleware) + app.add_middleware(SuppressClientDisconnectMiddleware) app.add_middleware(AsyncCloseConnectionsMiddleware) app.add_middleware(AuthenticationMiddleware, backend=UserAuthenticationBackend()) app.add_middleware(NextJsMiddleware)