Skip to content

Data race when initializing PeriodicReader with short interval #8769

Closed

Description

@CTrando

Description

NewPeriodicReader starts the run() goroutine before it finishes initializing the PeriodicReader struct. The r.inst field is assigned after the goroutine is already launched. r.inst is a plain pointer with no mutex/atomic protection, and collect() reads it unsynchronized on every collection.

When the reader's first tick can fire before the constructor finishes (e.g. a short configured export interval), the race detector flags a genuine data race between the constructor's write and the background goroutine's read.

It's definitely a rare edge case, but I noticed it in my testing with different collection intervals. In practice this probably doesn't occur much, but I figured I would report it anyways.

Environment

  • OS: darwin / linux
  • Architecture: arm64/amd64
  • Go Version: go1.26.5
  • opentelemetry-go version: v1.44.0 and v1.45.0

Steps To Reproduce

func TestNewPeriodicReaderInstRace(t *testing.T) {
      for i := 0; i < 50; i++ {
              r := NewPeriodicReader(new(fnExporter), WithInterval(time.Nanosecond))
              r.register(testSDKProducer{})
              _ = r.Shutdown(context.Background())
      }
}

Run the test via:

go test -race -run TestNewPeriodicReaderInstRace -v .

This should give a data race like below.

=== RUN   TestNewPeriodicReaderInstRace
==================
WARNING: DATA RACE
Write at 0x00c00031c5f0 by goroutine 8:
  go.opentelemetry.io/otel/sdk/metric.NewPeriodicReader()
      sdk/metric/periodic_reader.go:141 +0x4a8
  go.opentelemetry.io/otel/sdk/metric.TestNewPeriodicReaderInstRace()
      sdk/metric/periodic_reader_race_repro_test.go:23 +0xa8
  testing.tRunner()
      testing.go:2036 +0x164

Previous read at 0x00c00031c5f0 by goroutine 34:
  go.opentelemetry.io/otel/sdk/metric.(*PeriodicReader).collect()
      sdk/metric/periodic_reader.go:289 +0x58
  go.opentelemetry.io/otel/sdk/metric.(*PeriodicReader).Collect()
      sdk/metric/periodic_reader.go:283 +0xbc
  go.opentelemetry.io/otel/sdk/metric.(*PeriodicReader).collectAndExport()
      sdk/metric/periodic_reader.go:252 +0x13c
  go.opentelemetry.io/otel/sdk/metric.(*PeriodicReader).run()
      sdk/metric/periodic_reader.go:201 +0x1a8
  go.opentelemetry.io/otel/sdk/metric.NewPeriodicReader.func2()
      sdk/metric/periodic_reader.go:137 +0x94

Goroutine 8 (running) created at:
  testing.(*T).Run()
      testing.go:2101 +0x7bc
  testing.runTests.func1()
      testing.go:2585 +0x74
  testing.tRunner()
      testing.go:2036 +0x164

Goroutine 34 (running) created at:
  go.opentelemetry.io/otel/sdk/metric.NewPeriodicReader()
      sdk/metric/periodic_reader.go:135 +0x418
  go.opentelemetry.io/otel/sdk/metric.TestNewPeriodicReaderInstRace()
      sdk/metric/periodic_reader_race_repro_test.go:23 +0xa8
  testing.tRunner()
      testing.go:2036 +0x164
==================
--- FAIL: TestNewPeriodicReaderInstRace (0.00s)
FAIL
FAIL  go.opentelemetry.io/otel/sdk/metric     0.808s

Proposed fix

I believe the fix should be quite simple. We just have to move the initialization of r.inst to before the start of the goroutine. See:

      r.externalProducers.Store(conf.producers)

-     go func() {
-             defer func() { close(r.done) }()
-             r.run(ctx, conf.interval)
-     }()
-
      var err error
      r.inst, err = observ.NewInstrumentation(
              semconv.OTelComponentTypePeriodicMetricReader.Value.AsString(),
              nextPeriodicReaderID(),
      )
      if err != nil {
              otel.Handle(err)
      }

+     go func() {
+             defer func() { close(r.done) }()
+             r.run(ctx, conf.interval)
+     }()
+
      return r
 }

Expected behavior

Expect no data race.

Activity

  1. added
    bug
    on Aug 17, 2026
  2. ShreeyashPatil2708 commented on Aug 23, 2026

    @ShreeyashPatil2708
    Contributor

    I'd like to take a stab at this. The proposed fix looks correct to me. I'll open a PR shortly

  3. CTrando commented on Sep 24, 2026

    @CTrando
    ContributorAuthor

    I'll be taking over this issue. Thank you!

  4. added this to the v1.47.0 milestone on Oct 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Fields

Priority

None yet

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions