cihub / seelog

Seelog is a native Go logging library that provides flexible asynchronous dispatching, filtering, and formatting.
BSD 3-Clause "New" or "Revised" License
1.64k stars 243 forks source link

goroutine leak in package detected #182

Open crobert-1 opened 9 months ago

crobert-1 commented 9 months ago

Context Hello, I'm currently working on https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/30438 to enable goleak to check for leaked goroutines.

Bug Here's the goleak output for the leaking goroutine:

[Goroutine 13 in state sync.Cond.Wait, with sync.runtime_notifyListWait on top of the stack:
sync.runtime_notifyListWait(0xc002140d10, 0x0)
        /usr/local/Cellar/go/1.21.0/libexec/src/runtime/sema.go:527 +0x159
sync.(*Cond).Wait(0x0?)
        /usr/local/Cellar/go/1.21.0/libexec/src/sync/cond.go:70 +0x85
github.com/cihub/seelog.(*asyncLoopLogger).processItem(0xc0020e46c0)
        /Users/crobert/go/pkg/mod/github.com/cihub/seelog@v0.0.0-20170130134532-f561c5e57575/behavior_asynclooplogger.go:50 +0x99
github.com/cihub/seelog.(*asyncLoopLogger).processQueue(0xc0020e46c0)
        /Users/crobert/go/pkg/mod/github.com/cihub/seelog@v0.0.0-20170130134532-f561c5e57575/behavior_asynclooplogger.go:63 +0x33
created by github.com/cihub/seelog.NewAsyncLoopLogger in goroutine 1
        /Users/crobert/go/pkg/mod/github.com/cihub/seelog@v0.0.0-20170130134532-f561c5e57575/behavior_asynclooplogger.go:40 +0xcf

Here's the line that causing it: https://github.com/cihub/seelog/blob/f561c5e57575bb1e0a2167028b7339b3a8d16fb4/behavior_asynclooplogger.go#L40

This call is happening on init(), which means the goroutine is started when a package is imported, even if it isn't used.

Solution It's generally not good practice to start a goroutine in init. The best solution here would only be start the goroutine when required, and provide a public API to stop it.

Related:

86

songy23 commented 3 months ago

+1 frequency in https://github.com/open-telemetry/opentelemetry-collector-contrib/pull/34030

songy23 commented 1 month ago

+1 https://github.com/open-telemetry/opentelemetry-collector-contrib/actions/runs/10788871238/job/29920787176?pr=35104