Skip to content

Commit 4f5ad0a

Browse files
bm1549claude
andcommitted
Fix flaky PendingTraceBufferTest by removing buffer thread race
The "continuation allows adding after root finished" test was flaky because it started the buffer worker thread before calling continuation.cancel(). The worker thread could process the enqueued trace before the continuation was cancelled, causing extra mock invocations (TooManyInvocationsError). The fix removes buffer.start() since it's unnecessary: when the continuation is cancelled and pendingReferenceCount drops to 0 with rootSpanWritten still false, PendingTrace.write() is called synchronously on the calling thread. This eliminates the race condition entirely. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 5ec9637 commit 4f5ad0a

1 file changed

Lines changed: 7 additions & 8 deletions

File tree

dd-trace-core/src/test/groovy/datadog/trace/core/PendingTraceBufferTest.groovy

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,6 @@ class PendingTraceBufferTest extends DDSpecification {
268268

269269
def "continuation allows adding after root finished"() {
270270
setup:
271-
def latch = new CountDownLatch(1)
272-
273271
def trace = factory.create(DDTraceId.ONE)
274272
def parent = addContinuation(newSpanOf(trace))
275273
TraceScope.Continuation continuation = continuations[0]
@@ -302,19 +300,20 @@ class PendingTraceBufferTest extends DDSpecification {
302300
!trace.rootSpanWritten
303301

304302
when:
305-
buffer.start()
303+
// Don't start the buffer thread here. When the continuation is cancelled,
304+
// pendingReferenceCount drops to 0 with rootSpanWritten still false, so
305+
// write() is called synchronously on this thread. Starting the buffer
306+
// would introduce a race where the worker thread could process the
307+
// enqueued trace before continuation.cancel(), causing extra mock
308+
// invocations (TooManyInvocationsError).
306309
continuation.cancel()
307-
latch.await()
308310

309311
then:
310312
trace.size() == 0
311313
trace.pendingReferenceCount == 0
312314
trace.rootSpanWritten
313-
_ * bufferSpy.longRunningSpansEnabled()
314315
1 * tracer.writeTimer() >> Monitoring.DISABLED.newTimer("")
315-
1 * tracer.write({ it.size() == 2 }) >> {
316-
latch.countDown()
317-
}
316+
1 * tracer.write({ it.size() == 2 })
318317
_ * tracer.getPartialFlushMinSpans() >> 10
319318
_ * tracer.getTagInterceptor()
320319
0 * _

0 commit comments

Comments
 (0)