![]() It provides more readable output and has additional APIs for using it inside a unit test. goleak.IgnoreCurrent is needed to filter out the goroutine that gets started when importing go.opencensus.io/stats/view. In order to handle background goroutines that get created on demand and cannot be stopped (like the one for LogzHealth), a helper function ensures that those are running before calling goleak.IgnoreCurrent. Keeping those goroutines running is not a problem and thus not worth the effort of adding new APIs to stop them. Other goroutines are genuine leaks for which no fix is available. Those get suppressed via IgnoreTopFunction, which works as long as that function is unique enough. Example output for the leak fixed in https://github.com/kubernetes/kubernetes/pull/115423: E0202 09:30:51.641841 74789 etcd.go:205] "EtcdMain goroutine check" err=< found unexpected goroutines: [Goroutine 4889 in state chan receive, with k8s.io/apimachinery/pkg/watch.(*Broadcaster).loop on top of the stack: goroutine 4889 [chan receive]: k8s.io/apimachinery/pkg/watch.(*Broadcaster).loop(0xc0076183c0) /nvme/gopath/src/k8s.io/kubernetes/vendor/k8s.io/apimachinery/pkg/watch/mux.go:268 +0x65 created by k8s.io/apimachinery/pkg/watch.NewBroadcaster /nvme/gopath/src/k8s.io/kubernetes/vendor/k8s.io/apimachinery/pkg/watch/mux.go:77 +0x116 > |
||
---|---|---|
.. | ||
internal/stack | ||
.gitignore | ||
CHANGELOG.md | ||
doc.go | ||
glide.yaml | ||
leaks.go | ||
LICENSE | ||
Makefile | ||
options.go | ||
README.md | ||
testmain.go | ||
tracestack_new.go |
goleak

Goroutine leak detector to help avoid Goroutine leaks.
Installation
You can use go get
to get the latest version:
go get -u go.uber.org/goleak
goleak
also supports semver releases.
Note that go-leak only supports the two most recent minor versions of Go.
Quick Start
To verify that there are no unexpected goroutines running at the end of a test:
func TestA(t *testing.T) {
defer goleak.VerifyNone(t)
// test logic here.
}
Instead of checking for leaks at the end of every test, goleak
can also be run
at the end of every test package by creating a TestMain
function for your
package:
func TestMain(m *testing.M) {
goleak.VerifyTestMain(m)
}
Determine Source of Package Leaks
When verifying leaks using TestMain
, the leak test is only run once after all tests
have been run. This is typically enough to ensure there's no goroutines leaked from
tests, but when there are leaks, it's hard to determine which test is causing them.
You can use the following bash script to determine the source of the failing test:
# Create a test binary which will be used to run each test individually
$ go test -c -o tests
# Run each test individually, printing "." for successful tests, or the test name
# for failing tests.
$ for test in $(go test -list . | grep -E "^(Test|Example)"); do ./tests -test.run "^$test\$" &>/dev/null && echo -n "." || echo -e "\n$test failed"; done
This will only print names of failing tests which can be investigated individually. E.g.,
.....
TestLeakyTest failed
.......
Stability
goleak is v1 and follows SemVer strictly.
No breaking changes will be made to exported APIs before 2.0.