Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: child span not acquiring transaction lock in some cases #1487

Merged
merged 14 commits into from
Jul 26, 2023
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
8 changes: 8 additions & 0 deletions span.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,14 @@ func (s *Span) End() {

if s.Type != parentType || s.Subtype != parentSubtype {
s.dropWhen(true)
if s.tx != nil {
s.tx.mu.Lock()
defer s.tx.mu.Unlock()
kruskall marked this conversation as resolved.
Show resolved Hide resolved
if !s.tx.ended() {
s.tx.TransactionData.mu.Lock()
defer s.tx.TransactionData.mu.Unlock()
}
}
s.end()
return
}
Expand Down
36 changes: 36 additions & 0 deletions span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,42 @@ func TestCompressSpanSameKindConcurrent(t *testing.T) {
assert.Equal(t, 101, spanCount)
}

func TestDroppedSpanParentConcurrent(t *testing.T) {
// This test verifies there aren't any deadlocks on calling
// span.End(), Parent.End() and tx.End().
tracer := apmtest.NewRecordingTracer()
kruskall marked this conversation as resolved.
Show resolved Hide resolved
defer tracer.Close()
tracer.SetExitSpanMinDuration(0)

tx := tracer.StartTransaction("name", "type")
ctx := apm.ContextWithTransaction(context.Background(), tx)

parent, _ := apm.StartSpanOptions(ctx, "parent", "request", apm.SpanOptions{
ExitSpan: true,
})
ctx = apm.ContextWithSpan(ctx, parent)

var wg sync.WaitGroup
count := 100
wg.Add(count)
for i := 0; i < count; i++ {
go func(i int) {
child, _ := apm.StartSpanOptions(ctx, fmt.Sprint(i), fmt.Sprintf("request%d", i), apm.SpanOptions{
ExitSpan: true,
})
child.Context.SetDestinationService(apm.DestinationServiceSpanContext{Resource: fmt.Sprintf("foo%d", i)})
child.End()
wg.Done()
}(i)
}

// Wait until all the spans have ended.
wg.Wait()
parent.End()
tx.End()
tracer.Flush(nil)
}

func TestCompressSpanPrematureEnd(t *testing.T) {
// This test cases assert that the cached spans are sent when the span or
// tx that holds their cache is ended and the cache isn't lost.
Expand Down