# CANCELLED: grpc: 客户端流式调用被客户端取消

- **ID:** `grpc/client-side-streaming-cancel`
- **领域:** grpc
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

客户端显式取消了一个流式RPC（例如通过context取消或stream.CloseSend），在服务器完成处理之前，通常由于超时或提前退出。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| gRPC Python 1.60+ | active | — | — |
| gRPC Java 1.60+ | active | — | — |
| gRPC Node 1.10+ | active | — | — |

## 解决方案

1. ```
   Check server-side handling of client cancel: ensure server reads from stream until EOF and handles context cancellation gracefully. In Python: for request in stub.StreamingCall(...): try: process(request) except grpc.RpcError as e: if e.code() == grpc.StatusCode.CANCELLED: break
   ```
2. ```
   Use a graceful shutdown pattern: before canceling, call stream.CloseSend() and wait for server response. In Go: if err := stream.CloseSend(); err != nil { log.Fatal(err) } for { _, err := stream.Recv(); if err == io.EOF { break } }
   ```

## 无效尝试

- **Adding longer timeouts to all RPCs** — The cancellation is intentional from client logic; longer timeouts only mask the symptom and may cause resource leaks. (50% 失败率)
- **Ignoring the error and retrying immediately** — Retrying without fixing the cancel logic may loop indefinitely or waste resources. (60% 失败率)
