Merge pull request #1411 from nspcc-dev/fix-panic-in-cli-tests

consensus: wait goroutine to finish on Shutdown
This commit is contained in:
Roman Khimov 2020-09-21 09:45:24 +03:00 committed by GitHub
commit a9f5fe424d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -74,8 +74,9 @@ type service struct {
network netmode.Magic
// started is a flag set with Start method that runs an event handling
// goroutine.
started *atomic.Bool
quit chan struct{}
started *atomic.Bool
quit chan struct{}
finished chan struct{}
}
// Config is a configuration for consensus services.
@ -119,6 +120,7 @@ func NewService(cfg Config) (Service, error) {
network: cfg.Chain.GetConfig().Magic,
started: atomic.NewBool(false),
quit: make(chan struct{}),
finished: make(chan struct{}),
}
if cfg.Wallet == nil {
@ -197,14 +199,16 @@ func (s *service) Start() {
// Shutdown implements Service interface.
func (s *service) Shutdown() {
close(s.quit)
<-s.finished
}
func (s *service) eventLoop() {
events:
for {
select {
case <-s.quit:
s.dbft.Timer.Stop()
return
break events
case <-s.dbft.Timer.C():
hv := s.dbft.Timer.HV()
s.log.Debug("timer fired",
@ -250,6 +254,7 @@ func (s *service) eventLoop() {
}
}
close(s.finished)
}
func (s *service) handleChainBlock(b *coreb.Block) {