pax_global_header00006660000000000000000000000064141266763440014527gustar00rootroot0000000000000052 comment=421b885faf413a221d1a86a17684fb3c45bd35c5 goleak-1.1.12/000077500000000000000000000000001412667634400130535ustar00rootroot00000000000000goleak-1.1.12/.github/000077500000000000000000000000001412667634400144135ustar00rootroot00000000000000goleak-1.1.12/.github/workflows/000077500000000000000000000000001412667634400164505ustar00rootroot00000000000000goleak-1.1.12/.github/workflows/fossa.yaml000066400000000000000000000005021412667634400204440ustar00rootroot00000000000000name: FOSSA Analysis on: push jobs: build: runs-on: ubuntu-latest if: github.repository_owner == 'uber-go' steps: - name: Checkout code uses: actions/checkout@v2 - name: FOSSA analysis uses: fossas/fossa-action@v1 with: api-key: ${{ secrets.FOSSA_API_KEY }} goleak-1.1.12/.github/workflows/go.yml000066400000000000000000000016461412667634400176070ustar00rootroot00000000000000name: Go on: push: branches: ['*'] tags: ['v*'] pull_request: branches: ['*'] jobs: build: runs-on: ubuntu-latest strategy: matrix: go: ["1.14.x", "1.15.x", "1.16.x", "1.17.x"] include: - go: 1.17.x latest: true steps: - name: Setup Go uses: actions/setup-go@v2 with: go-version: ${{ matrix.go }} - name: Checkout code uses: actions/checkout@v2 - name: Load cached dependencies uses: actions/cache@v1 with: path: ~/go/pkg/mod key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} restore-keys: | ${{ runner.os }}-go- - name: Download Dependencies run: go mod download - name: Lint if: matrix.latest run: make lint - name: Test run: make cover - name: Upload coverage to codecov.io uses: codecov/codecov-action@v1 goleak-1.1.12/.gitignore000066400000000000000000000000561412667634400150440ustar00rootroot00000000000000vendor/ /bin /lint.log /cover.out /cover.html goleak-1.1.12/CHANGELOG.md000066400000000000000000000020611412667634400146630ustar00rootroot00000000000000# Changelog All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## [1.1.12] ### Fixed - Fixed logic for ignoring trace related goroutines on Go versions 1.16 and above. ## [1.1.11] ### 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) ## [1.1.10] ### Added - [#49]: Add option to ignore current goroutines, which checks for any additional leaks and allows for incremental adoption of goleak in larger projects. Thanks to @denis-tingajkin for their contributions to this release. ## [1.0.0] ### Changed - Migrate to Go modules. ### Fixed - Ignore trace related goroutines that cause false positives with -trace. ## 0.10.0 - Initial release. [1.0.0]: https://github.com/uber-go/goleak/compare/v0.10.0...v1.0.0 [#49]: https://github.com/uber-go/goleak/pull/49 goleak-1.1.12/LICENSE000066400000000000000000000021021412667634400140530ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2018 Uber Technologies, Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. goleak-1.1.12/Makefile000066400000000000000000000016111412667634400145120ustar00rootroot00000000000000export GOBIN ?= $(shell pwd)/bin GOLINT = $(GOBIN)/golint GO_FILES := $(shell \ find . '(' -path '*/.*' -o -path './vendor' ')' -prune \ -o -name '*.go' -print | cut -b3-) .PHONY: build build: go build ./... .PHONY: install install: go mod download .PHONY: test test: go test -v -race ./... go test -v -trace=/dev/null . .PHONY: cover cover: go test -race -coverprofile=cover.out -coverpkg=./... ./... go tool cover -html=cover.out -o cover.html $(GOLINT): go install golang.org/x/lint/golint .PHONY: lint lint: $(GOLINT) @rm -rf lint.log @echo "Checking formatting..." @gofmt -d -s $(GO_FILES) 2>&1 | tee lint.log @echo "Checking vet..." @go vet ./... 2>&1 | tee -a lint.log @echo "Checking lint..." @$(GOLINT) ./... 2>&1 | tee -a lint.log @echo "Checking for unresolved FIXMEs..." @git grep -i fixme | grep -v -e '^vendor/' -e '^Makefile' | tee -a lint.log @[ ! -s lint.log ] goleak-1.1.12/README.md000066400000000000000000000041301412667634400143300ustar00rootroot00000000000000# goleak [![GoDoc][doc-img]][doc] [![Build Status][ci-img]][ci] [![Coverage Status][cov-img]][cov] 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: ```go 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: ```go 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: ```sh # 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](http://semver.org/) strictly. No breaking changes will be made to exported APIs before 2.0. [doc-img]: https://godoc.org/go.uber.org/goleak?status.svg [doc]: https://godoc.org/go.uber.org/goleak [ci-img]: https://github.com/uber-go/goleak/actions/workflows/go.yml/badge.svg [ci]: https://github.com/uber-go/goleak/actions/workflows/go.yml [cov-img]: https://codecov.io/gh/uber-go/goleak/branch/master/graph/badge.svg [cov]: https://codecov.io/gh/uber-go/goleak goleak-1.1.12/doc.go000066400000000000000000000022761412667634400141560ustar00rootroot00000000000000// Copyright (c) 2018 Uber Technologies, Inc. // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // Package goleak is a Goroutine leak detector. package goleak // import "go.uber.org/goleak" goleak-1.1.12/glide.yaml000066400000000000000000000002221412667634400150170ustar00rootroot00000000000000package: go.uber.org/goleak import: [] testImport: - package: github.com/stretchr/testify version: ^1.1.4 subpackages: - assert - require goleak-1.1.12/go.mod000066400000000000000000000004341412667634400141620ustar00rootroot00000000000000module go.uber.org/goleak go 1.13 require ( github.com/kr/pretty v0.1.0 // indirect github.com/stretchr/testify v1.7.0 golang.org/x/lint v0.0.0-20190930215403-16217165b5de golang.org/x/tools v0.1.5 // indirect gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect ) goleak-1.1.12/go.sum000066400000000000000000000110631412667634400142070ustar00rootroot00000000000000github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.5 h1:ouewzE6p+/VEB31YYnTbEJdi8pFqKp4P4n85vwo3DHA= golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= goleak-1.1.12/internal/000077500000000000000000000000001412667634400146675ustar00rootroot00000000000000goleak-1.1.12/internal/stack/000077500000000000000000000000001412667634400157745ustar00rootroot00000000000000goleak-1.1.12/internal/stack/stacks.go000066400000000000000000000101741412667634400176160ustar00rootroot00000000000000// Copyright (c) 2017 Uber Technologies, Inc. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. package stack import ( "bufio" "bytes" "fmt" "io" "runtime" "strconv" "strings" ) const _defaultBufferSize = 64 * 1024 // 64 KiB // Stack represents a single Goroutine's stack. type Stack struct { id int state string firstFunction string fullStack *bytes.Buffer } // ID returns the goroutine ID. func (s Stack) ID() int { return s.id } // State returns the Goroutine's state. func (s Stack) State() string { return s.state } // Full returns the full stack trace for this goroutine. func (s Stack) Full() string { return s.fullStack.String() } // FirstFunction returns the name of the first function on the stack. func (s Stack) FirstFunction() string { return s.firstFunction } func (s Stack) String() string { return fmt.Sprintf( "Goroutine %v in state %v, with %v on top of the stack:\n%s", s.id, s.state, s.firstFunction, s.Full()) } func getStacks(all bool) []Stack { var stacks []Stack var curStack *Stack stackReader := bufio.NewReader(bytes.NewReader(getStackBuffer(all))) for { line, err := stackReader.ReadString('\n') if err == io.EOF { break } if err != nil { // We're reading using bytes.NewReader which should never fail. panic("bufio.NewReader failed on a fixed string") } // If we see the goroutine header, start a new stack. isFirstLine := false if strings.HasPrefix(line, "goroutine ") { // flush any previous stack if curStack != nil { stacks = append(stacks, *curStack) } id, goState := parseGoStackHeader(line) curStack = &Stack{ id: id, state: goState, fullStack: &bytes.Buffer{}, } isFirstLine = true } curStack.fullStack.WriteString(line) if !isFirstLine && curStack.firstFunction == "" { curStack.firstFunction = parseFirstFunc(line) } } if curStack != nil { stacks = append(stacks, *curStack) } return stacks } // All returns the stacks for all running goroutines. func All() []Stack { return getStacks(true) } // Current returns the stack for the current goroutine. func Current() Stack { return getStacks(false)[0] } func getStackBuffer(all bool) []byte { for i := _defaultBufferSize; ; i *= 2 { buf := make([]byte, i) if n := runtime.Stack(buf, all); n < i { return buf[:n] } } } func parseFirstFunc(line string) string { line = strings.TrimSpace(line) if idx := strings.LastIndex(line, "("); idx > 0 { return line[:idx] } panic(fmt.Sprintf("function calls missing parents: %q", line)) } // parseGoStackHeader parses a stack header that looks like: // goroutine 643 [runnable]:\n // And returns the goroutine ID, and the state. func parseGoStackHeader(line string) (goroutineID int, state string) { line = strings.TrimSuffix(line, ":\n") parts := strings.SplitN(line, " ", 3) if len(parts) != 3 { panic(fmt.Sprintf("unexpected stack header format: %q", line)) } id, err := strconv.Atoi(parts[1]) if err != nil { panic(fmt.Sprintf("failed to parse goroutine ID: %v in line %q", parts[1], line)) } state = strings.TrimSuffix(strings.TrimPrefix(parts[2], "["), "]") return id, state } goleak-1.1.12/internal/stack/stacks_test.go000066400000000000000000000100201412667634400206430ustar00rootroot00000000000000// Copyright (c) 2017 Uber Technologies, Inc. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. package stack import ( "runtime" "sort" "strings" "sync" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) var _allDone chan struct{} func waitForDone() { <-_allDone } func TestAll(t *testing.T) { // We use a global channel so that the function below does not // receive any arguments, so we can test that parseFirstFunc works // regardless of arguments on the stack. _allDone = make(chan struct{}) defer close(_allDone) for i := 0; i < 5; i++ { go waitForDone() } cur := Current() got := All() // Retry until the background stacks are not runnable/running. for { if !isBackgroundRunning(cur, got) { break } runtime.Gosched() got = All() } // We have exactly 7 gorotuines: // "main" goroutine // test goroutine // 5 goroutines started above. require.Len(t, got, 7) sort.Sort(byGoroutineID(got)) assert.Contains(t, got[0].Full(), "testing.(*T).Run") assert.Contains(t, got[1].Full(), "TestAll") for i := 0; i < 5; i++ { assert.Contains(t, got[2+i].Full(), "stack.waitForDone") } } func TestCurrent(t *testing.T) { got := Current() assert.NotZero(t, got.ID(), "Should get non-zero goroutine id") assert.Equal(t, "running", got.State()) assert.Equal(t, "go.uber.org/goleak/internal/stack.getStackBuffer", got.FirstFunction()) wantFrames := []string{ "stack.getStackBuffer", "stack.getStacks", "stack.Current", "stack.Current", "stack.TestCurrent", } all := got.Full() for _, frame := range wantFrames { assert.Contains(t, all, frame) } assert.Contains(t, got.String(), "in state") assert.Contains(t, got.String(), "on top of the stack") // Ensure that we are not returning the buffer without slicing it // from getStackBuffer. if len(got.Full()) > 1024 { t.Fatalf("Returned stack is too large") } } func TestAllLargeStack(t *testing.T) { const ( stackDepth = 100 numGoroutines = 100 ) var started sync.WaitGroup done := make(chan struct{}) for i := 0; i < numGoroutines; i++ { var f func(int) f = func(count int) { if count == 0 { started.Done() <-done return } f(count - 1) } started.Add(1) go f(stackDepth) } started.Wait() buf := getStackBuffer(true /* all */) if len(buf) <= _defaultBufferSize { t.Fatalf("Expected larger stack buffer") } // Start enough goroutines so we exceed the default buffer size. close(done) } type byGoroutineID []Stack func (ss byGoroutineID) Len() int { return len(ss) } func (ss byGoroutineID) Less(i, j int) bool { return ss[i].ID() < ss[j].ID() } func (ss byGoroutineID) Swap(i, j int) { ss[i], ss[j] = ss[j], ss[i] } // Note: This is the same logic as in ../../utils_test.go // Copy+pasted to avoid dependency loops and exporting this test-helper. func isBackgroundRunning(cur Stack, stacks []Stack) bool { for _, s := range stacks { if cur.ID() == s.ID() { continue } if strings.Contains(s.State(), "run") { return true } } return false } goleak-1.1.12/leaks.go000066400000000000000000000047561412667634400145150ustar00rootroot00000000000000// Copyright (c) 2017 Uber Technologies, Inc. // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. package goleak import ( "fmt" "go.uber.org/goleak/internal/stack" ) // TestingT is the minimal subset of testing.TB that we use. type TestingT interface { Error(...interface{}) } // filterStacks will filter any stacks excluded by the given opts. // filterStacks modifies the passed in stacks slice. func filterStacks(stacks []stack.Stack, skipID int, opts *opts) []stack.Stack { filtered := stacks[:0] for _, stack := range stacks { // Always skip the running goroutine. if stack.ID() == skipID { continue } // Run any default or user-specified filters. if opts.filter(stack) { continue } filtered = append(filtered, stack) } return filtered } // Find looks for extra goroutines, and returns a descriptive error if // any are found. func Find(options ...Option) error { cur := stack.Current().ID() opts := buildOpts(options...) var stacks []stack.Stack retry := true for i := 0; retry; i++ { stacks = filterStacks(stack.All(), cur, opts) if len(stacks) == 0 { return nil } retry = opts.retry(i) } return fmt.Errorf("found unexpected goroutines:\n%s", stacks) } // VerifyNone marks the given TestingT as failed if any extra goroutines are // found by Find. This is a helper method to make it easier to integrate in // tests by doing: // defer VerifyNone(t) func VerifyNone(t TestingT, options ...Option) { if err := Find(options...); err != nil { t.Error(err) } } goleak-1.1.12/leaks_test.go000066400000000000000000000107331412667634400155440ustar00rootroot00000000000000// Copyright (c) 2017 Uber Technologies, Inc. // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. package goleak import ( "fmt" "sync" "testing" "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) // Ensure that testingT is a subset of testing.TB. var _ = TestingT(testing.TB(nil)) // testOptions passes a shorter max sleep time, used so tests don't wait // ~1 second in cases where we expect Find to error out. func testOptions() Option { return maxSleep(time.Millisecond) } func TestFind(t *testing.T) { require.NoError(t, Find(), "Should find no leaks by default") bg := startBlockedG() err := Find(testOptions()) require.Error(t, err, "Should find leaks with leaked goroutine") assert.Contains(t, err.Error(), "blockedG") assert.Contains(t, err.Error(), "created by go.uber.org/goleak.startBlockedG") // Once we unblock the goroutine, we shouldn't have leaks. bg.unblock() require.NoError(t, Find(), "Should find no leaks by default") } func TestFindRetry(t *testing.T) { // for i := 0; i < 10; i++ { bg := startBlockedG() require.Error(t, Find(testOptions()), "Should find leaks with leaked goroutine") go func() { time.Sleep(time.Millisecond) bg.unblock() }() require.NoError(t, Find(), "Find should retry while background goroutine ends") } type fakeT struct { errors []string } func (ft *fakeT) Error(args ...interface{}) { ft.errors = append(ft.errors, fmt.Sprint(args...)) } func TestVerifyNone(t *testing.T) { ft := &fakeT{} VerifyNone(ft) require.Empty(t, ft.errors, "Expect no errors from VerifyNone") bg := startBlockedG() VerifyNone(ft, testOptions()) require.NotEmpty(t, ft.errors, "Expect errors from VerifyNone on leaked goroutine") bg.unblock() } func TestIgnoreCurrent(t *testing.T) { t.Run("Should ignore current", func(t *testing.T) { defer VerifyNone(t) done := make(chan struct{}) go func() { <-done }() // We expect the above goroutine to be ignored. VerifyNone(t, IgnoreCurrent()) close(done) }) t.Run("Should detect new leaks", func(t *testing.T) { defer VerifyNone(t) // There are no leaks currently. VerifyNone(t) done1 := make(chan struct{}) done2 := make(chan struct{}) go func() { <-done1 }() err := Find() require.Error(t, err, "Expected to find background goroutine as leak") opt := IgnoreCurrent() VerifyNone(t, opt) // A second goroutine started after IgnoreCurrent is a leak go func() { <-done2 }() err = Find(opt) require.Error(t, err, "Expect second goroutine to be flagged as a leak") close(done1) close(done2) }) t.Run("Should not ignore false positive", func(t *testing.T) { defer VerifyNone(t) const goroutinesCount = 5 var wg sync.WaitGroup done := make(chan struct{}) // Spawn few goroutines before checking leaks for i := 0; i < goroutinesCount; i++ { wg.Add(1) go func() { <-done wg.Done() }() } // Store all goroutines option := IgnoreCurrent() // Free goroutines close(done) wg.Wait() // We expect the below goroutines to be founded. for i := 0; i < goroutinesCount; i++ { ch := make(chan struct{}) go func() { <-ch }() require.Error(t, Find(option), "Expect spawned goroutine to be flagged as a leak") // Free spawned goroutine close(ch) // Make sure that there are no leaks VerifyNone(t) } }) } func TestVerifyParallel(t *testing.T) { t.Run("parallel", func(t *testing.T) { t.Parallel() }) t.Run("serial", func(t *testing.T) { VerifyNone(t) }) } goleak-1.1.12/options.go000066400000000000000000000112701412667634400150760ustar00rootroot00000000000000// Copyright (c) 2017 Uber Technologies, Inc. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. package goleak import ( "strings" "time" "go.uber.org/goleak/internal/stack" ) // Option lets users specify custom verifications. type Option interface { apply(*opts) } // We retry up to 20 times if we can't find the goroutine that // we are looking for. In between each attempt, we will sleep for // a short while to let any running goroutines complete. const _defaultRetries = 20 type opts struct { filters []func(stack.Stack) bool maxRetries int maxSleep time.Duration } // optionFunc lets us easily write options without a custom type. type optionFunc func(*opts) func (f optionFunc) apply(opts *opts) { f(opts) } // IgnoreTopFunction ignores any goroutines where the specified function // is at the top of the stack. The function name should be fully qualified, // e.g., go.uber.org/goleak.IgnoreTopFunction func IgnoreTopFunction(f string) Option { return addFilter(func(s stack.Stack) bool { return s.FirstFunction() == f }) } // IgnoreCurrent records all current goroutines when the option is created, and ignores // them in any future Find/Verify calls. func IgnoreCurrent() Option { excludeIDSet := map[int]bool{} for _, s := range stack.All() { excludeIDSet[s.ID()] = true } return addFilter(func(s stack.Stack) bool { return excludeIDSet[s.ID()] }) } func maxSleep(d time.Duration) Option { return optionFunc(func(opts *opts) { opts.maxSleep = d }) } func addFilter(f func(stack.Stack) bool) Option { return optionFunc(func(opts *opts) { opts.filters = append(opts.filters, f) }) } func buildOpts(options ...Option) *opts { opts := &opts{ maxRetries: _defaultRetries, maxSleep: 100 * time.Millisecond, } opts.filters = append(opts.filters, isTestStack, isSyscallStack, isStdLibStack, isTraceStack, ) for _, option := range options { option.apply(opts) } return opts } func (vo *opts) filter(s stack.Stack) bool { for _, filter := range vo.filters { if filter(s) { return true } } return false } func (vo *opts) retry(i int) bool { if i >= vo.maxRetries { return false } d := time.Duration(int(time.Microsecond) << uint(i)) if d > vo.maxSleep { d = vo.maxSleep } time.Sleep(d) return true } // isTestStack is a default filter installed to automatically skip goroutines // that the testing package runs while the user's tests are running. func isTestStack(s stack.Stack) bool { // Until go1.7, the main goroutine ran RunTests, which started // the test in a separate goroutine and waited for that test goroutine // to end by waiting on a channel. // Since go1.7, a separate goroutine is started to wait for signals. // T.Parallel is for parallel tests, which are blocked until all serial // tests have run with T.Parallel at the top of the stack. switch s.FirstFunction() { case "testing.RunTests", "testing.(*T).Run", "testing.(*T).Parallel": // In pre1.7 and post-1.7, background goroutines started by the testing // package are blocked waiting on a channel. return strings.HasPrefix(s.State(), "chan receive") } return false } func isSyscallStack(s stack.Stack) bool { // Typically runs in the background when code uses CGo: // https://github.com/golang/go/issues/16714 return s.FirstFunction() == "runtime.goexit" && strings.HasPrefix(s.State(), "syscall") } func isStdLibStack(s stack.Stack) bool { // Importing os/signal starts a background goroutine. // The name of the function at the top has changed between versions. if f := s.FirstFunction(); f == "os/signal.signal_recv" || f == "os/signal.loop" { return true } // Using signal.Notify will start a runtime goroutine. return strings.Contains(s.Full(), "runtime.ensureSigM") } goleak-1.1.12/options_test.go000066400000000000000000000053551412667634400161440ustar00rootroot00000000000000// Copyright (c) 2017 Uber Technologies, Inc. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. package goleak import ( "testing" "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.uber.org/goleak/internal/stack" ) func TestOptionsFilters(t *testing.T) { opts := buildOpts() cur := stack.Current() all := getStableAll(t, cur) // At least one of these should be the same as current, the others should be filtered out. for _, s := range all { if s.ID() == cur.ID() { require.False(t, opts.filter(s), "Current test running function should not be filtered") } else { require.True(t, opts.filter(s), "Default goroutines should be filtered: %v", s) } } defer startBlockedG().unblock() // Now the filters should find something that doesn't match a filter. countUnfiltered := func() int { var unmatched int for _, s := range stack.All() { if s.ID() == cur.ID() { continue } if !opts.filter(s) { unmatched++ } } return unmatched } require.Equal(t, 1, countUnfiltered(), "Expected blockedG goroutine to not match any filter") // If we add an extra filter to ignore blockTill, it shouldn't match. opts = buildOpts(IgnoreTopFunction("go.uber.org/goleak.(*blockedG).run")) require.Zero(t, countUnfiltered(), "blockedG should be filtered out. running: %v", stack.All()) } func TestOptionsRetry(t *testing.T) { opts := buildOpts() opts.maxRetries = 50 // initial attempt + 50 retries = 11 opts.maxSleep = time.Millisecond for i := 0; i < 50; i++ { assert.True(t, opts.retry(i), "Attempt %v/51 should allow retrying", i) } assert.False(t, opts.retry(51), "Attempt 51/51 should not allow retrying") assert.False(t, opts.retry(52), "Attempt 52/51 should not allow retrying") } goleak-1.1.12/signal_test.go000066400000000000000000000034241412667634400157210ustar00rootroot00000000000000// Copyright (c) 2017 Uber Technologies, Inc. // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. package goleak_test // Importing the os/signal package causes a goroutine to be started. import ( "os" "os/signal" "testing" "github.com/stretchr/testify/require" "go.uber.org/goleak" ) func TestNoLeaks(t *testing.T) { // Just importing the package can cause leaks. require.NoError(t, goleak.Find(), "Found leaks caused by signal import") // Register some signal handlers and ensure there's no leaks. c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) require.NoError(t, goleak.Find(), "Found leaks caused by signal.Notify") // Restore all registered signals. signal.Reset(os.Interrupt) require.NoError(t, goleak.Find(), "Found leaks caused after signal.Reset") } goleak-1.1.12/testmain.go000066400000000000000000000041001412667634400152210ustar00rootroot00000000000000// Copyright (c) 2017 Uber Technologies, Inc. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. package goleak import ( "fmt" "io" "os" ) // Variables for stubbing in unit tests. var ( _osExit = os.Exit _osStderr io.Writer = os.Stderr ) // TestingM is the minimal subset of testing.M that we use. type TestingM interface { Run() int } // VerifyTestMain can be used in a TestMain function for package tests to // verify that there were no goroutine leaks. // To use it, your TestMain function should look like: // // func TestMain(m *testing.M) { // goleak.VerifyTestMain(m) // } // // See https://golang.org/pkg/testing/#hdr-Main for more details. // // This will run all tests as per normal, and if they were successful, look // for any goroutine leaks and fail the tests if any leaks were found. func VerifyTestMain(m TestingM, options ...Option) { exitCode := m.Run() if exitCode == 0 { if err := Find(options...); err != nil { fmt.Fprintf(_osStderr, "goleak: Errors on successful test run: %v\n", err) exitCode = 1 } } _osExit(exitCode) } goleak-1.1.12/testmain_test.go000066400000000000000000000046761412667634400163020ustar00rootroot00000000000000// Copyright (c) 2017 Uber Technologies, Inc. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. package goleak import ( "bytes" "testing" "github.com/stretchr/testify/assert" ) func init() { clearOSStubs() } func clearOSStubs() { // We don't want to use the real os.Exit or os.Stderr so nil them out. // Tests MUST set them explicitly if they rely on them. _osExit = nil _osStderr = nil } type dummyTestMain int func (d dummyTestMain) Run() int { return int(d) } func osStubs() (chan int, chan string) { exitCode := make(chan int, 1) stderr := make(chan string, 1) buf := &bytes.Buffer{} _osStderr = buf _osExit = func(code int) { exitCode <- code stderr <- buf.String() buf.Reset() } return exitCode, stderr } func TestVerifyTestMain(t *testing.T) { defer clearOSStubs() exitCode, stderr := osStubs() blocked := startBlockedG() VerifyTestMain(dummyTestMain(7)) assert.Equal(t, 7, <-exitCode, "Exit code should not be modified") assert.NotContains(t, <-stderr, "goleak: Errors", "Ignore leaks on unsuccessful runs") VerifyTestMain(dummyTestMain(0)) assert.Equal(t, 1, <-exitCode, "Expect error due to leaks on successful runs") assert.Contains(t, <-stderr, "goleak: Errors", "Find leaks on successful runs") blocked.unblock() VerifyTestMain(dummyTestMain(0)) assert.Equal(t, 0, <-exitCode, "Expect no errors without leaks") assert.NotContains(t, <-stderr, "goleak: Errors", "No errors on successful run without leaks") } goleak-1.1.12/tools_test.go000066400000000000000000000023421412667634400156020ustar00rootroot00000000000000// Copyright (c) 2019 Uber Technologies, Inc. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. //go:build tools // +build tools package goleak import ( // Tools we use during development. _ "golang.org/x/lint/golint" ) goleak-1.1.12/tracestack_new.go000066400000000000000000000024651412667634400164060ustar00rootroot00000000000000// Copyright (c) 2021 Uber Technologies, Inc. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. //go:build go1.16 // +build go1.16 package goleak import ( "strings" "go.uber.org/goleak/internal/stack" ) func isTraceStack(s stack.Stack) bool { return strings.Contains(s.Full(), "runtime.ReadTrace") } goleak-1.1.12/tracestack_old.go000066400000000000000000000026041412667634400163660ustar00rootroot00000000000000// Copyright (c) 2021 Uber Technologies, Inc. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. //go:build !go1.16 // +build !go1.16 package goleak import ( "strings" "go.uber.org/goleak/internal/stack" ) func isTraceStack(s stack.Stack) bool { if f := s.FirstFunction(); f != "runtime.goparkunlock" { return false } return strings.Contains(s.Full(), "runtime.ReadTrace") } goleak-1.1.12/utils_test.go000066400000000000000000000045301412667634400156030ustar00rootroot00000000000000// Copyright (c) 2017 Uber Technologies, Inc. // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. package goleak import ( "runtime" "strings" "testing" "go.uber.org/goleak/internal/stack" ) type blockedG struct { started chan struct{} wait chan struct{} } func startBlockedG() *blockedG { bg := &blockedG{ started: make(chan struct{}), wait: make(chan struct{}), } go bg.run() <-bg.started return bg } func (bg *blockedG) run() { close(bg.started) <-bg.wait } func (bg *blockedG) unblock() { close(bg.wait) } func getStableAll(t *testing.T, cur stack.Stack) []stack.Stack { all := stack.All() // There may be running goroutines that were just scheduled or finishing up // from previous tests, so reduce flakiness by waiting till no other goroutines // are runnable or running except the current goroutine. for retry := 0; true; retry++ { if !isBackgroundRunning(cur, all) { break } if retry >= 100 { t.Fatalf("background goroutines are possibly running, %v", all) } runtime.Gosched() all = stack.All() } return all } // Note: This is the same logic as in internal/stacks/stacks_test.go func isBackgroundRunning(cur stack.Stack, stacks []stack.Stack) bool { for _, s := range stacks { if cur.ID() == s.ID() { continue } if strings.Contains(s.State(), "run") { return true } } return false }