From e45a06cbf1ca1d59cace5289dc4b4d862eb2896c Mon Sep 17 00:00:00 2001 From: Nishant Das Date: Wed, 24 Jun 2020 17:01:48 +0800 Subject: [PATCH] Revert "QSP-42 Remove Double Unsubscribe in Initial Sync" (#6376) * Revert "QSP-42 Remove Double Unsubscribe in Initial Sync (#6368)" This reverts commit 637354f037e53165c5e7aa8c266be0b13de143e1. * comment * add go fmt --- beacon-chain/sync/initial-sync/service.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/beacon-chain/sync/initial-sync/service.go b/beacon-chain/sync/initial-sync/service.go index d25ef8400..d659639f5 100644 --- a/beacon-chain/sync/initial-sync/service.go +++ b/beacon-chain/sync/initial-sync/service.go @@ -80,7 +80,17 @@ func (s *Service) Start() { // Wait for state to be initialized. stateChannel := make(chan *feed.Event, 1) stateSub := s.stateNotifier.StateFeed().Subscribe(stateChannel) - for genesisSet := false; !genesisSet; { + // We have two instances in which we call unsubscribe. The first + // instance below is to account for the fact that we exit + // the for-select loop through a return when we receive a closed + // context or error from our subscription. The only way to correctly + // close the subscription would be through a defer. The second instance we + // call unsubscribe when we have already received the state + // initialized event and are proceeding with the main synchronization + // routine. + defer stateSub.Unsubscribe() + genesisSet := false + for !genesisSet { select { case event := <-stateChannel: if event.Type == statefeed.Initialized { @@ -95,10 +105,10 @@ func (s *Service) Start() { } case <-s.ctx.Done(): log.Debug("Context closed, exiting goroutine") - break + return case err := <-stateSub.Err(): log.WithError(err).Error("Subscription to state notifier failed") - break + return } } stateSub.Unsubscribe()