python network_error ai_generated true

starlette.requests.ClientDisconnect: Client disconnected

ID: python/starlette-jsondecodeerror

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2025-03-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
0.30.x active — — —
0.40.x active — — —

Root Cause

The client closed the connection before the server could read the full request body.

generic

中文

客户端在服务器读取完整请求体之前关闭了连接。

Workarounds

  1. 90% success
    try:
        await request.json()
    except ClientDisconnect:
        return JSONResponse(status_code=499, content={'detail': 'Client disconnected'})
  2. 85% success
    async for chunk in request.stream():
        if await request.is_disconnected():
            break

Dead Ends

Common approaches that don't work:

  1. 80% fail

    Client already disconnected; timeout doesn't help.

  2. 60% fail

    May cause duplicate operations if not idempotent.