[SDK] Add destructor to PeriodicExportingMetricReader to fix shutdown race#4008
Open
jnillius wants to merge 1 commit intoopen-telemetry:mainfrom
Open
[SDK] Add destructor to PeriodicExportingMetricReader to fix shutdown race#4008jnillius wants to merge 1 commit intoopen-telemetry:mainfrom
jnillius wants to merge 1 commit 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❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4008 +/- ##
==========================================
- Coverage 90.18% 90.16% -0.01%
==========================================
Files 230 230
Lines 7299 7306 +7
==========================================
+ Hits 6582 6587 +5
- Misses 717 719 +2
🚀 New features to boost your workflow:
|
Member
|
Thanks for the bug report and fix. Please sign the EasyCLA, and fix clang-format. |
dbarker
reviewed
Apr 16, 2026
Member
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.
| const PeriodicExportingMetricReaderOptions &options, | ||
| const PeriodicExportingMetricReaderRuntimeOptions &runtime_options); | ||
|
|
||
| ~PeriodicExportingMetricReader() override; |
Member
There was a problem hiding this comment.
Please fix the new clang-tidy warning
| Line | Check | Message |
|---|---|---|
| 28 | cppcoreguidelines-special-member-functions | class 'PeriodicExportingMetricReader' defines a destructor but does not define a copy constructor, a copy assignment operator, a move constructor or a move assignment operator |
| // background worker thread. Before the destructor fix this test would fail | ||
| // under ThreadSanitizer with: | ||
| // WARNING: ThreadSanitizer: unlock of an unlocked mutex (or by a wrong thread) | ||
| std::unique_ptr<PushMetricExporter> exporter( |
Member
There was a problem hiding this comment.
nit - can use std::make_unique here
| Shutdown(); | ||
| } | ||
|
|
||
| if (worker_thread_.joinable()) |
Member
There was a problem hiding this comment.
This section to join the thread can be removed. Shutdown will call OnShutdown which joins the thread (and calls Shutdown on the exporter).
Contributor
|
Add an entry to CHANGELOG.md? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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