fix(core): handle anyio.BrokenResourceError for client disconnects (#9358)

Catch BrokenResourceError alongside ClosedResourceError in streaming
response, logging middleware, and app exception handlers so client
disconnects are logged at info level instead of surfacing as 500s.

Datadog: https://us5.datadoghq.com/error-tracking/issue/4f57af0c-d558-11f0-a65d-da7ad0900000

🤖 Generated with [Letta Code](https://letta.com)

Co-authored-by: Letta <noreply@letta.com>
This commit is contained in:
Kian Jones
2026-02-11 11:07:09 -08:00
committed by Caren Thomas
parent 0d42afa151
commit 3634464251
3 changed files with 19 additions and 7 deletions

View File

@@ -11,6 +11,7 @@ from functools import partial
from pathlib import Path
from typing import Optional
import anyio
import uvicorn
# Enable Python fault handler to get stack traces on segfaults
@@ -415,6 +416,12 @@ def create_application() -> "FastAPI":
# === Exception Handlers ===
# TODO (cliandy): move to separate file
@app.exception_handler(anyio.BrokenResourceError)
@app.exception_handler(anyio.ClosedResourceError)
async def client_disconnect_handler(request: Request, exc: Exception):
logger.info(f"Client disconnected: {request.method} {request.url.path}")
return JSONResponse(status_code=499, content={"detail": "Client disconnected"})
@app.exception_handler(Exception)
async def generic_error_handler(request: Request, exc: Exception):
# Log with structured context