2 grpc protocol_error ai_generated partial

内部错误:gRPC:客户端流式写入失败:流已关闭,错误代码 2

INTERNAL: grpc: client-side streaming write failed: stream closed with error code 2

ID: grpc/client-side-streaming-write-failure

其他格式: JSON · Markdown 中文 · English
82%修复率
86%置信度
1证据数
2024-08-05首次发现

版本兼容性

版本状态引入弃用备注
gRPC v1.55.0 active
gRPC v1.61.0 active
gRPC v1.65.0 active

根因分析

gRPC 客户端流式 RPC 遇到写入失败,因为服务器因内部错误或超时而提前关闭流,导致客户端无法发送更多消息。

English

The gRPC client-side streaming RPC encountered a write failure because the server prematurely closed the stream due to an internal error or timeout, leaving the client unable to send further messages.

generic

官方文档

https://grpc.io/docs/guides/error/

解决方案

  1. 为流式 RPC 实现带退避的重试循环:`for attempt in range(3): try: call.write(msg); break; except Exception: time.sleep(0.5*2**attempt)`
  2. 检查服务器的流超时时间,如果太低则增加:在服务器配置中设置 `grpc.max_connection_age` 或调整客户端超时以匹配。
  3. 添加客户端逻辑以检测流关闭并重新创建流:`if call.done(): call = stub.StreamingMethod()`

无效尝试

常见但无效的做法:

  1. 60% 失败

    Retrying the entire streaming RPC from scratch may work but is inefficient and does not prevent the server-side issue.

  2. 50% 失败

    Reducing the client's message size does not help if the server closes the stream due to a logic error or resource limit.

  3. 90% 失败

    Ignoring the error and continuing to write to the closed stream will cause a panic or crash.