sdk/metric: fix data race in NewPeriodicReader - #8842
Conversation
|
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #8842 +/- ##
=====================================
Coverage 88.4% 88.4%
=====================================
Files 331 331
Lines 21001 21001
=====================================
+ Hits 18571 18574 +3
+ Misses 2430 2427 -3
🚀 New features to boost your workflow:
|
pellared
left a comment
There was a problem hiding this comment.
panic: test timed out after 1m0s
running tests:
TestNewPeriodicReaderConcurrentSafe (59s)
MrAlias
left a comment
There was a problem hiding this comment.
The constructor ordering fix looks correct. The new regression test needs to isolate its expected error handling so the compatibility suite does not deadlock.
|
|
||
| func TestNewPeriodicReaderConcurrentSafe(t *testing.T) { | ||
| for range 50 { | ||
| r := NewPeriodicReader(new(fnExporter), WithInterval(time.Nanosecond)) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
done, pls check
MrAlias
left a comment
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
will work on it, give me a couple of days...
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.