name: go-leak-detection description: "Use when Go work involves or investigates goroutine, timer, ticker, channel, socket, file-handle, process, shutdown, lifecycle, queue, retained-heap, map/cache/index, pprof, trace, or long-running leak concerns. Distinguish static/local evidence from runtime confirmation."
Go Leak Detection
Overview
Use this skill to investigate and validate leak risks in long-lived Go cluster paths. It covers goroutine/lifecycle leaks, OS resource leaks, and retained heap growth.
Workflow
Classify the leak class.
- Goroutine blocked after cancellation or shutdown
- Orphan timer/ticker or retry loop
- Channel send/receive blocked by lifecycle mismatch
- Socket, listener, file, process, or HTTP body not closed
- Queue or backpressure path retaining work after disconnect
- Retained heap growth in maps, caches, heaps, indexes, interners, or pools
Inspect ownership and shutdown.
- Identify goroutine owners, cancellation sources, close ordering, WaitGroups, deadlines, drain/drop policy, and cleanup coupling.
- Use
go-retained-state-auditas well when server-lifetime retained state is added or modified. - Use
go-hotpath-designas well when leak fixes touch hot paths or allocation-sensitive queues.
Define local tests.
- Add targeted lifecycle tests for start/stop, reconnect, cancellation, queue overflow, slow clients, and repeated churn when the code changes.
- Use
go test -race ./...when concurrency, lifecycle, queues, timers, cancellation, long-lived connections, or shared state are touched. - Consider
go.uber.org/goleakfor targeted packages with goroutine-heavy tests. Do not add it repo-wide without an approved scope item.
Use profiling and runtime evidence when needed.
- Use
go testprofiles:-memprofile,-blockprofile,-mutexprofile,-trace, andgo tool pprof/go tool tracefor focused tests. - Use
scripts/run-with-profiling.ps1for local runtime captures; it collects CPU, heap, allocs, block, mutex, goroutine, trace, retained-state, and OS process samples. - Optional Sysinternals tools such as
handle.exe, TCPView, or Process Explorer can improve Windows handle/socket/process evidence when installed. Missing optional tools are reported as evidence gaps only for investigations that need OS-handle proof.
- Use
Separate evidence levels.
- Static reasoning: ownership and close paths inspected.
- Local test evidence: targeted tests and race checks passed.
- Profile evidence: pprof/trace/goroutine/OS samples reviewed.
- Runtime confirmation: comparable long-running or load/churn capture shows stable goroutine, heap, handle, socket, and queue trends.
Output Expectations
- Include a
Leak-detection auditsection when this skill triggers. - Name the leak class, owner, cancellation/close path, tests, profiles, and remaining evidence gaps.
- Do not claim "no leak" unless the relevant evidence level was actually performed.
- Say "locally validated, not runtime-confirmed" when pprof or long-running runtime evidence is missing.