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

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