go
runtime_error
ai_generated
true
panic: grpc: Server.RegisterService found duplicate service registration for "pb.UserService"
ID: go/grpc-already-exists-duplicate-register
80%Fix Rate
89%Confidence
0Evidence
2024-08-14First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| google.golang.org/grpc 1.50+ | active | — | — | — |
Root Cause
The same gRPC service was registered twice on one grpc.Server, often from init() functions in multiple packages or from calling RegisterXServer twice during setup.
generic中文
同一个 gRPC 服务被注册到同一 grpc.Server 两次,通常来自多个包中的 init() 函数,或初始化时两次调用 RegisterXServer。
Workarounds
-
95% success
func RegisterAll(s *grpc.Server) { pb.RegisterUserServiceServer(s, &userServer{}) pb.RegisterOrderServiceServer(s, &orderServer{}) } // call exactly once from main -
90% success
// delete func init() { pb.RegisterUserServiceServer(...) } // replace with explicit call in main()
Dead Ends
Common approaches that don't work:
-
85% fail
The server is left in an inconsistent state; subsequent calls may hit the wrong handler or panic again.
-
70% fail
Two servers on the same listener cause bind errors or split traffic unpredictably.