go system_error ai_generated true

rpc 错误:code = Internal,desc = grpc:服务器已停止

rpc error: code = Internal desc = grpc: the server has been stopped

ID: go/grpc-server-stream-closed

其他格式: JSON · Markdown 中文 · English
80%修复率
87%置信度
0证据数
2024-03-28首次发现

版本兼容性

版本状态引入弃用备注
google.golang.org/grpc 1.40+ active

根因分析

在调用 grpc.Server.Stop() 或 GracefulStop() 后,处理器仍尝试在流上发送。常见于关闭期间未排空进行中的处理器。

English

A handler attempted to send on a stream after grpc.Server.Stop() or GracefulStop() was called. Common during shutdown when in-flight handlers are not drained.

generic

解决方案

  1. 95% 成功率
    Use GracefulStop with a timeout fallback:
    done := make(chan struct{})
    go func() {
        grpcServer.GracefulStop()
        close(done)
    }()
    select {
    case <-done:
    case <-time.After(30 * time.Second):
        grpcServer.Stop()
    }
  2. 90% 成功率
    Check ctx.Err() before each Send in streaming handlers:
    if err := stream.Context().Err(); err != nil {
        return status.FromContextError(err).Err()
    }

无效尝试

常见但无效的做法:

  1. 80% 失败

    Restarting does not drain in-flight handlers; the error recurs on the next shutdown.

  2. 75% 失败

    Clients receive truncated responses and may retry with stale state.