Skip to content

[SDK] Add destructor to PeriodicExportingMetricReader to fix shutdown race#4008

Open
jnillius wants to merge 1 commit intoopen-telemetry:mainfrom
jnillius:fix/periodic-metric-reader-destructor
Open

[SDK] Add destructor to PeriodicExportingMetricReader to fix shutdown race#4008
jnillius wants to merge 1 commit intoopen-telemetry:mainfrom
jnillius:fix/periodic-metric-reader-destructor

Conversation

@jnillius
Copy link
Copy Markdown

@jnillius jnillius commented Apr 16, 2026

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.md updated for non-trivial changes
  • Unit tests have been added
  • Changes in public API reviewed — the only addition is the destructor declaration in the header; no existing API is changed

@jnillius jnillius requested a review from a team as a code owner April 16, 2026 13:48
@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla bot commented Apr 16, 2026

CLA Not Signed

… 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.
@jnillius jnillius force-pushed the fix/periodic-metric-reader-destructor branch from d2f7139 to 19f4a24 Compare April 16, 2026 13:51
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 16, 2026

Codecov Report

❌ Patch coverage is 71.42857% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.16%. Comparing base (a42027a) to head (19f4a24).

Files with missing lines Patch % Lines
...metrics/export/periodic_exporting_metric_reader.cc 71.43% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            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     
Files with missing lines Coverage Δ
...metrics/export/periodic_exporting_metric_reader.cc 77.00% <71.43%> (-0.41%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@marcalff marcalff added the pr:waiting-on-cla Waiting on CLA label Apr 16, 2026
@marcalff
Copy link
Copy Markdown
Member

@jnillius

Thanks for the bug report and fix.

Please sign the EasyCLA, and fix clang-format.

Copy link
Copy Markdown
Member

@dbarker dbarker left a comment

Choose a reason for hiding this comment

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

Good catch! Thanks for the PR. Please see feedback below on some minor points.

const PeriodicExportingMetricReaderOptions &options,
const PeriodicExportingMetricReaderRuntimeOptions &runtime_options);

~PeriodicExportingMetricReader() override;
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.

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(
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.

nit - can use std::make_unique here

Shutdown();
}

if (worker_thread_.joinable())
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.

This section to join the thread can be removed. Shutdown will call OnShutdown which joins the thread (and calls Shutdown on the exporter).

@ThomsonTan
Copy link
Copy Markdown
Contributor

Add an entry to CHANGELOG.md?

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

Labels

pr:waiting-on-cla Waiting on CLA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PeriodicExportingMetricReader: missing destructor causes use-after-destroy race (ThreadSanitizer)

4 participants