{"id":28064406,"url":"https://github.com/ysmood/gotrace","last_synced_at":"2025-10-11T08:32:47.114Z","repository":{"id":57545996,"uuid":"299425724","full_name":"ysmood/gotrace","owner":"ysmood","description":"A lib for monitoring runtime goroutine stack","archived":false,"fork":false,"pushed_at":"2022-04-01T09:45:19.000Z","size":31,"stargazers_count":39,"open_issues_count":2,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-12T13:56:18.542Z","etag":null,"topics":["goroutine","leak","leak-detection","test"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/ysmood/gotrace","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/ysmood.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}},"created_at":"2020-09-28T20:35:28.000Z","updated_at":"2024-06-01T16:51:14.000Z","dependencies_parsed_at":"2022-09-26T18:31:04.320Z","dependency_job_id":null,"html_url":"https://github.com/ysmood/gotrace","commit_stats":null,"previous_names":["ysmood/gostack"],"tags_count":13,"template":false,"template_full_name":null,"purl":"pkg:github/ysmood/gotrace","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ysmood%2Fgotrace","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ysmood%2Fgotrace/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ysmood%2Fgotrace/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ysmood%2Fgotrace/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ysmood","download_url":"https://codeload.github.com/ysmood/gotrace/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ysmood%2Fgotrace/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279006611,"owners_count":26084148,"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","status":"online","status_checked_at":"2025-10-11T02:00:06.511Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["goroutine","leak","leak-detection","test"],"created_at":"2025-05-12T13:51:29.172Z","updated_at":"2025-10-11T08:32:47.081Z","avatar_url":"https://github.com/ysmood.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# Overview\n\nA lib for monitoring runtime goroutine stack.\nSuch as wait for goroutines to exit, leak detection, etc.\n\n## Features\n\n- `context.Context` first design\n- Concurrent leak detection\n- No dependencies and 100% test coverage\n- Provides handy low-level APIs to extend the lib\n\n## Guides\n\nCheck the [examples](examples_test.go) and [godoc](https://pkg.go.dev/github.com/ysmood/gotrace) for detailed usage.\n\nLet's get started with a typical async producer-consumer model, can you find the leaking issue of the code below?\nIf we visit the port 3000 again and again the service will leak memory.\n\n```go\npackage main\n\nimport (\n    \"fmt\"\n    \"net/http\"\n)\n\nfunc main() {\n    _ = http.ListenAndServe(\":3000\", http.HandlerFunc(handle))\n}\n\nfunc handle(_ http.ResponseWriter, _ *http.Request) {\n    c := make(chan int)\n    go produce(c)\n    go consume(c)\n}\n\nfunc produce(c chan int) {\n    for i := range \"...\" {\n        c \u003c- i\n    }\n}\n\nfunc consume(c chan int) {\n    for i := range c {\n        fmt.Println(i)\n    }\n}\n```\n\nWe can use gotrace to locate the leak point, let's create a `main_test.go` file:\n\n```go\npackage main\n\nimport (\n    \"testing\"\n\n    \"github.com/ysmood/gotrace\"\n)\n\nfunc TestHandle(t *testing.T) {\n    // Just add one line before the standard test case\n    gotrace.CheckLeak(t, 0)\n\n    handle(nil, nil)\n}\n```\n\nRun `GODEBUG=\"tracebackancestors=10\" go test`, wait for 3 seconds you will see the below output:\n\n```txt\n--- FAIL: TestHandle (3.00s)\n    main_test.go:10: leaking goroutines: goroutine 6 [chan receive]:\n        play.consume(0x0)\n            /Users/ys/repos/play/default/main.go:25 +0x74\n```\n\nThe `range` on line 25 get stuck, the reason is `chan receive`, it keeps waiting for new messages from `c`, but the `produce` function has already exited, it will not send messages to `c` anymore.\n\nWe have several ways to fix it, such as change the `produce` to:\n\n```go\nfunc produce(c chan int) {\n    for i := range \"...\" {\n        c \u003c- i\n    }\n    close(c)\n}\n```\n\nRun `go test` again, the test should exit immediately without errors.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fysmood%2Fgotrace","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fysmood%2Fgotrace","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fysmood%2Fgotrace/lists"}