{
  "id": "grpc/client-side-streaming-write-failure",
  "signature": "INTERNAL: grpc: client-side streaming write failed: stream closed with error code 2",
  "signature_zh": "内部错误：gRPC：客户端流式写入失败：流已关闭，错误代码 2",
  "regex": "INTERNAL: grpc: client-side streaming write failed: stream closed with error code \\d+",
  "domain": "grpc",
  "category": "protocol_error",
  "subcategory": null,
  "root_cause": "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.",
  "root_cause_type": "generic",
  "root_cause_zh": "gRPC 客户端流式 RPC 遇到写入失败，因为服务器因内部错误或超时而提前关闭流，导致客户端无法发送更多消息。",
  "versions": [
    {
      "version": "gRPC v1.55.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "gRPC v1.61.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "gRPC v1.65.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Retrying the entire streaming RPC from scratch may work but is inefficient and does not prevent the server-side issue.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Reducing the client's message size does not help if the server closes the stream due to a logic error or resource limit.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Ignoring the error and continuing to write to the closed stream will cause a panic or crash.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Implement a retry loop with backoff for the streaming RPC: `for attempt in range(3): try: call.write(msg); break; except Exception: time.sleep(0.5*2**attempt)`",
      "success_rate": 0.75,
      "how": "Implement a retry loop with backoff for the streaming RPC: `for attempt in range(3): try: call.write(msg); break; except Exception: time.sleep(0.5*2**attempt)`",
      "condition": "",
      "sources": []
    },
    {
      "action": "Check the server's stream deadline and increase it if too low: in server config, set `grpc.max_connection_age` or adjust client deadline to match.",
      "success_rate": 0.8,
      "how": "Check the server's stream deadline and increase it if too low: in server config, set `grpc.max_connection_age` or adjust client deadline to match.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Add client-side logic to detect stream closure and recreate the stream: `if call.done(): call = stub.StreamingMethod()`",
      "success_rate": 0.7,
      "how": "Add client-side logic to detect stream closure and recreate the stream: `if call.done(): call = stub.StreamingMethod()`",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "为流式 RPC 实现带退避的重试循环：`for attempt in range(3): try: call.write(msg); break; except Exception: time.sleep(0.5*2**attempt)`",
    "检查服务器的流超时时间，如果太低则增加：在服务器配置中设置 `grpc.max_connection_age` 或调整客户端超时以匹配。",
    "添加客户端逻辑以检测流关闭并重新创建流：`if call.done(): call = stub.StreamingMethod()`"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://grpc.io/docs/guides/error/",
  "official_doc_section": null,
  "error_code": "2",
  "verification_tier": "ai_generated",
  "confidence": 0.86,
  "fix_success_rate": 0.82,
  "resolvable": "partial",
  "first_seen": "2024-08-05",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}