Skip to content

Prevents reraising of exception from BaseHTTPMiddleware #2911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions starlette/middleware/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
wrapped_receive = request.wrapped_receive
response_sent = anyio.Event()
app_exc: Exception | None = None
exception_already_raised = False

async def call_next(request: Request) -> Response:
async def receive_or_disconnect() -> Message:
Expand Down Expand Up @@ -150,6 +151,8 @@ async def coro() -> None:
message = await recv_stream.receive()
except anyio.EndOfStream:
if app_exc is not None:
nonlocal exception_already_raised
exception_already_raised = True
raise app_exc
raise RuntimeError("No response returned.")

Expand All @@ -176,8 +179,7 @@ async def body_stream() -> typing.AsyncGenerator[bytes, None]:
await response(scope, wrapped_receive, send)
response_sent.set()
recv_stream.close()

if app_exc is not None:
if app_exc is not None and not exception_already_raised:
raise app_exc

async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -> Response:
Expand Down
21 changes: 21 additions & 0 deletions tests/middleware/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,27 @@ async def passthrough(request: Request, call_next: RequestResponseEndpoint) -> R
client.get("/")


def test_exception_can_be_caught(test_client_factory: TestClientFactory) -> None:
async def error_endpoint(_: Request) -> None:
raise ValueError("TEST")

async def catches_error(request: Request, call_next: RequestResponseEndpoint) -> Response:
try:
return await call_next(request)
except ValueError as exc:
return PlainTextResponse(content=str(exc), status_code=400)

app = Starlette(
middleware=[Middleware(BaseHTTPMiddleware, dispatch=catches_error)],
routes=[Route("/", error_endpoint)],
)

client = test_client_factory(app)
response = client.get("/")
assert response.status_code == 400
assert response.text == "TEST"


@pytest.mark.anyio
async def test_do_not_block_on_background_tasks() -> None:
response_complete = anyio.Event()
Expand Down