Skip to content

Commit

Permalink
leakcheck: use a growing buf for stacktraces
Browse files Browse the repository at this point in the history
  • Loading branch information
Adhityaa Chandrasekar committed Nov 12, 2019
1 parent caeb239 commit 506dba6
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions internal/leakcheck/leakcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"sort"
"strings"
"time"
"sync/atomic"
)

var goroutinesToIgnore = []string{
Expand Down Expand Up @@ -76,11 +77,24 @@ func ignore(g string) bool {
return false
}

var lastStacktraceSize uint32 = 4 << 10

// interestingGoroutines returns all goroutines we care about for the purpose of
// leak checking. It excludes testing or runtime ones.
func interestingGoroutines() (gs []string) {
buf := make([]byte, 2<<20)
buf = buf[:runtime.Stack(buf, true)]
n := atomic.LoadUint32(&lastStacktraceSize)
buf := make([]byte, n)
for {
nb := uint32(runtime.Stack(buf, true))
if nb < uint32(len(buf)) {
buf = buf[:nb]
break
}
n <<= 1
buf = make([]byte, n)
}
atomic.StoreUint32(&lastStacktraceSize, n)

for _, g := range strings.Split(string(buf), "\n\n") {
if !ignore(g) {
gs = append(gs, g)
Expand Down

0 comments on commit 506dba6

Please sign in to comment.