Skip to content

Commit 8ec03e3

Browse files
committed
tapdb: ensure all timers are fully stopped before restarting
This fixes a bug where if the timer had already expired, then the receive on the timer channel would block.
1 parent 9ab2d30 commit 8ec03e3

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

tapdb/universe_stats.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ func (u *UniverseStats) populateSyncStatsCache() {
512512

513513
// If this is a test, then we'll just purge the items.
514514
if u.opts.cacheDuration == 0 {
515+
log.Debugf("nil state cache duration, wiping cache")
515516
u.statsSnapshot.Store(nil)
516517
return
517518
}
@@ -535,7 +536,10 @@ func (u *UniverseStats) populateSyncStatsCache() {
535536

536537
// Reset the timer so we'll refresh again after the cache duration.
537538
if !u.statsRefresh.Stop() {
538-
<-u.statsRefresh.C
539+
select {
540+
case <-u.statsRefresh.C:
541+
default:
542+
}
539543
}
540544

541545
u.statsRefresh.Reset(u.opts.cacheDuration)
@@ -565,6 +569,8 @@ func (u *UniverseStats) AggregateSyncStats(
565569

566570
u.statsCacheLogger.Miss()
567571

572+
log.Debugf("Populating aggregate sync stats")
573+
568574
dbStats, err := u.querySyncStats(ctx)
569575
if err != nil {
570576
return dbStats, err
@@ -574,6 +580,13 @@ func (u *UniverseStats) AggregateSyncStats(
574580
// the stats pointer so we'll refresh it after a period of time.
575581
u.statsSnapshot.Store(&dbStats)
576582

583+
// Reset the timer so we'll refresh again after the cache duration.
584+
if u.statsRefresh != nil && !u.statsRefresh.Stop() {
585+
select {
586+
case <-u.statsRefresh.C:
587+
default:
588+
}
589+
}
577590
u.statsRefresh = time.AfterFunc(
578591
u.opts.cacheDuration, u.populateSyncStatsCache,
579592
)
@@ -844,7 +857,10 @@ func (u *UniverseStats) QuerySyncStats(ctx context.Context,
844857
// If we already have a timer active, then stop it, so we only have a
845858
// single timer going at any given time.
846859
if u.syncStatsRefresh != nil && !u.syncStatsRefresh.Stop() {
847-
<-u.syncStatsRefresh.C
860+
select {
861+
case <-u.syncStatsRefresh.C:
862+
default:
863+
}
848864
}
849865

850866
u.syncStatsRefresh = time.AfterFunc(u.opts.cacheDuration, func() {

0 commit comments

Comments
 (0)