{"id":22618921,"url":"https://github.com/0xvbetsun/goleak-example","last_synced_at":"2025-10-08T04:09:04.797Z","repository":{"id":61627269,"uuid":"546237448","full_name":"0xvbetsun/goleak-example","owner":"0xvbetsun","description":"Example project which represents how to use goleak module","archived":false,"fork":false,"pushed_at":"2023-02-14T14:06:47.000Z","size":11,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-27T06:12:21.537Z","etag":null,"topics":["example","go","golang","goroutine","leak"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/0xvbetsun.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2022-10-05T19:05:22.000Z","updated_at":"2024-02-13T19:54:10.000Z","dependencies_parsed_at":"2023-07-18T11:51:05.927Z","dependency_job_id":null,"html_url":"https://github.com/0xvbetsun/goleak-example","commit_stats":null,"previous_names":["0xvbetsun/goleak-example","vbetsun/goleak-example"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0xvbetsun%2Fgoleak-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0xvbetsun%2Fgoleak-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0xvbetsun%2Fgoleak-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0xvbetsun%2Fgoleak-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/0xvbetsun","download_url":"https://codeload.github.com/0xvbetsun/goleak-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246125020,"owners_count":20727359,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["example","go","golang","goroutine","leak"],"created_at":"2024-12-08T21:12:04.523Z","updated_at":"2025-10-08T04:08:59.763Z","avatar_url":"https://github.com/0xvbetsun.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# goleak-example [![GoDoc][doc-img]][doc] [![Build Status][ci-img]][ci] [![GoReport][report-img]][report] [![Coverage Status][cov-img]][cov] [![GitHub go.mod Go version of a Go module][version-img]][version]\n\nExample project which represents how to use `goleak` module to avoid Goroutine leaks.\n\n## Prolog\n\nConcurrency in Go materializes itself in the form of goroutines (independent activities) and channels (used for communication). While dealing with goroutines programmer needs to be careful to avoid their leakage\n\n### Primary reasons of leakage:\n\n1. The goroutine is waiting to read from a channel and the data never arrives.\n2. The goroutine tries to write into a channel but blocked as the existing data is never read (buffered channel).\n\n## Step 1. The Problem\n\nPlease checkout to the `problem` branch and explore `main.go` and `main_test.go`.\nAfter running tests `make test` and `make cover` you can recognize that tests are passing and you have 100% of test coverage.\nWe are running in **False Negative** test situation.\n\n```sh\n$ git checkout problem\n\n$ make test\n\ngo test -v -race ./...\n=== RUN   Test_main\n--- PASS: Test_main (2.00s)\nPASS\nok      github.com/vbetsun/goleak-example       2.446s\n\n$ make cover\n\ngo test -race -coverprofile=cover.out -coverpkg=./... ./...\nok      github.com/vbetsun/goleak-example       2.404s  coverage: 100.0% of statements in ./...\ngo tool cover -html=cover.out -o cover.html\n```\n\n## Step 2. Detecting\n\nNext, checkout to the `detecting` branch \n\n```sh\n$ git checkout detecting\n```\n\nAt this step we have added `go.uber.org/goleak` package\n\n```diff\nimport (\n\t\"bytes\"\n\t\"encoding/json\"\n\t\"io\"\n\t\"os\"\n\t\"sync\"\n\t\"testing\"\n+\n+\t\"go.uber.org/goleak\"\n)\n\n+func TestMain(m *testing.M) {\n+\tgoleak.VerifyTestMain(m)\n+}\n+\n...\n```\nAnd after running test one more time - we have fixed our **False Negative** tests\n\n```sh\n$ make test\n\ngo test -v -race ./...\n=== RUN   Test_main\n--- PASS: Test_main (2.00s)\nPASS\ngoleak: Errors on successful test run: found unexpected goroutines:\n[Goroutine 7 in state chan send, with github.com/vbetsun/goleak-example.main.func1 on top of the stack:\ngoroutine 7 [chan send]:\ngithub.com/vbetsun/goleak-example.main.func1(0x1)\n        /goleak-example/main.go:15 +0x53\ncreated by github.com/vbetsun/goleak-example.main\n        /goleak-example/main.go:14 +0x85\n\n Goroutine 8 in state chan send, with github.com/vbetsun/goleak-example.main.func1 on top of the stack:\ngoroutine 8 [chan send]:\ngithub.com/vbetsun/goleak-example.main.func1(0x2)\n        /goleak-example/main.go:15 +0x53\ncreated by github.com/vbetsun/goleak-example.main\n        /goleak-example/main.go:14 +0x85\n]\nFAIL    github.com/vbetsun/goleak-example       2.837s\nFAIL\nmake: *** [test] Error 1\n```\nSo, now we have clear understanding that we have a goroutine leak and we are already able to fix it!\n\n## Step 3. Solution\n\nNext, checkout to the `solution` branch \nWe are going to add `sync.WaitGroup` for synchronize all goroutines\n\nNow\u003c after running tests and coverage we will receive correct results\n\n```sh\n$ make test\n\ngo test -v -race ./...\n=== RUN   Test_main\n--- PASS: Test_main (2.00s)\nPASS\nok      github.com/vbetsun/goleak-example \n\n$ make cover\n\ngo test -race -coverprofile=cover.out -coverpkg=./... ./...\nok      github.com/vbetsun/goleak-example       2.407s  coverage: 100.0% of statements in ./...\ngo tool cover -html=cover.out -o cover.html\n```\n\n[doc-img]: https://pkg.go.dev/badge/github.com/vbetsun/goleak-example?status.svg\n[doc]: https://pkg.go.dev/github.com/vbetsun/goleak-example\n[ci-img]: https://github.com/vbetsun/goleak-example/actions/workflows/ci.yml/badge.svg\n[ci]: https://github.com/vbetsun/goleak-example/actions/workflows/ci.yml\n[report-img]: https://goreportcard.com/badge/github.com/vbetsun/goleak-example\n[report]: https://goreportcard.com/report/github.com/vbetsun/goleak-example\n[cov-img]: https://codecov.io/gh/vbetsun/goleak-example/branch/master/graph/badge.svg\n[cov]: https://codecov.io/gh/vbetsun/goleak-example\n[version-img]: https://img.shields.io/github/go-mod/go-version/vbetsun/goleak-example.svg\n[version]: https://github.com/vbetsun/goleak-example\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F0xvbetsun%2Fgoleak-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F0xvbetsun%2Fgoleak-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F0xvbetsun%2Fgoleak-example/lists"}