Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Use lock when retrieving span.Context() #268

Merged
merged 1 commit into from
Mar 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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()
}