File size: 5,498 Bytes
e36aeda | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | // Copyright 2025 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"os"
"runtime"
"runtime/pprof"
"sync"
)
// This is a set of micro-tests with obvious goroutine leaks that
// ensures goroutine leak detection works.
//
// Tests in this file are not flaky iff. run with GOMAXPROCS=1.
// The main goroutine forcefully yields via `runtime.Gosched()` before
// running the profiler. This moves them to the back of the run queue,
// allowing the leaky goroutines to be scheduled beforehand and get stuck.
func init() {
register("NilRecv", NilRecv)
register("NilSend", NilSend)
register("SelectNoCases", SelectNoCases)
register("ChanRecv", ChanRecv)
register("ChanSend", ChanSend)
register("Select", Select)
register("WaitGroup", WaitGroup)
register("MutexStack", MutexStack)
register("MutexHeap", MutexHeap)
register("RWMutexRLock", RWMutexRLock)
register("RWMutexLock", RWMutexLock)
register("Cond", Cond)
register("Mixed", Mixed)
register("NoLeakGlobal", NoLeakGlobal)
}
func NilRecv() {
prof := pprof.Lookup("goroutineleak")
go func() {
var c chan int
<-c
panic("should not be reached")
}()
// Yield several times to allow the child goroutine to run.
for i := 0; i < yieldCount; i++ {
runtime.Gosched()
}
prof.WriteTo(os.Stdout, 2)
}
func NilSend() {
prof := pprof.Lookup("goroutineleak")
go func() {
var c chan int
c <- 0
panic("should not be reached")
}()
// Yield several times to allow the child goroutine to run.
for i := 0; i < yieldCount; i++ {
runtime.Gosched()
}
prof.WriteTo(os.Stdout, 2)
}
func ChanRecv() {
prof := pprof.Lookup("goroutineleak")
go func() {
<-make(chan int)
panic("should not be reached")
}()
// Yield several times to allow the child goroutine to run.
for i := 0; i < yieldCount; i++ {
runtime.Gosched()
}
prof.WriteTo(os.Stdout, 2)
}
func SelectNoCases() {
prof := pprof.Lookup("goroutineleak")
go func() {
select {}
panic("should not be reached")
}()
// Yield several times to allow the child goroutine to run.
for i := 0; i < yieldCount; i++ {
runtime.Gosched()
}
prof.WriteTo(os.Stdout, 2)
}
func ChanSend() {
prof := pprof.Lookup("goroutineleak")
go func() {
make(chan int) <- 0
panic("should not be reached")
}()
// Yield several times to allow the child goroutine to run.
for i := 0; i < yieldCount; i++ {
runtime.Gosched()
}
prof.WriteTo(os.Stdout, 2)
}
func Select() {
prof := pprof.Lookup("goroutineleak")
go func() {
select {
case make(chan int) <- 0:
case <-make(chan int):
}
panic("should not be reached")
}()
// Yield several times to allow the child goroutine to run.
for i := 0; i < yieldCount; i++ {
runtime.Gosched()
}
prof.WriteTo(os.Stdout, 2)
}
func WaitGroup() {
prof := pprof.Lookup("goroutineleak")
go func() {
var wg sync.WaitGroup
wg.Add(1)
wg.Wait()
panic("should not be reached")
}()
// Yield several times to allow the child goroutine to run.
for i := 0; i < yieldCount; i++ {
runtime.Gosched()
}
prof.WriteTo(os.Stdout, 2)
}
func MutexStack() {
prof := pprof.Lookup("goroutineleak")
for i := 0; i < 1000; i++ {
go func() {
var mu sync.Mutex
mu.Lock()
mu.Lock()
panic("should not be reached")
}()
}
// Yield several times to allow the child goroutine to run.
for i := 0; i < yieldCount; i++ {
runtime.Gosched()
}
prof.WriteTo(os.Stdout, 2)
}
func MutexHeap() {
prof := pprof.Lookup("goroutineleak")
for i := 0; i < 1000; i++ {
go func() {
mu := &sync.Mutex{}
go func() {
mu.Lock()
mu.Lock()
panic("should not be reached")
}()
}()
}
// Yield several times to allow the child goroutine to run.
for i := 0; i < yieldCount; i++ {
runtime.Gosched()
}
prof.WriteTo(os.Stdout, 2)
}
func RWMutexRLock() {
prof := pprof.Lookup("goroutineleak")
go func() {
mu := &sync.RWMutex{}
mu.Lock()
mu.RLock()
panic("should not be reached")
}()
// Yield several times to allow the child goroutine to run.
for i := 0; i < yieldCount; i++ {
runtime.Gosched()
}
prof.WriteTo(os.Stdout, 2)
}
func RWMutexLock() {
prof := pprof.Lookup("goroutineleak")
go func() {
mu := &sync.RWMutex{}
mu.Lock()
mu.Lock()
panic("should not be reached")
}()
// Yield several times to allow the child goroutine to run.
for i := 0; i < yieldCount; i++ {
runtime.Gosched()
}
prof.WriteTo(os.Stdout, 2)
}
func Cond() {
prof := pprof.Lookup("goroutineleak")
go func() {
cond := sync.NewCond(&sync.Mutex{})
cond.L.Lock()
cond.Wait()
panic("should not be reached")
}()
// Yield several times to allow the child goroutine to run.
for i := 0; i < yieldCount; i++ {
runtime.Gosched()
}
prof.WriteTo(os.Stdout, 2)
}
func Mixed() {
prof := pprof.Lookup("goroutineleak")
go func() {
ch := make(chan int)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
ch <- 0
wg.Done()
panic("should not be reached")
}()
wg.Wait()
<-ch
panic("should not be reached")
}()
// Yield several times to allow the child goroutine to run.
for i := 0; i < yieldCount; i++ {
runtime.Gosched()
}
prof.WriteTo(os.Stdout, 2)
}
var ch = make(chan int)
// No leak should be reported by this test
func NoLeakGlobal() {
prof := pprof.Lookup("goroutineleak")
go func() {
<-ch
}()
// Yield several times to allow the child goroutine to run.
for i := 0; i < yieldCount; i++ {
runtime.Gosched()
}
prof.WriteTo(os.Stdout, 2)
}
|