From 242d52ac67a8c20484cddabd2b3feeebeb4399b0 Mon Sep 17 00:00:00 2001 From: WenTao Ou Date: Thu, 22 Aug 2024 06:36:53 +0800 Subject: [PATCH] [SDK] PeriodicExportingMetricReader: future is never set, blocks until timeout (#3030) --- .../periodic_exporting_metric_reader.cc | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/sdk/src/metrics/export/periodic_exporting_metric_reader.cc b/sdk/src/metrics/export/periodic_exporting_metric_reader.cc index 7ca2747337..1ebfc2a1b6 100644 --- a/sdk/src/metrics/export/periodic_exporting_metric_reader.cc +++ b/sdk/src/metrics/export/periodic_exporting_metric_reader.cc @@ -106,19 +106,22 @@ bool PeriodicExportingMetricReader::CollectAndExportOnce() std::promise sender; auto receiver = sender.get_future(); - task_thread.reset(new std::thread([this, &cancel_export_for_timeout] { - this->Collect([this, &cancel_export_for_timeout](ResourceMetrics &metric_data) { - if (cancel_export_for_timeout.load(std::memory_order_acquire)) - { - OTEL_INTERNAL_LOG_ERROR( - "[Periodic Exporting Metric Reader] Collect took longer configured time: " - << this->export_timeout_millis_.count() << " ms, and timed out"); - return false; - } - this->exporter_->Export(metric_data); - return true; - }); - })); + task_thread.reset( + new std::thread([this, &cancel_export_for_timeout, sender = std::move(sender)] { + this->Collect([this, &cancel_export_for_timeout](ResourceMetrics &metric_data) { + if (cancel_export_for_timeout.load(std::memory_order_acquire)) + { + OTEL_INTERNAL_LOG_ERROR( + "[Periodic Exporting Metric Reader] Collect took longer configured time: " + << this->export_timeout_millis_.count() << " ms, and timed out"); + return false; + } + this->exporter_->Export(metric_data); + return true; + }); + + const_cast &>(sender).set_value(); + })); std::future_status status; do