Skip to content

Commit 2545897

Browse files
committed
fix(chain/local_v1): prevent double-close panic in block counter watcher
The count() loop could call close(watcher.channel) on consecutive ticks before the WatchBlocks cleanup goroutine removed the cancelled watcher from the list, causing a "close of closed channel" panic. Use sync.Once to guarantee the channel is closed exactly once.
1 parent cc30229 commit 2545897

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

pkg/chain/local_v1/blockcounter.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ type localBlockCounter struct {
1616
}
1717

1818
type watcher struct {
19-
ctx context.Context
20-
channel chan uint64
19+
ctx context.Context
20+
channel chan uint64
21+
closeOnce sync.Once
2122
}
2223

2324
var defaultBlockTime = 500 * time.Millisecond
@@ -120,7 +121,7 @@ func (lbc *localBlockCounter) count(blockTime ...time.Duration) {
120121

121122
for _, watcher := range watchers {
122123
if watcher.ctx.Err() != nil {
123-
close(watcher.channel)
124+
watcher.closeOnce.Do(func() { close(watcher.channel) })
124125
continue
125126
}
126127

0 commit comments

Comments
 (0)