Skip to content

Commit

Permalink
Add TestBatchSpanProcessorConcurrentSafe and fix deadlock
Browse files Browse the repository at this point in the history
  • Loading branch information
pellared committed Jul 24, 2023
1 parent 7993242 commit 68e62aa
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions sdk/trace/batch_span_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ func (bsp *batchSpanProcessor) ForceFlush(ctx context.Context) error {
flushCh := make(chan struct{})
if bsp.enqueueBlockOnQueueFull(ctx, forceFlushSpan{flushed: flushCh}) {
select {
case <-bsp.stopCh:
// The batchSpanProcessor is Shutdown.
return nil

Check warning on line 200 in sdk/trace/batch_span_processor.go

View check run for this annotation

Codecov / codecov/patch

sdk/trace/batch_span_processor.go#L198-L200

Added lines #L198 - L200 were not covered by tests
case <-flushCh:
// Processed any items in queue prior to ForceFlush being called
case <-ctx.Done():
Expand Down
43 changes: 43 additions & 0 deletions sdk/trace/batch_span_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,49 @@ func TestBatchSpanProcessorForceFlushQueuedSpans(t *testing.T) {
}
}

func TestBatchSpanProcessorConcurrentSafe(t *testing.T) {
ctx := context.Background()
var bp testBatchExporter
bsp := sdktrace.NewBatchSpanProcessor(&bp)
tp := basicTracerProvider(t)
tp.RegisterSpanProcessor(bsp)
tr := tp.Tracer(t.Name())

var wg sync.WaitGroup

wg.Add(1)
go func() {
defer wg.Done()
generateSpan(t, tr, testOption{genNumSpans: 1})
}()

wg.Add(1)
go func() {
defer wg.Done()
_ = bsp.ForceFlush(ctx)
}()

wg.Add(1)
go func() {
defer wg.Done()
_ = bsp.Shutdown(ctx)
}()

wg.Add(1)
go func() {
defer wg.Done()
_ = tp.ForceFlush(ctx)
}()

wg.Add(1)
go func() {
defer wg.Done()
_ = tp.Shutdown(ctx)
}()

wg.Wait()
}

func BenchmarkSpanProcessor(b *testing.B) {
tp := sdktrace.NewTracerProvider(
sdktrace.WithBatcher(
Expand Down

0 comments on commit 68e62aa

Please sign in to comment.