Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions pkg/triple/dubbo3_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,28 +282,21 @@ func (t *TripleServer) Start() {

func (t *TripleServer) RefreshService() {
t.opt.Logger.Debugf("TripleServer.Refresh: call refresh services")
grpcServer := newGrpcServerWithCodec(t.opt)
t.rpcServiceMap.Range(func(key, value interface{}) bool {
grpcService, ok := value.(common.TripleGrpcService)
if ok {
desc := grpcService.XXX_ServiceDesc()
desc.ServiceName = key.(string)
grpcServer.RegisterService(desc, value)
t.grpcServer.RegisterService(desc, value)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling for service registration failures.

The RegisterService calls lack error handling, which could lead to silent failures during service refresh.

Add error handling and logging:

 grpcService, ok := value.(common.TripleGrpcService)
 if ok {
     desc := grpcService.XXX_ServiceDesc()
     desc.ServiceName = key.(string)
-    t.grpcServer.RegisterService(desc, value)
+    if err := t.grpcServer.RegisterService(desc, value); err != nil {
+        t.opt.Logger.Errorf("Failed to register gRPC service %s: %v", key.(string), err)
+        return true
+    }
 } else {
     desc := createGrpcDesc(key.(string), value.(common.TripleUnaryService))
-    t.grpcServer.RegisterService(desc, value)
+    if err := t.grpcServer.RegisterService(desc, value); err != nil {
+        t.opt.Logger.Errorf("Failed to register unary service %s: %v", key.(string), err)
+        return true
+    }
 }

Also applies to: 293-293

🤖 Prompt for AI Agents
In pkg/triple/dubbo3_server.go at lines 290 and 293, the calls to
t.grpcServer.RegisterService do not handle errors, risking silent failures
during service refresh. Modify these calls to capture the returned error, check
if it is non-nil, and log an appropriate error message to ensure any
registration failure is detected and reported.

⚠️ Potential issue

Critical: Prevent duplicate service registration to avoid runtime panics.

Calling RegisterService on an already registered service typically causes a panic in gRPC. The code should check if services are already registered before attempting re-registration.

Consider implementing duplicate registration prevention:

 t.rpcServiceMap.Range(func(key, value interface{}) bool {
+   // Skip if already registered to prevent duplicate registration panic
+   if t.registeredKey[key.(string)] {
+       return true
+   }
+   t.registeredKey[key.(string)] = true
+   
    grpcService, ok := value.(common.TripleGrpcService)
    if ok {
        desc := grpcService.XXX_ServiceDesc()
        desc.ServiceName = key.(string)
        t.grpcServer.RegisterService(desc, value)
    } else {
        desc := createGrpcDesc(key.(string), value.(common.TripleUnaryService))
        t.grpcServer.RegisterService(desc, value)
    }

Also applies to: 293-293

🤖 Prompt for AI Agents
In pkg/triple/dubbo3_server.go at lines 290 and 293, the code calls
RegisterService without checking if the service is already registered, which can
cause runtime panics. To fix this, add a check before each RegisterService call
to verify if the service has already been registered on the grpcServer, and only
call RegisterService if it is not registered yet. This prevents duplicate
registration and avoids panics.

} else {
desc := createGrpcDesc(key.(string), value.(common.TripleUnaryService))
grpcServer.RegisterService(desc, value)
t.grpcServer.RegisterService(desc, value)
}
if key == "grpc.reflection.v1alpha.ServerReflection" {
grpcService.(common.TripleGrpcReflectService).SetGRPCServer(grpcServer)
grpcService.(common.TripleGrpcReflectService).SetGRPCServer(t.grpcServer)
}
return true
})
t.grpcServer.Stop()
t.lst.Close()
lst, _ := net.Listen("tcp", t.opt.Location)
go grpcServer.Serve(lst)
t.grpcServer = grpcServer
t.lst = lst
}

func getServerTlsCertificate(opt *config.Option) (credentials.TransportCredentials, error) {
Expand Down