census-instrumentation / opencensus-go

A stats collection and distributed tracing framework
http://opencensus.io
Apache License 2.0
2.05k stars 326 forks source link

A memory leak cause by the active set of spanStore #1245

Closed NanoRed closed 3 years ago

NanoRed commented 3 years ago

Please answer these questions before submitting a bug report.

What version of OpenCensus are you using?

v0.22.5

What version of Go are you using?

1.13.15

What did you do?

import (
    "go.opencensus.io/trace"
    _ "go.opencensus.io/zpages" // internal.LocalSpanStoreEnabled = true
)

type SpanTest struct {
    Ctx   context.Context
    Count int
}

func (s *SpanTest) Inc() {
    span := trace.StartSpan(s.Ctx, "test")
    defer span.End()
}

func main() {
    test := &SpanTest{
        Ctx: context.Background(),
    }
    go func() {
        for {
            test.Inc()
        }
    }()
    for {}
}

it make memory usage gradually increasing. and i have found the problem: opencensus-go/trace/trace.go line:269 ss.add(NewSpan(s)) opencensus-go/trace/trace.go line:294 s.spanStore.finished(NewSpan(s), sd) when add a new span to the spanStore active set. it wrote a wrong way to release the active set, it shouldn't use NewSpan() to wrap the *span and pass to the finished function

NanoRed commented 3 years ago

has been fixed