From 64a83355c7e50c37b994823b2305beb383050e98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Wed, 3 Sep 2025 19:14:26 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A5=20Remove=20holding=20references=20?= =?UTF-8?q?to=20exceptions=20from=20dependencies=20with=20yield,=20as=20th?= =?UTF-8?q?ose=20caused=20memory=20issues=20before?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi/middleware/asyncexitstack.py | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/fastapi/middleware/asyncexitstack.py b/fastapi/middleware/asyncexitstack.py index 7d34aa9ed..808da413c 100644 --- a/fastapi/middleware/asyncexitstack.py +++ b/fastapi/middleware/asyncexitstack.py @@ -1,5 +1,4 @@ from contextlib import AsyncExitStack -from typing import Optional from starlette.types import ASGIApp, Receive, Scope, Send @@ -10,16 +9,6 @@ class AsyncExitStackMiddleware: self.context_name = context_name async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: - dependency_exception: Optional[Exception] = None async with AsyncExitStack() as stack: scope[self.context_name] = stack - try: - await self.app(scope, receive, send) - except Exception as e: - dependency_exception = e - raise e - if dependency_exception: - # This exception was possibly handled by the dependency but it should - # still bubble up so that the ServerErrorMiddleware can return a 500 - # or the ExceptionMiddleware can catch and handle any other exceptions - raise dependency_exception + await self.app(scope, receive, send)