Skip to content

sdk/metric: fix data race in NewPeriodicReader - #8842

Open
ShreeyashPatil2708 wants to merge 5 commits into
open-telemetry:mainfrom
ShreeyashPatil2708:fix-periodic-reader-inst-race
Open

sdk/metric: fix data race in NewPeriodicReader#8842
ShreeyashPatil2708 wants to merge 5 commits into
open-telemetry:mainfrom
ShreeyashPatil2708:fix-periodic-reader-inst-race

Conversation

@ShreeyashPatil2708

Copy link
Copy Markdown
Contributor

Fixes #8769

NewPeriodicReader started the background goroutine before initializing r.inst, creating a data race between the constructor's write and the goroutine's read of that field via collect(). This moves the r.inst initialization before the goroutine launch to eliminate the race.

Added TestNewPeriodicReaderConcurrentSafe which reproduces the race under -race and passes after the fix.

@linux-foundation-easycla

linux-foundation-easycla Bot commented Aug 23, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: ShreeyashPatil2708 / name: ShreeyashPatil2708 (6930cec)

@codecov

codecov Bot commented Aug 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.4%. Comparing base (58db4c8) to head (4a1ca17).
⚠️ Report is 32 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@          Coverage Diff          @@
##            main   #8842   +/-   ##
=====================================
  Coverage   88.4%   88.4%           
=====================================
  Files        331     331           
  Lines      21001   21001           
=====================================
+ Hits       18571   18574    +3     
+ Misses      2430    2427    -3     
Files with missing lines Coverage Δ
sdk/metric/periodic_reader.go 87.1% <100.0%> (ø)

... and 16 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@pellared pellared left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

panic: test timed out after 1m0s
	running tests:
		TestNewPeriodicReaderConcurrentSafe (59s)

@MrAlias MrAlias left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The constructor ordering fix looks correct. The new regression test needs to isolate its expected error handling so the compatibility suite does not deadlock.

Comment thread sdk/metric/periodic_reader_test.go Outdated

func TestNewPeriodicReaderConcurrentSafe(t *testing.T) {
for range 50 {
r := NewPeriodicReader(new(fnExporter), WithInterval(time.Nanosecond))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test intentionally allows collection before register to exercise the constructor race, which also sends ErrReaderNotRegistered through the process-global error handler. Existing tests can leave a handler that blocks or refers to an already-completed test; CI hit the blocking path and left Shutdown waiting on r.done.

Could we install a known non-blocking error handler for this test and restore the previous handler in cleanup?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, pls check

@MrAlias MrAlias left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The constructor reordering itself fixes the reported unsynchronized access, and the no-op error handler resolves the previously reported test deadlock. The sdk/metric package passes locally under the race detector. GitHub CI remains mostly pending after the latest merge from main.

otel.SetErrorHandler(otel.ErrorHandlerFunc(func(error) {}))
t.Cleanup(func() { otel.SetErrorHandler(origErrorHandler) })

for range 50 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test does not reliably exercise the pre-fix race. After restoring the old constructor ordering, it still passed under go test -race for 100 repetitions—5,000 reader constructions—because nothing ensures the run goroutine reads r.inst before NewInstrumentation assigns it.

Could we synchronize the test around that window so the old ordering fails deterministically? Otherwise the regression can return while this test remains green.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will work on it, give me a couple of days...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Data race when initializing PeriodicReader with short interval

3 participants