go runtime_error ai_generated true

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

ID: go/grpc-client-conn-closed

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2025-07-19First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.x active — — —

Root Cause

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

中文

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

Workarounds

  1. 92% success
    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% success
    var wg sync.WaitGroup
    wg.Add(1)
    go func() { defer wg.Done(); client.Do(ctx, req) }()
    wg.Wait()
    conn.Close()

Dead Ends

Common approaches that don't work:

  1. 90% fail

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

  2. 95% fail

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