Skip to content

Commit

Permalink
fix(metrics): fix race condition in metrics.Init()
Browse files Browse the repository at this point in the history
IterationMetricsEnabled can be concurrently set

```
==================
WARNING: DATA RACE
Write at 0x00c000124098 by goroutine 12:
  github.com/form3tech-oss/f1/v2/internal/metrics.Init()
      /Users/nikolayvladimirov/go/src/github.com/form3tech-oss/f1/internal/metrics/metrics.go:82 +0x7c
  github.com/form3tech-oss/f1/v2/internal/metrics_test.TestMetrics_Init_IsSafe.func1()
      /Users/nikolayvladimirov/go/src/github.com/form3tech-oss/f1/internal/metrics/metrics_test.go:15 +0x20

Previous write at 0x00c000124098 by goroutine 14:
  github.com/form3tech-oss/f1/v2/internal/metrics.Init()
      /Users/nikolayvladimirov/go/src/github.com/form3tech-oss/f1/internal/metrics/metrics.go:82 +0x7c
  github.com/form3tech-oss/f1/v2/internal/metrics_test.TestMetrics_Init_IsSafe.func1()
      /Users/nikolayvladimirov/go/src/github.com/form3tech-oss/f1/internal/metrics/metrics_test.go:15 +0x20

Goroutine 12 (running) created at:
  github.com/form3tech-oss/f1/v2/internal/metrics_test.TestMetrics_Init_IsSafe()
      /Users/nikolayvladimirov/go/src/github.com/form3tech-oss/f1/internal/metrics/metrics_test.go:14 +0xa4
  testing.tRunner()
      /opt/homebrew/Cellar/go/1.23.1/libexec/src/testing/testing.go:1690 +0x184
  testing.(*T).Run.gowrap1()
      /opt/homebrew/Cellar/go/1.23.1/libexec/src/testing/testing.go:1743 +0x40

Goroutine 14 (finished) created at:
  github.com/form3tech-oss/f1/v2/internal/metrics_test.TestMetrics_Init_IsSafe()
      /Users/nikolayvladimirov/go/src/github.com/form3tech-oss/f1/internal/metrics/metrics_test.go:14 +0xa4
  testing.tRunner()
      /opt/homebrew/Cellar/go/1.23.1/libexec/src/testing/testing.go:1690 +0x184
  testing.(*T).Run.gowrap1()
      /opt/homebrew/Cellar/go/1.23.1/libexec/src/testing/testing.go:1743 +0x40
```
  • Loading branch information
nvloff-f3 committed Sep 11, 2024
1 parent f016ea2 commit d2db6af
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
1 change: 0 additions & 1 deletion internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ func Init(iterationMetricsEnabled bool) {
}
m = NewInstance(defaultRegistry, iterationMetricsEnabled)
})
m.IterationMetricsEnabled = iterationMetricsEnabled
}

func Instance() *Metrics {
Expand Down
24 changes: 24 additions & 0 deletions internal/metrics/metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package metrics_test

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/form3tech-oss/f1/v2/internal/metrics"
)

func TestMetrics_Init_IsSafe(t *testing.T) {
t.Parallel()

metrics.Init(true)

// race detector assertion
for range 10 {
go func() {
metrics.Init(false)
}()
}

assert.True(t, metrics.Instance().IterationMetricsEnabled)
}

0 comments on commit d2db6af

Please sign in to comment.