python network_error ai_generated true

starlette.requests.ClientDisconnect:客户端断开连接

starlette.requests.ClientDisconnect: Client disconnected

ID: python/starlette-jsondecodeerror

其他格式: JSON · Markdown 中文 · English
80%修复率
86%置信度
0证据数
2025-03-15首次发现

版本兼容性

版本状态引入弃用备注
0.30.x active
0.40.x active

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 80% 失败

    Client already disconnected; timeout doesn't help.

  2. 60% 失败

    May cause duplicate operations if not idempotent.