census-instrumentation / opencensus-go

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

stats: defaultWorker can not be stopped #1262

Closed cornelk closed 2 years ago

cornelk commented 2 years ago

Currently there is no way to stop the defaultWorker goroutine in the go.opencensus.io/stats/view/ package. This breaks adding a Goroutine leak detector to unit tests in packages that import the view package as it will always fail:

goleak: Errors on successful test run: found unexpected goroutines:
[Goroutine 21 in state select, with go.opencensus.io/stats/view.(*worker).start on top of the stack:
goroutine 21 [select]:
go.opencensus.io/stats/view.(*worker).start(0xc000272000)
    /home/user/go/pkg/mod/go.opencensus.io@v0.23.0/stats/view/worker.go:276 +0x157
created by go.opencensus.io/stats/view.init.0
    /home/user/go/pkg/mod/go.opencensus.io@v0.23.0/stats/view/worker.go:34 +0x87
markuspeloquin commented 1 year ago

This issue has been closed even though the fix isn't tagged for any release.

And just FYI, this fix cannot work with VerifyTestMain since you cannot trigger a cleanup before checking for leaks. You must basically reimplement it:

func TestMain(m *testing.M) {
    exitcode := 0
    defer func() { os.Exit(exitcode) }

    exitcode = m.Run()
    view.Stop()
    if err := goleak.Find(); err != nil {
        fmt.Fprintf(os.Stderr, "goleak: Errors on successful test run: %v\n", err)
        exitcode = 1
    }
}

IMO a package init() shouldn't be starting threads.