[SDK] Add destructor to PeriodicExportingMetricReader to fix shutdown race#4008
[SDK] Add destructor to PeriodicExportingMetricReader to fix shutdown race#4008jnillius wants to merge 5 commits intoopen-telemetry:mainfrom
Conversation
… race Destroying a PeriodicExportingMetricReader without calling Shutdown() leaves the background worker thread running inside cv_.wait_for() while the mutex and condition variable members are destroyed. This causes a use-after-destroy race detected by ThreadSanitizer. Add an explicit destructor that calls Shutdown() and joins the worker thread before member destruction, matching the pattern already used by BatchSpanProcessor and BatchLogRecordProcessor. Add a DestroyWithoutShutdown test to verify safe destruction without an explicit Shutdown() call.
d2f7139 to
19f4a24
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4008 +/- ##
==========================================
+ Coverage 90.32% 90.32% +0.01%
==========================================
Files 230 230
Lines 7298 7302 +4
==========================================
+ Hits 6591 6595 +4
Misses 707 707
🚀 New features to boost your workflow:
|
|
Thanks for the bug report and fix. Please sign the EasyCLA, and fix clang-format. |
dbarker
left a comment
There was a problem hiding this comment.
Good catch! Thanks for the PR. Please see feedback below on some minor points.
|
Add an entry to CHANGELOG.md? |
6ddc6de to
c4dc458
Compare
c4dc458 to
6948bb0
Compare
marcalff
left a comment
There was a problem hiding this comment.
Please fix clang-format.
LGTM otherwise.
- Delete copy/move constructors and assignment operators to satisfy cppcoreguidelines-special-member-functions (Rule of Five). - Remove redundant worker_thread_.join() from destructor; Shutdown() already calls OnShutDown() which joins the thread. - Use std::make_unique in DestroyWithoutShutdown test.
6948bb0 to
00cfffb
Compare
marcalff
left a comment
There was a problem hiding this comment.
LGTM, thanks for the contribution.
|
|
||
| PeriodicExportingMetricReader::~PeriodicExportingMetricReader() | ||
| { | ||
| if (!IsShutdown()) |
There was a problem hiding this comment.
Is this IsShutdown() check needed? Shutdown() is already idempotent on MetricReader. The only effect of this guard is to suppress the
"Cannot invoke shutdown twice!" warning. Worth a short
comment explaining that, or drop the guard to match the
BatchSpanProcessor dtor pattern.
Fixes #4007
Changes
Destroying a PeriodicExportingMetricReader without calling Shutdown()
leaves the background worker thread running inside cv_.wait_for() while
the mutex and condition variable members are destroyed. This causes a
use-after-destroy race detected by ThreadSanitizer.
Add an explicit destructor that calls Shutdown() and joins the worker
thread before member destruction, matching the pattern already used by
BatchSpanProcessor and BatchLogRecordProcessor.
Add a DestroyWithoutShutdown test to verify safe destruction without
an explicit Shutdown() call.
CHANGELOG.mdupdated for non-trivial changes