Skip to content

Commit be00d4d

Browse files
authored
Merge branch 'main' into fix-random-memory-leak
2 parents e665d74 + 4f32bc6 commit be00d4d

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

sdk/include/opentelemetry/sdk/metrics/metric_reader.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
#pragma once
55

6+
#include <atomic>
67
#include <chrono>
78
#include <memory>
89

9-
#include "opentelemetry/common/spin_lock_mutex.h"
1010
#include "opentelemetry/nostd/function_ref.h"
1111
#include "opentelemetry/sdk/metrics/data/metric_data.h"
1212
#include "opentelemetry/sdk/metrics/instruments.h"
@@ -72,8 +72,7 @@ class MetricReader
7272
protected:
7373
private:
7474
MetricProducer *metric_producer_;
75-
mutable opentelemetry::common::SpinLockMutex lock_;
76-
bool shutdown_;
75+
std::atomic<bool> shutdown_{false};
7776
};
7877
} // namespace metrics
7978
} // namespace sdk

sdk/src/metrics/export/periodic_exporting_metric_reader.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
#include "opentelemetry/sdk/metrics/export/periodic_exporting_metric_reader.h"
5+
#include "opentelemetry/common/spin_lock_mutex.h"
56
#include "opentelemetry/sdk/common/global_log_handler.h"
67
#include "opentelemetry/sdk/metrics/push_metric_exporter.h"
78

@@ -101,6 +102,7 @@ bool PeriodicExportingMetricReader::CollectAndExportOnce()
101102
bool notify_force_flush = is_force_flush_pending_.exchange(false, std::memory_order_acq_rel);
102103
if (notify_force_flush)
103104
{
105+
std::unique_lock<std::mutex> lk(force_flush_m_);
104106
is_force_flush_notified_.store(true, std::memory_order_release);
105107
force_flush_cv_.notify_one();
106108
}
@@ -191,7 +193,11 @@ bool PeriodicExportingMetricReader::OnShutDown(std::chrono::microseconds timeout
191193
{
192194
if (worker_thread_.joinable())
193195
{
194-
cv_.notify_one();
196+
{
197+
// ensure that `cv_` is awaiting, and the update doesn't get lost
198+
std::unique_lock<std::mutex> lk(cv_m_);
199+
cv_.notify_all();
200+
}
195201
worker_thread_.join();
196202
}
197203
return exporter_->Shutdown(timeout);

sdk/src/metrics/metric_reader.cc

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,7 @@ bool MetricReader::Shutdown(std::chrono::microseconds timeout) noexcept
4848
OTEL_INTERNAL_LOG_WARN("MetricReader::Shutdown - Cannot invoke shutdown twice!");
4949
}
5050

51-
{
52-
const std::lock_guard<opentelemetry::common::SpinLockMutex> locked(lock_);
53-
shutdown_ = true;
54-
}
51+
shutdown_.store(true, std::memory_order_release);
5552

5653
if (!OnShutDown(timeout))
5754
{
@@ -65,7 +62,7 @@ bool MetricReader::Shutdown(std::chrono::microseconds timeout) noexcept
6562
bool MetricReader::ForceFlush(std::chrono::microseconds timeout) noexcept
6663
{
6764
bool status = true;
68-
if (shutdown_)
65+
if (IsShutdown())
6966
{
7067
OTEL_INTERNAL_LOG_WARN("MetricReader::Shutdown Cannot invoke Force flush on shutdown reader!");
7168
}
@@ -79,8 +76,7 @@ bool MetricReader::ForceFlush(std::chrono::microseconds timeout) noexcept
7976

8077
bool MetricReader::IsShutdown() const noexcept
8178
{
82-
const std::lock_guard<opentelemetry::common::SpinLockMutex> locked(lock_);
83-
return shutdown_;
79+
return shutdown_.load(std::memory_order_acquire);
8480
}
8581

8682
} // namespace metrics

0 commit comments

Comments
 (0)