go runtime_error ai_generated true

rpc error: code = Unavailable desc = grpc: 客户端连接正在关闭

rpc error: code = Unavailable desc = grpc: the client connection is closing

ID: go/grpc-client-conn-closed

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

版本兼容性

版本状态引入弃用备注
1.x active — — —

根因分析

*grpc.ClientConn 在 RPC 进行中被关闭(conn.Close()),或在关闭期间对共享 conn 调用了 Close,而其他 goroutine 仍在使用它。

English

The *grpc.ClientConn was closed (conn.Close()) while an RPC was in flight, or Close was called on a shared conn during shutdown while other goroutines still use it.

generic

解决方案

  1. 92% 成功率
    var conn *grpc.ClientConn
    func Init() error {
      var err error
      conn, err = grpc.NewClient(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
      return err
    }
    // Close only at shutdown via signal handler
  2. 88% 成功率
    var wg sync.WaitGroup
    wg.Add(1)
    go func() { defer wg.Done(); client.Do(ctx, req) }()
    wg.Wait()
    conn.Close()

无效尝试

常见但无效的做法:

  1. 90% 失败

    Closes the shared connection after the first call; subsequent calls fail.

  2. 95% 失败

    A closed ClientConn cannot be reused; all calls fail immediately.