Skip to content

Commit 65539b6

Browse files
authored
Merge pull request #1126 from GeorgeTsagk/startup-fixes
Startup related fixes
2 parents c5a4b7a + 577cbad commit 65539b6

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

rpcperms/interceptor.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,11 @@ func (r *InterceptorChain) rpcStateUnaryServerInterceptor() grpc.UnaryServerInte
413413
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
414414
handler grpc.UnaryHandler) (interface{}, error) {
415415

416+
if info.Server == nil {
417+
return nil, fmt.Errorf("cannot handle call, server " +
418+
"not ready")
419+
}
420+
416421
if err := r.checkRPCState(info.Server); err != nil {
417422
return nil, err
418423
}
@@ -427,6 +432,10 @@ func (r *InterceptorChain) rpcStateStreamServerInterceptor() grpc.StreamServerIn
427432
return func(srv interface{}, ss grpc.ServerStream,
428433
info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
429434

435+
if srv == nil {
436+
return fmt.Errorf("srv is nil, can't check RPC state")
437+
}
438+
430439
if err := r.checkRPCState(srv); err != nil {
431440
return err
432441
}

server.go

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ func (s *Server) UpdateConfig(cfg *Config) {
9090
//
9191
// NOTE: the rpc server is not registered with any grpc server in this function.
9292
func (s *Server) initialize(interceptorChain *rpcperms.InterceptorChain) error {
93+
var ready bool
94+
95+
// If by the time this function exits we haven't yet given the ready
96+
// signal, we detect it here and signal that the daemon should quit.
97+
defer func() {
98+
if !ready {
99+
close(s.quit)
100+
}
101+
}()
102+
93103
// Show version at startup.
94104
srvrLog.Infof("Version: %s, build=%s, logging=%s, "+
95105
"debuglevel=%s, active_network=%v", Version(), build.Deployment,
@@ -239,6 +249,7 @@ func (s *Server) initialize(interceptorChain *rpcperms.InterceptorChain) error {
239249
shutdownFuncs = nil
240250

241251
close(s.ready)
252+
ready = true
242253

243254
return nil
244255
}
@@ -701,12 +712,23 @@ func (s *Server) waitForReady() error {
701712
// shutdown in case of a startup error). If we shut down after passing
702713
// this part of the code, the called component will handle the quit
703714
// signal.
704-
select {
705-
case <-s.ready:
706-
return nil
707715

716+
// In order to give priority to the quit signal, we wrap the blocking
717+
// select so that we give a chance to the quit signal to be read first.
718+
// This is needed as there is currently no wait to un-set the ready
719+
// signal, so we would have a race between the 2 channels.
720+
select {
708721
case <-s.quit:
709722
return fmt.Errorf("tapd is shutting down")
723+
724+
default:
725+
// We now wait for either signal to be provided.
726+
select {
727+
case <-s.ready:
728+
return nil
729+
case <-s.quit:
730+
return fmt.Errorf("tapd is shutting down")
731+
}
710732
}
711733
}
712734

@@ -820,7 +842,13 @@ func (s *Server) Name() protofsm.EndpointName {
820842
//
821843
// NOTE: This method is part of the protofsm.MsgEndpoint interface.
822844
func (s *Server) CanHandle(msg protofsm.PeerMsg) bool {
823-
<-s.ready
845+
err := s.waitForReady()
846+
if err != nil {
847+
srvrLog.Debugf("Can't handle PeerMsg, server not ready %v",
848+
err)
849+
return false
850+
}
851+
824852
return s.cfg.AuxFundingController.CanHandle(msg)
825853
}
826854

@@ -829,7 +857,13 @@ func (s *Server) CanHandle(msg protofsm.PeerMsg) bool {
829857
//
830858
// NOTE: This method is part of the protofsm.MsgEndpoint interface.
831859
func (s *Server) SendMessage(msg protofsm.PeerMsg) bool {
832-
<-s.ready
860+
err := s.waitForReady()
861+
if err != nil {
862+
srvrLog.Debugf("Failed to send PeerMsg, server not ready %v",
863+
err)
864+
return false
865+
}
866+
833867
return s.cfg.AuxFundingController.SendMessage(msg)
834868
}
835869

0 commit comments

Comments
 (0)