Skip to content

Commit

Permalink
Use lock when retrieving span.Context() (jaegertracing#268)
Browse files Browse the repository at this point in the history
Signed-off-by: Yuri Shkuro <ys@uber.com>
  • Loading branch information
yurishkuro authored and Isaac Hier committed Mar 26, 2018
1 parent 1d83b55 commit ef42508
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions span.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
25 changes: 25 additions & 0 deletions span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package jaeger

import (
"sync"
"testing"

"github.com/opentracing/opentracing-go"
Expand Down Expand Up @@ -121,3 +122,27 @@ type testThrottler struct {
func (t testThrottler) IsAllowed(operation string) bool {
return t.allowAll
}

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()
}

0 comments on commit ef42508

Please sign in to comment.