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.
5859constexpr std::chrono::minutes kParkScheduleDelay {10 };
5960constexpr 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.
6266template <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+
7899template <typename Operation>
79100void 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.
122133TEST (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
140188OPENTELEMETRY_END_NAMESPACE
0 commit comments