

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 4
click for more info
Not enough gems
Cost: 6 gems
1: Profiling
incomplete
2: Integrating pprof
incomplete
3: CPU Profiling
incomplete
4: CPU Profiling Quiz
incomplete
5: Memory Profiling
incomplete
6: Memory Profiling Quiz
incomplete
7: Goroutine Profiling
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
One of the most insidious types of bugs in a Go program is a goroutine leak. This happens when we unintentionally create goroutines that never exit. Finding such bugs can be a real pain in the rear end. Well, at least until you use goroutine profiling! Simply use the /goroutine endpoint:
curl http://localhost:XXXX/debug/pprof/goroutine --output goroutine.prof
Notice we didn't include a seconds=X query parameter this time. That's because when profiling goroutines, we get an instantaneous snapshot of all goroutines running at the moment.
I like to let my program run for a while before calling the goroutine profile endpoint because you need to have some goroutines that already leaked!
As before, you can read a goroutine profile using the go tool pprof CLI tool:
go tool pprof /path/to/linko goroutine.prof
As usual, the top command will show a ranked list of... ehm... goroutines:
Showing top 10 nodes out of 15
flat flat% sum% cum cum%
10 50% 50% 10 50% runtime.gopark
5 25% 75% 5 25% runtime.selectgo
3 15% 90% 3 15% linko/internal/worker.run
2 10% 100% 2 10% linko/internal/handler.HandleRequest
What this top-N list means for goroutines is not nearly as intuitive as it is for CPU or memory profiling. The flat and cum counts show the number of goroutines currently (at the time of the snapshot) running that function (directly, or cumulatively, respectively). In this case, half of the active goroutines are running the runtime.gopark function – that is to say, they're waiting for something to do.
Generally, you'll get more useful goroutine profiling insights using the web interface that go tool pprof provides:
go tool pprof -http=:0 /path/to/linko goroutine.prof
We won't be looking for a goroutine leak because they're notoriously brittle to reproduce. That said, I wanted you to at least know that this tool exists!