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 <leonard@morphbits.io>
This commit is contained in:
Leonard Lyubich 2023-04-13 12:03:18 +04:00
parent 649877d8f3
commit a113940f0b

View file

@ -344,6 +344,9 @@ func (s *Server) Start() {
s.log.Info("RPC server already started") s.log.Info("RPC server already started")
return return
} }
go s.handleSubEvents()
for _, srv := range s.http { for _, srv := range s.http {
srv.Handler = http.HandlerFunc(s.handleHTTPRequest) srv.Handler = http.HandlerFunc(s.handleHTTPRequest)
s.log.Info("starting rpc-server", zap.String("endpoint", srv.Addr)) s.log.Info("starting rpc-server", zap.String("endpoint", srv.Addr))
@ -363,7 +366,6 @@ func (s *Server) Start() {
}(srv) }(srv)
} }
go s.handleSubEvents()
if cfg := s.config.TLSConfig; cfg.Enabled { if cfg := s.config.TLSConfig; cfg.Enabled {
for _, srv := range s.https { for _, srv := range s.https {
srv.Handler = http.HandlerFunc(s.handleHTTPRequest) srv.Handler = http.HandlerFunc(s.handleHTTPRequest)