Skip to content

Commit

Permalink
Clean up MerkleDVB Sync Close lock (ava-labs#1469)
Browse files Browse the repository at this point in the history
Co-authored-by: Dan Laine <daniel.laine@avalabs.org>
  • Loading branch information
dboehm-avalabs and danlaine committed May 16, 2023
1 parent 9f6c371 commit 2e44364
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions x/sync/syncmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,9 @@ func (m *StateSyncManager) StartSyncing(ctx context.Context) error {
// the work.
func (m *StateSyncManager) sync(ctx context.Context) {
defer func() {
// Note we release [m.workLock] before calling Close()
// because Close() will acquire [m.workLock].
// Invariant: [m.workLock] is held when this goroutine begins.
m.close()
m.workLock.Unlock()
m.Close()
}()

// Keep doing work until we're closed, done or [ctx] is canceled.
Expand Down Expand Up @@ -203,12 +201,17 @@ func (m *StateSyncManager) sync(ctx context.Context) {
}
}

// Called when there is a fatal error or sync is complete.
// Close will stop the syncing process
func (m *StateSyncManager) Close() {
m.closeOnce.Do(func() {
m.workLock.Lock()
defer m.workLock.Unlock()
m.workLock.Lock()
defer m.workLock.Unlock()
m.close()
}

// close is called when there is a fatal error or sync is complete.
// [workLock] must be held
func (m *StateSyncManager) close() {
m.closeOnce.Do(func() {
// Don't process any more work items.
// Drop currently processing work items.
if m.cancelCtx != nil {
Expand Down

0 comments on commit 2e44364

Please sign in to comment.