go
config_error
ai_generated
true
rpc 错误:代码 = AlreadyExists 描述 = 服务 pb.OrderService 重复注册
rpc error: code = AlreadyExists desc = duplicate registration of service pb.OrderService
ID: go/grpc-already-exists
80%修复率
86%置信度
0证据数
2024-04-22首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| google.golang.org/grpc v1.46+ | active | — | — | — |
根因分析
在同一个 grpc.Server 上两次调用了 RegisterOrderServiceServer,或两个服务端实例共享了注册表。gRPC 在重复注册服务时会 panic。
English
RegisterOrderServiceServer was called twice on the same grpc.Server, or two server instances share a registry. gRPC panics on duplicate service registration.
解决方案
-
95% 成功率
s := grpc.NewServer() pb.RegisterOrderServiceServer(s, svc) // do not register again elsewhere
-
85% 成功率
var once sync.Once once.Do(func() { pb.RegisterOrderServiceServer(s, svc) })
无效尝试
常见但无效的做法:
-
80% 失败
Recovering hides the bug; the second registration still fails and the server state is inconsistent.
-
75% 失败
Multiple servers on one listener conflict; only one can bind the port, and requests route unpredictably.