@@ -7,22 +7,21 @@ import (
77 "testing"
88)
99
10- func goroutineStack (all bool ) []byte {
11- for i := 1 << 16 ; ; i *= 2 {
12- bb := make ([]byte , i )
13- if n := runtime .Stack (bb , all ); n < i {
14- return bb [:n ]
10+ func checkGoroutinesLeak (onLeak func (goroutines []string )) {
11+ var bb []byte
12+ for size := 1 << 16 ; ; size *= 2 {
13+ bb = make ([]byte , size )
14+ if n := runtime .Stack (bb , true ); n < size {
15+ bb = bb [:n ]
16+
17+ break
1518 }
1619 }
17- }
18-
19- func checkGoroutinesLeak (onLeak func (goroutines []string )) {
20- currentGoroutine := string (regexp .MustCompile (`^goroutine \d+ ` ).Find (goroutineStack (false )))
21- goroutines := strings .Split (string (goroutineStack (true )), "\n \n " )
20+ goroutines := strings .Split (string (bb ), "\n \n " )
2221 unexpectedGoroutines := make ([]string , 0 , len (goroutines ))
2322
24- for _ , g := range goroutines {
25- if strings . HasPrefix ( g , currentGoroutine ) {
23+ for i , g := range goroutines {
24+ if i == 0 {
2625 continue
2726 }
2827 stack := strings .Split (g , "\n " )
@@ -42,14 +41,21 @@ func checkGoroutinesLeak(onLeak func(goroutines []string)) {
4241 continue
4342 }
4443
45- case strings .HasPrefix (firstFunction , "runtime.goexit" ) && state == "syscall" :
46- continue
44+ case strings .HasPrefix (firstFunction , "runtime.goexit" ):
45+ switch state {
46+ case "syscall" , "runnable" :
47+ continue
48+ }
4749
4850 case strings .HasPrefix (firstFunction , "os/signal.signal_recv" ),
4951 strings .HasPrefix (firstFunction , "os/signal.loop" ):
50- if strings .Contains (g , "runtime.ensureSigM" ) {
51- continue
52- }
52+ continue
53+
54+ case strings .Contains (g , "runtime.ensureSigM" ):
55+ continue
56+
57+ case strings .Contains (g , "runtime.ReadTrace" ):
58+ continue
5359 }
5460
5561 unexpectedGoroutines = append (unexpectedGoroutines , g )
@@ -63,9 +69,6 @@ func CheckGoroutinesLeak(tb testing.TB) {
6369 tb .Helper ()
6470 checkGoroutinesLeak (func (goroutines []string ) {
6571 tb .Helper ()
66- tb .Errorf ("found %d unexpected goroutines:\n %s" ,
67- len (goroutines ),
68- strings .Join (goroutines , "\n " ),
69- )
72+ tb .Errorf ("found %d unexpected goroutines:\n %s" , len (goroutines ), strings .Join (goroutines , "\n " ))
7073 })
7174}
0 commit comments