We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Goroutine Profiling

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!