Skip to content

Commit

Permalink
blockchain: add waiting persist goroutine before the Shutdown
Browse files Browse the repository at this point in the history
The `persist` goroutine could write logs after the blockchain service
shutdown that leads to tests failure.

Close #3217

Signed-off-by: Ekaterina Pavlova <ekt@morphbits.io>
  • Loading branch information
AliceInHunterland committed Mar 4, 2024
1 parent 8d67f17 commit 8a69486
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ type Blockchain struct {
// Stop synchronization mechanisms.
stopCh chan struct{}
runToExitCh chan struct{}
wgPersist sync.WaitGroup
// isRunning denotes whether blockchain routines are currently running.
isRunning atomic.Value

Expand Down Expand Up @@ -1441,6 +1442,7 @@ func (bc *Blockchain) notificationDispatcher() {
// Close stops Blockchain's internal loop, syncs changes to persistent storage
// and closes it. The Blockchain is no longer functional after the call to Close.
func (bc *Blockchain) Close() {
bc.wgPersist.Wait()
// If there is a block addition in progress, wait for it to finish and
// don't allow new ones.
bc.addLock.Lock()
Expand Down Expand Up @@ -2113,6 +2115,8 @@ func (bc *Blockchain) persist(isSync bool) (time.Duration, error) {
persisted int
err error
)
bc.wgPersist.Add(1)
defer bc.wgPersist.Done()

if isSync {
persisted, err = bc.dao.PersistSync()
Expand Down

0 comments on commit 8a69486

Please sign in to comment.