Skip to content

Commit

Permalink
Merge pull request #16 from opentracing/fix_pool
Browse files Browse the repository at this point in the history
Fix SIGSEGV due to incorrect usage of sync.Pool
  • Loading branch information
tbg committed Mar 18, 2016
2 parents c607206 + 079d44b commit ee13d47
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
11 changes: 9 additions & 2 deletions debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ type errAssertionFailed struct {

func (s *spanImpl) Lock() {
s.Mutex.Lock()
s.maybeAssertLocked()
s.maybeAssertSanityLocked()
}

func (s *spanImpl) maybeAssertLocked() {
func (s *spanImpl) maybeAssertSanityLocked() {
if s.tracer == nil {
s.Mutex.Unlock()
panic(&errAssertionFailed{
msg: fmt.Sprintf("span used after Finish()"),
})
}
if s.tracer.Options.DebugAssertSingleGoroutine {
startID := curGoroutineID()
curID, ok := s.raw.Tags[debugGoroutineIDTag].(uint64)
Expand All @@ -29,6 +35,7 @@ func (s *spanImpl) maybeAssertLocked() {
return
}
if startID != curID {
s.Mutex.Unlock()
panic(&errAssertionFailed{
msg: fmt.Sprintf("span started on goroutine %d, but now running on %d", startID, curID),
})
Expand Down
12 changes: 6 additions & 6 deletions span.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ type spanImpl struct {
raw RawSpan
}

var spanPool = &sync.Pool{New: func() interface{} {
return &spanImpl{}
}}

func (s *spanImpl) reset() {
s.tracer, s.event = nil, nil
// Note: Would like to do the following, but then the consumer of RawSpan
Expand Down Expand Up @@ -108,24 +112,20 @@ func (s *spanImpl) FinishWithOptions(opts opentracing.FinishOptions) {
duration := finishTime.Sub(s.raw.Start)

s.Lock()
defer s.Unlock()
if opts.BulkLogData != nil {
s.raw.Logs = append(s.raw.Logs, opts.BulkLogData...)
}
s.raw.Duration = duration
s.Unlock()

s.onFinish(s.raw)
s.tracer.Recorder.RecordSpan(s.raw)
if s.tracer.Options.DebugAssertUseAfterFinish {
// This makes it much more likely to catch a panic on any subsequent
// operation since s.tracer is accessed on every call to `Lock`.
pool := s.tracer.spanPool
s.reset()
pool.Put(s)
} else {
s.tracer.spanPool.Put(s)
}

spanPool.Put(s)
}

func (s *spanImpl) SetBaggageItem(restrictedKey, val string) opentracing.Span {
Expand Down
11 changes: 2 additions & 9 deletions tracer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package basictracer

import (
"sync"
"time"

opentracing "github.com/opentracing/opentracing-go"
Expand Down Expand Up @@ -79,12 +78,7 @@ func DefaultOptions() Options {

// NewWithOptions creates a customized Tracer.
func NewWithOptions(opts Options) opentracing.Tracer {
rval := &tracerImpl{
Options: opts,
spanPool: sync.Pool{New: func() interface{} {
return &spanImpl{}
}},
}
rval := &tracerImpl{Options: opts}
rval.textPropagator = &splitTextPropagator{rval}
rval.binaryPropagator = &splitBinaryPropagator{rval}
rval.goHTTPPropagator = &goHTTPPropagator{rval.binaryPropagator}
Expand All @@ -105,7 +99,6 @@ func New(recorder SpanRecorder) opentracing.Tracer {
// Implements the `Tracer` interface.
type tracerImpl struct {
Options
spanPool sync.Pool
textPropagator *splitTextPropagator
binaryPropagator *splitBinaryPropagator
goHTTPPropagator *goHTTPPropagator
Expand All @@ -122,7 +115,7 @@ func (t *tracerImpl) StartSpan(
}

func (t *tracerImpl) getSpan() *spanImpl {
sp := t.spanPool.Get().(*spanImpl)
sp := spanPool.Get().(*spanImpl)
sp.reset()
return sp
}
Expand Down

0 comments on commit ee13d47

Please sign in to comment.