From 3ef2f4773d72621c17925cf0b4c726193e093434 Mon Sep 17 00:00:00 2001 From: Yuri Shkuro Date: Tue, 13 Mar 2018 17:53:31 -0400 Subject: [PATCH] Use lock when retrieving span.Context() Signed-off-by: Yuri Shkuro --- span.go | 2 ++ span_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/span.go b/span.go index 132fb721..17f7f35c 100644 --- a/span.go +++ b/span.go @@ -202,6 +202,8 @@ func (s *Span) FinishWithOptions(options opentracing.FinishOptions) { // Context implements opentracing.Span API func (s *Span) Context() opentracing.SpanContext { + s.Lock() + defer s.Unlock() return s.context } diff --git a/span_test.go b/span_test.go index 471ffac0..4d89220a 100644 --- a/span_test.go +++ b/span_test.go @@ -15,6 +15,7 @@ package jaeger import ( + "sync" "testing" "github.com/opentracing/opentracing-go" @@ -88,3 +89,27 @@ func TestSpanOperationName(t *testing.T) { assert.Equal(t, "s2", sp1.OperationName()) } + +func TestBaggageContextRace(t *testing.T) { + tracer, closer := NewTracer("DOOP", NewConstSampler(true), NewNullReporter()) + defer closer.Close() + + sp1 := tracer.StartSpan("s1").(*Span) + + var startWg, endWg sync.WaitGroup + startWg.Add(1) + endWg.Add(2) + + f := func() { + startWg.Wait() + sp1.SetBaggageItem("x", "y") + sp1.Context().ForeachBaggageItem(func(k, v string) bool { return false }) + endWg.Done() + } + + go f() + go f() + + startWg.Done() + endWg.Wait() +}