Skip to content

moul/goleak

This branch is up to date with uber-go/goleak:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

4ff5fa6 · Nov 21, 2024

History

85 Commits
Oct 1, 2024
Jun 7, 2024
Nov 11, 2019
Jun 5, 2024
Oct 24, 2023
Jan 5, 2018
Oct 21, 2023
Nov 21, 2024
Mar 21, 2018
Oct 22, 2023
Feb 6, 2023
Oct 1, 2024
Oct 24, 2023
Oct 1, 2024
Oct 24, 2023
Sep 27, 2021
Oct 1, 2024
Oct 1, 2024
Oct 23, 2023

Repository files navigation

goleak GoDoc Build Status Coverage Status

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)
}

Note

For tests that use t.Parallel, goleak does not know how to distinguish a leaky goroutine from tests that have not finished running.

func TestA(t *testing.T) {
	tt := struct{
		name  	 string
		input 	 SomeType
		expected string
	}{
		// ...
	}

	for _, t := range tt {
		t.Run(t.name, func(t *testing.T) {
			t.Parallel() // <- goleak gets confused here!

			// ...
		}
	}
}

For such cases you should also defer to using goleak.VerifyTestMain as shown above.

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.

About

Goroutine leak detector

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 98.6%
  • Makefile 1.4%