Skip to content

Commit

Permalink
Merge pull request #42 from opentracing/debug-assertion
Browse files Browse the repository at this point in the history
Improve the "use-after-free" assertion
  • Loading branch information
tbg authored Sep 28, 2016
2 parents 9f35438 + d41e5fc commit 15d0e11
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
15 changes: 10 additions & 5 deletions debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ import (
const debugGoroutineIDTag = "_initial_goroutine"

type errAssertionFailed struct {
msg string
span *spanImpl
msg string
}

// Error implements the error interface.
func (err *errAssertionFailed) Error() string {
return fmt.Sprintf("%s:\n%+v", err.msg, err.span)
}

func (s *spanImpl) Lock() {
Expand All @@ -22,9 +28,7 @@ func (s *spanImpl) Lock() {
func (s *spanImpl) maybeAssertSanityLocked() {
if s.tracer == nil {
s.Mutex.Unlock()
panic(&errAssertionFailed{
msg: fmt.Sprintf("span used after Finish()"),
})
panic(&errAssertionFailed{span: s, msg: "span used after call to Finish()"})
}
if s.tracer.options.DebugAssertSingleGoroutine {
startID := curGoroutineID()
Expand All @@ -37,7 +41,8 @@ func (s *spanImpl) maybeAssertSanityLocked() {
if startID != curID {
s.Mutex.Unlock()
panic(&errAssertionFailed{
msg: fmt.Sprintf("span started on goroutine %d, but now running on %d", startID, curID),
span: s,
msg: fmt.Sprintf("span started on goroutine %d, but now running on %d", startID, curID),
})
}
}
Expand Down
4 changes: 3 additions & 1 deletion span.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ func (s *spanImpl) FinishWithOptions(opts opentracing.FinishOptions) {
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`.
s.reset()
// We don't call `reset()` here to preserve the logs in the Span
// which are printed when the assertion triggers.
s.tracer = nil
}

if poolEnabled {
Expand Down

0 comments on commit 15d0e11

Please sign in to comment.