Skip to content

Commit d2345a4

Browse files
committed
[SDK] Fix lost wakeups in BatchSpanProcessor
1 parent d8786fb commit d2345a4

2 files changed

Lines changed: 28 additions & 15 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ Increment the:
1515

1616
## [Unreleased]
1717

18+
* [SDK] Fix lost-wakeups in BatchSpanProcessor to prevent stalls during
19+
shutdown and force flush.
20+
[#4382](https://github.com/open-telemetry/opentelemetry-cpp/pull/4382)
21+
1822
* [CONFIGURATION] Add support for the composite sampler configuration
1923
(programmatic and from yaml)
2024
([#4366](https://github.com/open-telemetry/opentelemetry-cpp/pull/4366))

‎sdk/src/trace/batch_span_processor.cc‎

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ void BatchSpanProcessor::OnEnd(std::unique_ptr<Recordable> &&span) noexcept
9898
size_t buffer_size = buffer_.size();
9999
if (buffer_size >= max_queue_size_ / 2 || buffer_size >= max_export_batch_size_)
100100
{
101-
// signal the worker thread
101+
// Best effort wakeup for worker thread.
102102
synchronization_data_->cv.notify_all();
103103
}
104104
}
@@ -127,6 +127,7 @@ bool BatchSpanProcessor::ForceFlush(std::chrono::microseconds timeout) noexcept
127127
if (synchronization_data_->force_flush_pending_sequence.load(std::memory_order_acquire) >
128128
synchronization_data_->force_flush_notified_sequence.load(std::memory_order_acquire))
129129
{
130+
std::lock_guard<std::mutex> cv_lock(synchronization_data_->cv_m);
130131
synchronization_data_->is_force_wakeup_background_worker.store(true,
131132
std::memory_order_release);
132133
synchronization_data_->cv.notify_all();
@@ -188,18 +189,21 @@ void BatchSpanProcessor::DoBackgroundWork()
188189
}
189190
#endif /* ENABLE_THREAD_INSTRUMENTATION_PREVIEW */
190191

191-
// Wait for `timeout` milliseconds
192-
std::unique_lock<std::mutex> lk(synchronization_data_->cv_m);
193-
synchronization_data_->cv.wait_for(lk, timeout, [this] {
194-
if (synchronization_data_->is_force_wakeup_background_worker.load(std::memory_order_acquire))
195-
{
196-
return true;
197-
}
198-
199-
return !buffer_.empty();
200-
});
201-
synchronization_data_->is_force_wakeup_background_worker.store(false,
202-
std::memory_order_release);
192+
// Wait for `timeout` milliseconds.
193+
{
194+
std::unique_lock<std::mutex> lk(synchronization_data_->cv_m);
195+
synchronization_data_->cv.wait_for(lk, timeout, [this] {
196+
if (synchronization_data_->is_force_wakeup_background_worker.load(
197+
std::memory_order_acquire))
198+
{
199+
return true;
200+
}
201+
202+
return !buffer_.empty();
203+
});
204+
synchronization_data_->is_force_wakeup_background_worker.store(false,
205+
std::memory_order_release);
206+
}
203207

204208
#ifdef ENABLE_THREAD_INSTRUMENTATION_PREVIEW
205209
if (worker_thread_instrumentation_ != nullptr)
@@ -309,6 +313,7 @@ void BatchSpanProcessor::NotifyCompletion(
309313
exporter->ForceFlush(timeout);
310314
}
311315

316+
std::lock_guard<std::mutex> lock(synchronization_data->force_flush_cv_m);
312317
std::uint64_t notified_sequence =
313318
synchronization_data->force_flush_notified_sequence.load(std::memory_order_acquire);
314319
while (notify_force_flush > notified_sequence)
@@ -376,8 +381,12 @@ bool BatchSpanProcessor::InternalShutdown(std::chrono::microseconds timeout) noe
376381

377382
if (worker_thread_.joinable())
378383
{
379-
synchronization_data_->is_force_wakeup_background_worker.store(true, std::memory_order_release);
380-
synchronization_data_->cv.notify_all();
384+
{
385+
std::lock_guard<std::mutex> cv_lock(synchronization_data_->cv_m);
386+
synchronization_data_->is_force_wakeup_background_worker.store(true,
387+
std::memory_order_release);
388+
synchronization_data_->cv.notify_all();
389+
}
381390
worker_thread_.join();
382391
}
383392

0 commit comments

Comments
 (0)