grpc runtime_error ai_generated true

已取消: grpc: 客户端侧流被客户端中止

CANCELLED: grpc: client-side stream aborted by client

ID: grpc/grpc-client-side-stream-abort

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

版本兼容性

版本状态引入弃用备注
grpc-go 1.62.0 active
grpc-python 1.60.0 active
grpc-node 1.10.0 active

根因分析

客户端在服务器完成处理之前显式关闭或取消了流,通常是由于过早的上下文取消或 stream.CloseSend() 的误用。

English

Client explicitly closed or cancelled the stream before the server finished processing, often due to premature context cancellation or stream.CloseSend() misuse.

generic

官方文档

https://grpc.io/docs/languages/go/basics/#client-side-streaming-rpc

解决方案

  1. Ensure the client does not call stream.CloseSend() before receiving all server responses. In gRPC-Go, use a proper pattern:
      stream, err := client.StreamingMethod(ctx)
      // Send all messages
      stream.CloseSend()
      // Then receive all responses
      for {
          resp, err := stream.Recv()
          if err == io.EOF { break }
      }
  2. Add a defer cancel() after creating the context to ensure proper cleanup without premature cancellation:
      ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
      defer cancel()
      stream, _ := client.StreamingMethod(ctx)
      // Use stream
      // cancel() will be called when function returns, not before stream completes

无效尝试

常见但无效的做法:

  1. 80% 失败

    The error is caused by explicit cancellation or stream abort, not timeout; extending timeout doesn't prevent premature CloseSend().

  2. 95% 失败

    The stream is already closed; server cannot send responses, leading to resource leaks and inconsistent state.