Skip to content

Commit 0bb79c2

Browse files
authored
fix(sync): prevent concurrent tail movements (#313)
Moving tail(or pruning) may take a lot of time, while it happens, new network heads can arrive, interfering with the in-progress move. This change simply detects that and skips tail processing if there is one in progress
1 parent 3d71cee commit 0bb79c2

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

sync/syncer.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ type Syncer[H header.Header[H]] struct {
4747
pending ranges[H]
4848
// incomingMu ensures only one incoming network head candidate is processed at the time
4949
incomingMu sync.Mutex
50+
// tailMu prevents concurrent tail movements
51+
tailMu sync.Mutex
5052

5153
// controls lifecycle for syncLoop
5254
ctx context.Context

sync/syncer_tail.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ func (s *Syncer[H]) subjectiveTail(ctx context.Context, head H) (H, error) {
1818
return oldTail, err
1919
}
2020

21+
if !s.tailMu.TryLock() {
22+
// prevents concurrent tail estimation and moving
23+
//
24+
// If a new head arrives, while tail for the previous head is still in progress
25+
// it is valid to skip tail renewal for the new one. It will be resolved with a more recent head
26+
// once in progress tail finishes.
27+
return oldTail, nil
28+
}
29+
defer s.tailMu.Unlock()
30+
2131
newTail, err := s.renewTail(ctx, oldTail, head)
2232
if err != nil {
2333
return oldTail, fmt.Errorf("updating tail: %w", err)

0 commit comments

Comments
 (0)