Goroutine leak detector

Related tags

Utilities goleak
Overview

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. It is compatible with Go 1.5+.

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 "\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.

Issues
  • Support t.Parallel?

    Support t.Parallel?

    Hi, thanks for providing great package :)

    BTW, I saw goleak internal source and found below switch...case: https://github.com/uber-go/goleak/blob/18742541d022941d2ef0db05d30bc68f69e96bd9/options.go#L118

    But, I used t.Parallel func on my project testing. So, goleak suggested:

    found unexpected goroutines
    

    Is it the expected behavior? Or, Should I adding goleak.IgnoreTopFunction("testing.(*T).Parallel") to defer goleak.VerifyNoLeaks(t)? (and, is it correct use case?)

    opened by zchee 7
  • CVE-2020-14040: imports golang.org/x/tools > golang.org/x/net > golang.org/x/text

    CVE-2020-14040: imports golang.org/x/tools > golang.org/x/net > golang.org/x/text

    This PR imports an updated version of golang.org/x/tools. x/tools imports x/net which imports x/text which includes the bug outlined in CVE-2020-14040.

    With the update of x/net it also fixes CVE-2020-9283 in the golang.org/x/crypto package/module.

    opened by jimmystewpot 5
  • Add option to pass teardown function

    Add option to pass teardown function

    Hi!

    In some cases performing additional work after test execution are required. But when VerifyTestMain is used it is not possible to do this work before leaked goroutine caught. This may be related to goroutine shutdown, so it should be done inside VerifyTestMain after test run but before check.

    This PR adds option to pass custom teardown function.

    opened by vdimir 4
  • Fix flaky tests

    Fix flaky tests

    Some tests assume that stack.All() will give a stable set of goroutines: that any background goroutines from the test framework/previous tests have run till they're blocked.

    Add verification that stack.All() returns a "stable" set of stacks before running these tests.

    opened by prashantv 4
  • Upgrade stretchr/testify

    Upgrade stretchr/testify

    Hello,

    I am working at Manomano and we use gemnasium analyser to report securities issues.

    We use zap logger and goleak is a dependency of zap.logger. This little pr purpose you to update your yaml dependency to a fixed version. Detail report: | Severity | Unknown | | Identifier | | | URL | https://github.com/docker/cli/pull/2117 | | Scanner | Gemnasium | | Message | XML Entity Expansion in gopkg.in/yaml.v2 | | Package | gopkg.in/yaml.v2 v2.2.1 | | Solution | Upgrade to version 2.2.3 or above. | | File | go.sum |

    opened by ymohl-cl 3
  • Add option to ignore current goroutines

    Add option to ignore current goroutines

    This allows usage specific tests in big projects that recently started to use go-leak check.

    Signed-off-by: denis-tingajkin [email protected]

    Motivation

    https://github.com/uber-go/goleak/issues/48

    opened by denis-tingaikin 3
  • Goleak detects leaked goroutines from other tests

    Goleak detects leaked goroutines from other tests

    Description

    We've found an interesting thing during using goleak on our CI, that goleak can detect leaked goroutine from other tests.

    Steps to reproduce

    1. Use go1.14.1 darwin/amd64
    2. Use goleak v1.0.0
    3. Run test file below goleak_test.go
    package goleak_test
    
    import (
    	"go.uber.org/goleak"
    	"testing"
    	"time"
    )
    
    func TestA(t *testing.T) {
    	go func() {
    		for {
    			<-time.After(time.Millisecond * 100)
    		}
    	}()
    }
    
    func TestB(t *testing.T) {
    	defer goleak.VerifyNone(t)
    }
    

    Expected: TestB should pass Actual: TestB fails with error "found unexpected goroutines:"

    opened by denis-tingaikin 3
  • Using strings.HasSuffix so vendored projects can pass tests

    Using strings.HasSuffix so vendored projects can pass tests

    Vendored and non-vendored packages have different absolute paths but have the same ending.

    eg. github.com/onsi/ginkgo/internal/specrunner.(*SpecRunner).registerForInterrupts and github.com/koshatul/testproject/vendor/github.com/onsi/ginkgo/internal/specrunner.(*SpecRunner).registerForInterrupts

    opened by koshatul 3
  • Running tests with `-trace` is failing for new versions of go

    Running tests with `-trace` is failing for new versions of go

    Problem: Using goleak with trace flag in earlier versions of go (go1.13.10) is working fine due to fixed issue #39.

    But in recent version of go (go1.17.1) testing simple programs with trace flag is failing again. I think the stack trace has now hidden the call to runtime.goparkunlock

    func Test(t *testing.T) {
    	defer goleak.VerifyNone(t)
    }
    
    make test:
            [Goroutine 18 in state trace reader (blocked), with runtime.ReadTrace on top of the stack:
            goroutine 18 [trace reader (blocked)]:
            runtime.ReadTrace()
            	/usr/local/go/src/runtime/trace.go:412 +0xd2
            runtime/trace.Start.func1()
            	/usr/local/go/src/runtime/trace/trace.go:129 +0x47
            created by runtime/trace.Start
            	/usr/local/go/src/runtime/trace/trace.go:127 +0xf4
    

    Possible Solution: Removing this piece of code: https://github.com/uber-go/goleak/blob/1e9de543a7aac025a9cd43ec3805a432ea33600d/options.go#L159-L161

    opened by kapillamba4 2
  • CVE-2020-14040: imports golang.org/x/tools > golang.org/x/net > golan…

    CVE-2020-14040: imports golang.org/x/tools > golang.org/x/net > golan…

    This PR imports an updated version of golang.org/x/tools. x/tools imports x/net which imports x/text which includes the bug outlined in CVE-2020-14040.

    With the update of x/net it also fixes CVE-2020-9283 in the golang.org/x/crypto package/module.

    opened by jimmystewpot 2
  • Add Examples to failing tests check

    Add Examples to failing tests check

    Hi there, thanks for the amazing tool!

    I insert following code into one of my package test files:

    func TestMain(m *testing.M) {
    	goleak.VerifyTestMain(m)
    }
    

    Then on tests run:

    $ go test -count=1 ./...
    PASS
    goleak: Errors on successful test run: found unexpected goroutines:
    <...>
    

    I've tried the following command to identify the problem:

    # 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 "^Test"); do ./tests -test.run "^$test\$" &>/dev/null && echo -n "." || echo "\n$test failed"; done
    .....................................%      
    

    And here is behavior after applying the adjusted command from this PR:

    for test in $(go test -list . | grep -E "^(Test|Example)"); do ./tests -test.run "^$test\$" &>/dev/null && echo -n "." || echo "\n$test failed"; done
    .....................................
    ExampleLoadingCache_Get failed
    
    ExampleLoadingCache_Delete failed
    ..%                            
    
    opened by paskal 2
  • Provide stub so apps using goleak can build unchanged with tinygo.

    Provide stub so apps using goleak can build unchanged with tinygo.

    Tinygo can build a large subset of go programs already. Unfortunately, tinygo currently does not quite support goleak.

    As a temporary measure, use the tinygo build tag to provide a stub for goleak. This will expand the world of go programs that run properly on tinygo, and remove one more obstacle keeping embedded system and wasm users from adopting go and goleak.

    This can be reverted if/when tinygo is enhanced to fully support goleak.

    Fixes https://github.com/uber-go/goleak/issues/71

    opened by dkegel-fastly 6
  • Friction for tinygo users

    Friction for tinygo users

    tinygo as of release 0.22 does not support goleak; this isn't particularly surprising, as tinygo still only provides stubs for several things goleak relies upon.

    A number of apps - particularly, the one I'm trying to ship :-) - would pass tests under both go and tinygo if goleak sensed the tinygo build constraint and provided a minimal stub while tinygo matures.

    This would be an elegant way to reduce friction for goleak users who must pass tests with both go and tinygo.

    I'll provide a pull request with the simple change required. I think this is sufficient; works for my app, at least.

    opened by dkegel-fastly 0
  • Support tracebackancestors?

    Support tracebackancestors?

    I'd like to verify that there are no goroutines leaked in my test, but only for goroutines where my test itself is an ancestor goroutine. AFAIK, this is not what goleak currently does. It looks at all goroutines, which means it doesn't work in parallel, and it doesn't work for many globally initialized goroutines like those created in the init function of some imported package.

    There is a tracebackancestors option that can be passed to the GODEBUG environment variable (documented here, withe the original accepted proposal here. This option adds ancestry metadata to goroutines, which should make the functionality I'm looking for possible.

    Are there any plans to add this functionality to goleak?

    opened by MichaelSnowden 1
  • support register teardown function in VerifyTestMain

    support register teardown function in VerifyTestMain

    TestMain can be used for setup and teardown logic. However, VerifyTestMain calls os.Exit which disables to register teardown function by defer. cc @prashantv

    opened by tisonkun 6
  • Global way to ignore known leaks from a set of tests

    Global way to ignore known leaks from a set of tests

    Currently, every call to goleak.Verify* needs to specify any stacks to ignore. If there are known leaks which affect multiple packages, this results in duplication in each package specifying the same ignores.

    We should investigate approaches to make this easier, as a strawman proposal: Add AppendDefaultOptions as an exported function that allows appending a default set of ignores, that can be called in some init function, and can be shared for all tests by depending on the package that adds the default options.

    opened by prashantv 0
Releases(v1.1.12)
  • v1.1.12(Oct 4, 2021)

  • v1.1.11(Sep 8, 2021)

    Fixed

    • Documentation fix on how to test.
    • Update dependency on stretchr/testify to v1.7.0. (#59)
    • Update dependency on golang.org/x/tools to address CVE-2020-14040. (#62)
    Source code(tar.gz)
    Source code(zip)
  • v1.1.10(Jul 21, 2020)

    Added

    • [#49]: Add option to ignore current goroutines, which checks for any additional leaks and allows for incremental adoption of goleak in larger projects.
    Source code(tar.gz)
    Source code(zip)
  • v1.0.0(Jan 7, 2020)

Owner
Uber Go
Uber's open source software for Go development
Uber Go
:speedboat: a limited consumer goroutine or unlimited goroutine pool for easier goroutine handling and cancellation

Package pool Package pool implements a limited consumer goroutine or unlimited goroutine pool for easier goroutine handling and cancellation. Features

Go Playgound 692 Aug 12, 2022
🐜🐜🐜 ants is a high-performance and low-cost goroutine pool in Go, inspired by fasthttp./ ants 是一个高性能且低损耗的 goroutine 池。

A goroutine pool for Go English | ???? 中文 ?? Introduction Library ants implements a goroutine pool with fixed capacity, managing and recycling a massi

Andy Pan 8.9k Aug 17, 2022
Friends don't let friends leak secrets on their terminal window

senv - safer env Friends don't let friends leak secrets in terminal windows. ?? Print your environment to the terminal without worry.

null 103 Aug 4, 2022
Tracking down a Memory Leak in Go/SQLite

Tracking down a Memory Leak in Go/SQLite run make test - WARNING: long running - several minutes on my workstation OSs supported: Windows_NT => memory

Stefan Thiel 1 Feb 28, 2022
A toy deadlock detector written in Go.

Toy Deadlock Detector This package aims to provide a DSL to represent processes as finite state machines and their concurrent composition. A detector

Yuto Takahashi 27 Feb 2, 2022
Chronos - A static race detector for the go language

Chronos Chronos is a static race detector for the Go language written in Go. Quick Start: Download the package go get -v github.com/amit-davidson/Chro

Amit Davidson 369 Jul 26, 2022
A faster file programming language detector

Programming language detector and toolbox to ignore binary or vendored files. enry, started as a port to Go of the original Linguist Ruby library, that has an improved 2x performance.

go-enry 290 Aug 9, 2022
TProx is a fast reverse proxy path traversal detector and directory bruteforcer.

TProx is a fast reverse proxy path traversal detector and directory bruteforcer Install • Usage • Examples • Join Discord Install Options From Source

Krypt0mux 25 Aug 8, 2022
A detector for the Trojan Source and other unicode-based vulnerabilities.

Trojan Source Detector This application detects Trojan Source attacks in source code. It can be used as part of the CI system to make sure there are n

Have you debugged.it? 5 Jan 6, 2022
A Log4J Version 2 Detector written in golang

Installation From source: go install github.com/juergenhoetzel/log4j2go/cmd/log4

Jürgen Hötzel 1 Dec 20, 2021
Log4j detector and reporting server for scalable detection of vulnerable running processes.

Log4j Detector A client and reporting server to identify systems vulnerable to Log4j at scale. This work is based on Stripe's Remediation Tools, but w

Praetorian 8 Apr 8, 2022
User Agents detector for tv, phone, tablet and desktop devices.

gouseragents Accurate and fresh list of desktop, phone, tablet and tv user agents. install go get github.com/emetriq/gouseragents usage import ( "f

emetriq GmbH 2 Apr 26, 2022
Go concurrent-safe, goroutine-safe, thread-safe queue

goconcurrentqueue - Concurrent safe queues The package goconcurrentqueue offers a public interface Queue with methods for a queue. It comes with multi

Enrique Bris 200 Aug 9, 2022
🐝 A Highly Performant and easy to use goroutine pool for Go

gohive Package gohive implements a simple and easy to use goroutine pool for Go Features Pool can be created with a specific size as per the requireme

Lovelesh 41 Aug 11, 2022
golang worker pool , Concurrency limiting goroutine pool

golang worker pool 中文说明 Concurrency limiting goroutine pool. Limits the concurrency of task execution, not the number of tasks queued. Never blocks su

xxj 404 Aug 8, 2022
Lightweight Goroutine pool

grpool Lightweight Goroutine pool Clients can submit jobs. Dispatcher takes job, and sends it to first available worker. When worker is done with proc

Ivan Pusic 700 Aug 9, 2022
errgroup with goroutine worker limits

neilotoole/errgroup neilotoole/errgroup is a drop-in alternative to Go's wonderful sync/errgroup but limited to N goroutines. This is useful for inter

Neil O'Toole 131 Aug 14, 2022
Minimalistic and High-performance goroutine worker pool written in Go

pond Minimalistic and High-performance goroutine worker pool written in Go Motivation This library is meant to provide a simple way to limit concurren

Alejandro Durante 599 Aug 12, 2022
A goroutine pool for Go

Tunny is a Golang library for spawning and managing a goroutine pool, allowing you to limit work coming from any number of goroutines with a synchrono

Ashley Jeffs 3.2k Aug 14, 2022
Concurrency limiting goroutine pool

workerpool Concurrency limiting goroutine pool. Limits the concurrency of task execution, not the number of tasks queued. Never blocks submitting task

Andrew Gillis 890 Aug 16, 2022
:wink: :cyclone: :strawberry: TextRank implementation in Golang with extendable features (summarization, phrase extraction) and multithreading (goroutine) support (Go 1.8, 1.9, 1.10)

TextRank on Go This source code is an implementation of textrank algorithm, under MIT licence. The minimum requred Go version is 1.8. MOTIVATION If th

David Belicza 162 Aug 17, 2022
Waiting group for collecting goroutine information.

在go语言waitGroup和errGroup都是用来控制goroutine的并发的方式,前者只能等待所有goroutine执行完成之后再执行Wait()函数后面的代码并且不

Jarvib Ding 111 Dec 3, 2021
ChanBroker, a Broker for goroutine, is simliar to kafka

Introduction chanbroker, a Broker for goroutine, is simliar to kafka In chanbroker has three types of goroutine: Producer Consumer(Subscriber) Broker

沉风 61 Aug 12, 2021
A goroutine monitor to keep track of active routines from within your favorite shell.

roumon A goroutine monitor to keep track of active routines from within your favorite shell. Features Track live state of all active goroutines Termin

Armin Becher 89 Aug 18, 2022
Goroutine local storage

gls Goroutine local storage IMPORTANT NOTE It is my duty to point you to https://blog.golang.org/context, which is how Google solves all of the proble

JT Olio 585 Aug 15, 2022
A simple and useful goroutine concurrent library.

Taskgroup A simple and useful goroutine concurrent library. Installation go get github.com/anthhub/taskgroup

Tangqy 4 May 19, 2021
Provides some convenient API, includes Goid(), AllGoid(), and LocalStorage, which is a goroutine's local storage, just like ThreadLocal in other languages.

routine 中文版 routine encapsulates and provides some easy-to-use, high-performance goroutine context access interfaces, which can help you access corout

null 62 Aug 7, 2022
gogo is a language with a gogoroutine on goroutine

gogo based on the go lang branch dev.go2go 修改 ast 支持 gogo 关键字 修改 checker(types/stmt.go) Example package main import ( "fmt" "time" ) func main()

Peng Gao 5 Oct 30, 2021
cpuworker - A Customized Goroutine Scheduler over Golang Runtime

cpuworker Status Working in process. Run the Demo Make sure the GOMAXPROCS is bigger than 1 and there is at least GOMAXPROCS physical OS threads avail

Sen Han 119 Aug 4, 2022