Skip to content

Commit 8f6d3f1

Browse files
committed
guard tickers slice
1 parent 3093efd commit 8f6d3f1

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

coderd/metricscache/metricscache.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package metricscache
33
import (
44
"context"
55
"database/sql"
6+
"slices"
7+
"sync"
68
"sync/atomic"
79
"time"
810

@@ -33,8 +35,9 @@ type Cache struct {
3335
templateAverageBuildTime atomic.Pointer[map[uuid.UUID]database.GetTemplateAverageBuildTimeRow]
3436
deploymentStatsResponse atomic.Pointer[codersdk.DeploymentStats]
3537

36-
cancel func()
37-
tickers []quartz.Waiter
38+
cancel func()
39+
tickersMu sync.Mutex
40+
tickers []quartz.Waiter
3841

3942
// usage is a experiment flag to enable new workspace usage tracking behavior and will be
4043
// removed when the experiment is complete.
@@ -193,12 +196,18 @@ func (c *Cache) run(ctx context.Context, name string, interval time.Duration, re
193196
_ = tickerFunc()
194197

195198
tkr := c.clock.TickerFunc(ctx, interval, tickerFunc, "metricscache", name)
199+
c.tickersMu.Lock()
196200
c.tickers = append(c.tickers, tkr)
201+
c.tickersMu.Unlock()
197202
}
198203

199204
func (c *Cache) Close() error {
200205
c.cancel()
201-
for _, tkr := range c.tickers {
206+
c.tickersMu.Lock()
207+
tickers := slices.Clone(c.tickers)
208+
c.tickersMu.Unlock()
209+
210+
for _, tkr := range tickers {
202211
_ = tkr.Wait()
203212
}
204213
return nil

0 commit comments

Comments
 (0)