|
1 | 1 | ## Runtime {#runtime} |
| 2 | + |
| 3 | +### Goroutine leak profiles {#goroutineleak-profiles} |
| 4 | + |
| 5 | +We introduce a new profile type for goroutine leaks. With the experimental flag set to `GOEXPERIMENT=goroutineleakprofile`, it becomes accessible through `pprof` under the name `"goroutineleak"`. |
| 6 | + |
| 7 | +The following snippet showcases a common anti-pattern that leads to goroutine leaks: |
| 8 | +```go |
| 9 | +// AggregateResults concurrently processes each request and aggregates the results. |
| 10 | +// If one of the requests returns an error, the function returns immediately with the error. |
| 11 | +func (s *Server[T, R]) AggregateResults(reqs []T) ([]R, error) { |
| 12 | + ch := make(chan wrap[R]) |
| 13 | + for _, req := range reqs { |
| 14 | + go func(req T) { |
| 15 | + res, err := s.processRequest(req) |
| 16 | + ch <- wrap[R]{ |
| 17 | + res: res, |
| 18 | + err: err, |
| 19 | + } |
| 20 | + }(req) |
| 21 | + } |
| 22 | + |
| 23 | + var results []R |
| 24 | + for range len(reqs) { |
| 25 | + x := <-ch |
| 26 | + if x.err != nil { |
| 27 | + return nil, x.err |
| 28 | + } |
| 29 | + results = append(results, x.res) |
| 30 | + } |
| 31 | + return results, nil |
| 32 | +} |
| 33 | +``` |
| 34 | +Channel `ch` is used to synchronize when concurrently processing each request in the slice `reqs`. |
| 35 | +The responses are aggregated in a slice if all requests succeed. |
| 36 | +Conversely, if any request produces an error, `AggregateResults` is shortcircuited to |
| 37 | +return the error. |
| 38 | +However, because `ch` is unbuffered, all pending request goroutines beyond the first to produce |
| 39 | +the error will leak. |
| 40 | + |
| 41 | +The key insight is that `ch` is inaccessible outside the scope of `AggregateResults`. |
| 42 | +The Go runtime is now equipped to detect such patterns as they occur at execution time, |
| 43 | +and record them in the goroutine leak profile. |
| 44 | +For the case above, the goroutine leak profile would appear as: |
| 45 | +``` |
| 46 | +Samples: |
| 47 | +goroutineleak/count |
| 48 | + 6: 1 2 3 4 |
| 49 | +Locations |
| 50 | + 1: 0x104235daf M=1 runtime.gopark src/runtime/proc.go:464:0 s=447 |
| 51 | + 2: 0x1041c1ce7 M=1 runtime.chansend src/runtime/chan.go:283:0 s=176 |
| 52 | + 3: 0x1041c18f7 M=1 runtime.chansend1 src/runtime/chan.go:161:0 s=160 |
| 53 | + 4: 0x10428dd6b M=1 app.(*Server[go.shape.int,go.shape.int]).AggregateResults.func1 app/server.go:37:0 s=35 |
| 54 | +``` |
| 55 | +The leaked goroutines' stack precisely pinpoints the leaking operation in the source code. |
| 56 | + |
| 57 | +The main advantage of goroutine leak profiles is that they have **no false positives**, but, for theoretical reasons, they may nevertheless |
| 58 | +miss some goroutine leaks, e.g., when caused by global channels. |
| 59 | +The underlying theory is presented in detail in [this publication by Saioc et al.](https://dl.acm.org/doi/pdf/10.1145/3676641.3715990). |
| 60 | + |
| 61 | +More details about the implementation are presented in the [design document](https://github.com/golang/proposal/blob/master/design/74609-goroutine-leak-detection-gc.md). |
| 62 | +We encourage users to experiment with the new feature in different environments (tests, CI, production), and welcome feedback on the [proposal issue](https://github.com/golang/go/issues/74609). |
0 commit comments