Skip to content

Commit e208108

Browse files
committed
fix
1 parent 3eb9c59 commit e208108

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

internal/xtest/leak.go

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

internal/xtest/leak_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ func TestCheckGoroutinesLeak(t *testing.T) {
1111
TestManyTimes(t, func(t testing.TB) {
1212
ch := make(chan struct{})
1313
require.Panics(t, func() {
14-
defer checkGoroutinesLeak(func(goroutines []string) {
15-
panic(goroutines)
14+
defer checkGoroutinesLeak(func([]string) {
15+
panic("test")
1616
})
1717
go func() {
1818
<-ch
@@ -24,8 +24,8 @@ func TestCheckGoroutinesLeak(t *testing.T) {
2424
t.Run("NoLeak", func(t *testing.T) {
2525
TestManyTimes(t, func(t testing.TB) {
2626
require.NotPanics(t, func() {
27-
defer checkGoroutinesLeak(func(goroutines []string) {
28-
panic(goroutines)
27+
defer checkGoroutinesLeak(func([]string) {
28+
panic("test")
2929
})
3030
ch := make(chan struct{})
3131
defer func() {

0 commit comments

Comments
 (0)