Skip to content

Commit 88dea80

Browse files
authored
[SDK] Fix lost wakeup in BatchSpanProcessor OnEnd notify (#4741)
1 parent 90af189 commit 88dea80

4 files changed

Lines changed: 80 additions & 21 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ Increment the:
4242
and empty point guards
4343
[#4738](https://github.com/open-telemetry/opentelemetry-cpp/pull/4738)
4444

45+
* [SDK] Fix lost wakeup in BatchSpanProcessor `OnEnd` notify when a span
46+
fills the queue, which could stall the export and drop spans until the
47+
schedule delay.
48+
[#4741](https://github.com/open-telemetry/opentelemetry-cpp/pull/4741)
49+
4550
## [1.30.0] 2026-10-22
4651

4752
This release contains:

‎sdk/src/trace/batch_span_processor.cc‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,13 @@ void BatchSpanProcessor::OnEnd(std::unique_ptr<Recordable> &&span) noexcept
117117
// If the queue gets at least half full a preemptive notification is
118118
// sent to the worker thread to start a new export cycle.
119119
size_t buffer_size = buffer_.size();
120-
if (buffer_size >= max_queue_size_ / 2 || buffer_size >= max_export_batch_size_)
120+
if (buffer_size >= max_queue_size_)
121+
{
122+
// Later spans are dropped before they notify, so this notify must not be lost.
123+
std::lock_guard<std::mutex> cv_lock(synchronization_data_->cv_m);
124+
synchronization_data_->cv.notify_all();
125+
}
126+
else if (buffer_size >= max_queue_size_ / 2 || buffer_size >= max_export_batch_size_)
121127
{
122128
// Notified without lock to reduce contention for span end. If this notify is lost,
123129
// the worker thread may wait until next schedule or until the next notify attempt.

‎sdk/test/trace/batch_span_processor_test.cc‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -479,23 +479,23 @@ TEST_F(BatchSpanProcessorTestPeer, TestMaxExportBatchSizeClampedToMaxQueueSize)
479479
const int num_spans = 8;
480480

481481
const auto test_spans = GetTestSpans(batch_processor, num_spans);
482-
for (int i = 0; i < num_spans - 1; ++i)
482+
for (int i = 0; i < num_spans; ++i)
483483
{
484484
batch_processor->OnEnd(std::move(test_spans->at(i)));
485485
}
486486

487-
// Let the worker re-wait first, so the notify from the batch-filling span is not lost.
488-
std::this_thread::sleep_for(std::chrono::milliseconds(10));
489-
batch_processor->OnEnd(std::move(test_spans->at(num_spans - 1)));
490-
491487
// Well before schedule_delay_millis, so only a full batch can trigger the export.
492488
const auto wait_start = std::chrono::steady_clock::now();
493489
while (spans_received_count->load() < static_cast<std::size_t>(num_spans) &&
494490
std::chrono::steady_clock::now() - wait_start < std::chrono::seconds(2))
495491
{
496492
std::this_thread::sleep_for(std::chrono::milliseconds(1));
497493
}
494+
const auto spans_exported_before_delay = spans_received_count->load();
495+
496+
EXPECT_TRUE(batch_processor->Shutdown());
498497

498+
EXPECT_EQ(static_cast<std::size_t>(num_spans), spans_exported_before_delay);
499499
EXPECT_EQ(std::vector<std::size_t>({options.max_queue_size}), *batch_sizes);
500500
}
501501

‎sdk/test/trace/batch_span_processor_test_stress.cc‎

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <iostream>
1010
#include <memory>
1111
#include <string>
12+
#include <thread>
1213
#include <utility>
1314

1415
#include "opentelemetry/nostd/span.h"
@@ -57,10 +58,13 @@ class CountingSpanExporter final : public sdk::trace::SpanExporter
5758
// while being generous enough to avoid false positives on slow CI runners.
5859
constexpr std::chrono::minutes kParkScheduleDelay{10};
5960
constexpr std::chrono::minutes kWakeupWatchdog{1};
61+
constexpr int kRounds = 2000;
62+
constexpr const char *kShutdownStallHint =
63+
"possible lost shutdown wakeup stall during worker join()";
6064

6165
// Runs `operation` on another thread and aborts the binary if it does not return in time.
6266
template <typename Operation>
63-
bool CallWithWatchdog(const char *operation_name,
67+
auto CallWithWatchdog(const char *operation_name,
6468
const char *stall_hint,
6569
int round,
6670
const Operation &operation)
@@ -75,10 +79,26 @@ bool CallWithWatchdog(const char *operation_name,
7579
return result.get();
7680
}
7781

82+
// Busy-spins a scheduling-independent amount to offset the next call within a race window.
83+
void BusySpin(int iterations)
84+
{
85+
volatile int spin_sink = 0;
86+
for (int s = 0; s < iterations; ++s)
87+
{
88+
int next = spin_sink;
89+
spin_sink = next + 1;
90+
}
91+
}
92+
93+
void ShutdownWithWatchdog(sdk::trace::BatchSpanProcessor &processor, int round)
94+
{
95+
EXPECT_TRUE(CallWithWatchdog("teardown Shutdown()", kShutdownStallHint, round,
96+
[&processor] { return processor.Shutdown(); }));
97+
}
98+
7899
template <typename Operation>
79100
void RunWorkerParkRace(const char *operation_name, const char *stall_hint, Operation operation)
80101
{
81-
constexpr int kRounds = 2000;
82102
constexpr int kSpinSweep = 50;
83103

84104
for (int round = 0; round < kRounds; ++round)
@@ -95,14 +115,7 @@ void RunWorkerParkRace(const char *operation_name, const char *stall_hint, Opera
95115

96116
// Vary the offset across a sweep so that over the whole set we have a better chance of hitting
97117
// the race window.
98-
int spin_iterations = round * kSpinSweep;
99-
volatile int spin_sink = 0;
100-
for (int s = 0; s < spin_iterations; ++s)
101-
{
102-
// busy-spin a scheduling-independent increasing amount to sweep the race offset
103-
int next = spin_sink;
104-
spin_sink = next + 1;
105-
}
118+
BusySpin(round * kSpinSweep);
106119
processor->OnEnd(processor->MakeRecordable());
107120

108121
EXPECT_TRUE(CallWithWatchdog(operation_name, stall_hint, round,
@@ -111,18 +124,15 @@ void RunWorkerParkRace(const char *operation_name, const char *stall_hint, Opera
111124

112125
// Shutdown() already joined the worker; ForceFlush() left it running. Join it either way
113126
// before the next round.
114-
EXPECT_TRUE(CallWithWatchdog("teardown Shutdown()",
115-
"possible lost shutdown wakeup stall during worker join()", round,
116-
[processor] { return processor->Shutdown(); }));
127+
ShutdownWithWatchdog(*processor, round);
117128
}
118129
}
119130

120131
// Catch a lost cv wakeup during Shutdown(). A lost wakeup parks the worker for the whole schedule
121132
// delay, so the untimed join() inside Shutdown() blocks for that long.
122133
TEST(BatchSpanProcessorStress, ShutdownRacesWorkerPark)
123134
{
124-
RunWorkerParkRace("ShutdownRacesWorkerPark: Shutdown()",
125-
"possible lost shutdown wakeup stall during worker join()",
135+
RunWorkerParkRace("ShutdownRacesWorkerPark: Shutdown()", kShutdownStallHint,
126136
[](sdk::trace::BatchSpanProcessor &processor) { return processor.Shutdown(); });
127137
}
128138

@@ -135,6 +145,44 @@ TEST(BatchSpanProcessorStress, ForceFlushRacesWorkerPark)
135145
[](sdk::trace::BatchSpanProcessor &processor) { return processor.ForceFlush(); });
136146
}
137147

148+
// Catch a lost cv wakeup in OnEnd() from the span that fills the queue.
149+
TEST(BatchSpanProcessorStress, OnEndRacesWorkerPark)
150+
{
151+
static constexpr std::size_t kQueueSize = 2;
152+
constexpr int kSweepPeriod = 200;
153+
constexpr int kSpinSweep = 10;
154+
155+
for (int round = 0; round < kRounds; ++round)
156+
{
157+
auto exported_count = std::make_shared<std::atomic<std::size_t>>(0);
158+
159+
sdk::trace::BatchSpanProcessorOptions options;
160+
options.schedule_delay_millis = kParkScheduleDelay;
161+
options.max_queue_size = kQueueSize;
162+
options.max_export_batch_size = kQueueSize;
163+
164+
auto processor = std::make_shared<sdk::trace::BatchSpanProcessor>(
165+
std::make_unique<CountingSpanExporter>(exported_count), options);
166+
167+
// The half-full notify wakes the worker, which parks again on the partial batch.
168+
processor->OnEnd(processor->MakeRecordable());
169+
170+
// The race window sits shortly after that wakeup, so sweep short offsets repeatedly.
171+
BusySpin((round % kSweepPeriod) * kSpinSweep);
172+
processor->OnEnd(processor->MakeRecordable());
173+
174+
CallWithWatchdog("OnEndRacesWorkerPark: export", "possible lost OnEnd wakeup", round,
175+
[exported_count] {
176+
while (exported_count->load(std::memory_order_relaxed) < kQueueSize)
177+
{
178+
std::this_thread::yield();
179+
}
180+
});
181+
182+
ShutdownWithWatchdog(*processor, round);
183+
}
184+
}
185+
138186
} // namespace
139187

140188
OPENTELEMETRY_END_NAMESPACE

0 commit comments

Comments
 (0)