From a113940f0b05ecac15b15dc68f2410d7b8a6dc4d Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Thu, 13 Apr 2023 12:03:18 +0400 Subject: [PATCH] services/rpcsrv: Fix potential shutdown deadlock of RPC server Previously RPC server could never be shut down completely due to some start precondition failure (in particular, inability to serve HTTP on any configured endpoint). The problem was caused by next facts: * start method ran subscription routine after HTTP init succeeded only * stop method blocked waiting for the subscription routine to return Run `handleSubEvents` routine on fresh `Start` unconditionally. With this change, `Shutdown` method won't produce deadlock since `handleSubEvents` closes wait channel. Refs #2896. Signed-off-by: Leonard Lyubich --- pkg/services/rpcsrv/server.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/services/rpcsrv/server.go b/pkg/services/rpcsrv/server.go index 88fb38942..78562bc42 100644 --- a/pkg/services/rpcsrv/server.go +++ b/pkg/services/rpcsrv/server.go @@ -344,6 +344,9 @@ func (s *Server) Start() { s.log.Info("RPC server already started") return } + + go s.handleSubEvents() + for _, srv := range s.http { srv.Handler = http.HandlerFunc(s.handleHTTPRequest) s.log.Info("starting rpc-server", zap.String("endpoint", srv.Addr)) @@ -363,7 +366,6 @@ func (s *Server) Start() { }(srv) } - go s.handleSubEvents() if cfg := s.config.TLSConfig; cfg.Enabled { for _, srv := range s.https { srv.Handler = http.HandlerFunc(s.handleHTTPRequest)