Skip to content

runtime: goroutine leak detection by using the garbage collector #74622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/internal/goexperiment/exp_goleakfindergc_off.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/internal/goexperiment/exp_goleakfindergc_on.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/internal/goexperiment/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,7 @@ type Flags struct {
// RandomizedHeapBase enables heap base address randomization on 64-bit
// platforms.
RandomizedHeapBase64 bool

// GoroutineLeakFinderGC enables the Deadlock GC implementation.
GoroutineLeakFinderGC bool
}
32 changes: 16 additions & 16 deletions src/runtime/chan.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,11 @@ func chansend(c *hchan, ep unsafe.Pointer, block bool, callerpc uintptr) bool {
}
// No stack splits between assigning elem and enqueuing mysg
// on gp.waiting where copystack can find it.
mysg.elem = ep
mysg.elem.set(ep)
mysg.waitlink = nil
mysg.g = gp
mysg.isSelect = false
mysg.c = c
mysg.c.set(c)
gp.waiting = mysg
gp.param = nil
c.sendq.enqueue(mysg)
Expand Down Expand Up @@ -298,7 +298,7 @@ func chansend(c *hchan, ep unsafe.Pointer, block bool, callerpc uintptr) bool {
if mysg.releasetime > 0 {
blockevent(mysg.releasetime-t0, 2)
}
mysg.c = nil
mysg.c.set(nil)
releaseSudog(mysg)
if closed {
if c.closed == 0 {
Expand Down Expand Up @@ -336,9 +336,9 @@ func send(c *hchan, sg *sudog, ep unsafe.Pointer, unlockf func(), skip int) {
c.sendx = c.recvx // c.sendx = (c.sendx+1) % c.dataqsiz
}
}
if sg.elem != nil {
if sg.elem.get() != nil {
sendDirect(c.elemtype, sg, ep)
sg.elem = nil
sg.elem.set(nil)
}
gp := sg.g
unlockf()
Expand Down Expand Up @@ -395,7 +395,7 @@ func sendDirect(t *_type, sg *sudog, src unsafe.Pointer) {
// Once we read sg.elem out of sg, it will no longer
// be updated if the destination's stack gets copied (shrunk).
// So make sure that no preemption points can happen between read & use.
dst := sg.elem
dst := sg.elem.get()
typeBitsBulkBarrier(t, uintptr(dst), uintptr(src), t.Size_)
// No need for cgo write barrier checks because dst is always
// Go memory.
Expand All @@ -406,7 +406,7 @@ func recvDirect(t *_type, sg *sudog, dst unsafe.Pointer) {
// dst is on our stack or the heap, src is on another stack.
// The channel is locked, so src will not move during this
// operation.
src := sg.elem
src := sg.elem.get()
typeBitsBulkBarrier(t, uintptr(dst), uintptr(src), t.Size_)
memmove(dst, src, t.Size_)
}
Expand Down Expand Up @@ -441,9 +441,9 @@ func closechan(c *hchan) {
if sg == nil {
break
}
if sg.elem != nil {
typedmemclr(c.elemtype, sg.elem)
sg.elem = nil
if sg.elem.get() != nil {
typedmemclr(c.elemtype, sg.elem.get())
sg.elem.set(nil)
}
if sg.releasetime != 0 {
sg.releasetime = cputicks()
Expand All @@ -463,7 +463,7 @@ func closechan(c *hchan) {
if sg == nil {
break
}
sg.elem = nil
sg.elem.set(nil)
if sg.releasetime != 0 {
sg.releasetime = cputicks()
}
Expand Down Expand Up @@ -642,13 +642,13 @@ func chanrecv(c *hchan, ep unsafe.Pointer, block bool) (selected, received bool)
}
// No stack splits between assigning elem and enqueuing mysg
// on gp.waiting where copystack can find it.
mysg.elem = ep
mysg.elem.set(ep)
mysg.waitlink = nil
gp.waiting = mysg

mysg.g = gp
mysg.isSelect = false
mysg.c = c
mysg.c.set(c)
gp.param = nil
c.recvq.enqueue(mysg)
if c.timer != nil {
Expand Down Expand Up @@ -680,7 +680,7 @@ func chanrecv(c *hchan, ep unsafe.Pointer, block bool) (selected, received bool)
}
success := mysg.success
gp.param = nil
mysg.c = nil
mysg.c.set(nil)
releaseSudog(mysg)
return true, success
}
Expand Down Expand Up @@ -727,14 +727,14 @@ func recv(c *hchan, sg *sudog, ep unsafe.Pointer, unlockf func(), skip int) {
typedmemmove(c.elemtype, ep, qp)
}
// copy data from sender to queue
typedmemmove(c.elemtype, qp, sg.elem)
typedmemmove(c.elemtype, qp, sg.elem.get())
c.recvx++
if c.recvx == c.dataqsiz {
c.recvx = 0
}
c.sendx = c.recvx // c.sendx = (c.sendx+1) % c.dataqsiz
}
sg.elem = nil
sg.elem.set(nil)
gp := sg.g
unlockf()
gp.param = unsafe.Pointer(sg)
Expand Down
6 changes: 3 additions & 3 deletions src/runtime/crash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,18 @@ func buildTestProg(t *testing.T, binary string, flags ...string) (string, error)
cmd.Dir = "testdata/" + binary
cmd = testenv.CleanCmdEnv(cmd)

// Add the rangefunc GOEXPERIMENT unconditionally since some tests depend on it.
// Add the rangefunc and goroutineleakfindergc GOEXPERIMENT unconditionally since some tests depend on it.
// TODO(61405): Remove this once it's enabled by default.
edited := false
for i := range cmd.Env {
e := cmd.Env[i]
if _, vars, ok := strings.Cut(e, "GOEXPERIMENT="); ok {
cmd.Env[i] = "GOEXPERIMENT=" + vars + ",rangefunc"
cmd.Env[i] = "GOEXPERIMENT=" + vars + ",rangefunc,goroutineleakfindergc"
edited = true
}
}
if !edited {
cmd.Env = append(cmd.Env, "GOEXPERIMENT=rangefunc")
cmd.Env = append(cmd.Env, "GOEXPERIMENT=rangefunc,goroutineleakfindergc")
}

out, err := cmd.CombinedOutput()
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,7 @@ func (t *SemTable) Enqueue(addr *uint32) {
s.releasetime = 0
s.acquiretime = 0
s.ticket = 0
t.semTable.rootFor(addr).queue(addr, s, false)
t.semTable.rootFor(addr).queue(addr, s, false, false)
}

// Dequeue simulates dequeuing a waiter for a semaphore (or lock) at addr.
Expand Down
138 changes: 138 additions & 0 deletions src/runtime/gc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"math/rand"
"os"
"reflect"
"regexp"
"runtime"
"runtime/debug"
"slices"
Expand Down Expand Up @@ -1095,3 +1096,140 @@ func TestDetectFinalizerAndCleanupLeaks(t *testing.T) {
t.Fatalf("expected %d symbolized locations, got:\n%s", wantSymbolizedLocations, got)
}
}

func TestGoroutineLeakGC(t *testing.T) {
type testCase struct {
tname string
funcName string
expectedLeaks map[*regexp.Regexp]int
}

testCases := []testCase{{
tname: "ChanReceiveNil",
funcName: "GoroutineLeakNilRecv",
expectedLeaks: map[*regexp.Regexp]int{
regexp.MustCompile(`\[chan receive \(nil chan\)\]`): 0,
},
}, {
tname: "ChanSendNil",
funcName: "GoroutineLeakNilSend",
expectedLeaks: map[*regexp.Regexp]int{
regexp.MustCompile(`\[chan send \(nil chan\)\]`): 0,
},
}, {
tname: "SelectNoCases",
funcName: "GoroutineLeakSelectNoCases",
expectedLeaks: map[*regexp.Regexp]int{
regexp.MustCompile(`\[select \(no cases\)\]`): 0,
},
}, {
tname: "ChanRecv",
funcName: "GoroutineLeakChanRecv",
expectedLeaks: map[*regexp.Regexp]int{
regexp.MustCompile(`\[chan receive\]`): 0,
},
}, {
tname: "ChanSend",
funcName: "GoroutineLeakChanSend",
expectedLeaks: map[*regexp.Regexp]int{
regexp.MustCompile(`\[chan send\]`): 0,
},
}, {
tname: "Select",
funcName: "GoroutineLeakSelect",
expectedLeaks: map[*regexp.Regexp]int{
regexp.MustCompile(`\[select\]`): 0,
},
}, {
tname: "WaitGroup",
funcName: "GoroutineLeakWaitGroup",
expectedLeaks: map[*regexp.Regexp]int{
regexp.MustCompile(`\[sync\.WaitGroup\.Wait\]`): 0,
},
}, {
tname: "MutexStack",
funcName: "GoroutineLeakMutexStack",
expectedLeaks: map[*regexp.Regexp]int{
regexp.MustCompile(`\[sync\.Mutex\.Lock\]`): 0,
},
}, {
tname: "MutexHeap",
funcName: "GoroutineLeakMutexHeap",
expectedLeaks: map[*regexp.Regexp]int{
regexp.MustCompile(`\[sync\.Mutex\.Lock\]`): 0,
},
}, {
tname: "Cond",
funcName: "GoroutineLeakCond",
expectedLeaks: map[*regexp.Regexp]int{
regexp.MustCompile(`\[sync\.Cond\.Wait\]`): 0,
},
}, {
tname: "RWMutexRLock",
funcName: "GoroutineLeakRWMutexRLock",
expectedLeaks: map[*regexp.Regexp]int{
regexp.MustCompile(`\[sync\.RWMutex\.RLock\]`): 0,
},
}, {
tname: "RWMutexLock",
funcName: "GoroutineLeakRWMutexLock",
expectedLeaks: map[*regexp.Regexp]int{
// Invoking Lock on a RWMutex may either put a goroutine a waiting state
// of either sync.RWMutex.Lock or sync.Mutex.Lock.
regexp.MustCompile(`\[sync\.(RW)?Mutex\.Lock\]`): 0,
},
}, {
tname: "Mixed",
funcName: "GoroutineLeakMixed",
expectedLeaks: map[*regexp.Regexp]int{
regexp.MustCompile(`\[sync\.WaitGroup\.Wait\]`): 0,
regexp.MustCompile(`\[chan send\]`): 0,
},
}, {
tname: "NoLeakGlobal",
funcName: "NoGoroutineLeakGlobal",
}}

failStates := regexp.MustCompile(`fatal|panic`)

for _, tcase := range testCases {
t.Run(tcase.tname, func(t *testing.T) {
exe, err := buildTestProg(t, "testprog")
if err != nil {
t.Fatal(fmt.Sprintf("building testprog failed: %v", err))
}
output := runBuiltTestProg(t, exe, tcase.funcName, "GODEBUG=gctrace=1,gcgoroutineleaks=1")

if len(tcase.expectedLeaks) == 0 && strings.Contains(output, "goroutine leak!") {
t.Fatalf("output:\n%s\n\nunexpected goroutines leaks detected", output)
return
}

if failStates.MatchString(output) {
t.Fatalf("output:\n%s\n\nunexpected fatal exception or panic", output)
return
}

for _, line := range strings.Split(output, "\n") {
if strings.Contains(line, "goroutine leak!") {
for expectedLeak, count := range tcase.expectedLeaks {
if expectedLeak.MatchString(line) {
tcase.expectedLeaks[expectedLeak] = count + 1
}
}
}
}

missingLeakStrs := make([]string, 0, len(tcase.expectedLeaks))
for expectedLeak, count := range tcase.expectedLeaks {
if count == 0 {
missingLeakStrs = append(missingLeakStrs, expectedLeak.String())
}
}

if len(missingLeakStrs) > 0 {
t.Fatalf("output:\n%s\n\nnot enough goroutines leaks detected. Missing:\n%s", output, strings.Join(missingLeakStrs, ", "))
}
})
}
}
22 changes: 22 additions & 0 deletions src/runtime/mbitmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,28 @@ func markBitsForSpan(base uintptr) (mbits markBits) {
return mbits
}

// isMarkedOrNotInHeap returns true if a pointer is in the heap and marked,
// or if the pointer is not in the heap. Used by goroutine leak detection
// to determine if concurrency resources are reachable in memory.
func isMarkedOrNotInHeap(p unsafe.Pointer) bool {
obj, span, objIndex := findObject(uintptr(p), 0, 0)
if obj != 0 {
mbits := span.markBitsForIndex(objIndex)
return mbits.isMarked()
}

// If we fall through to get here, the object is not in the heap.
// In this case, it is either a pointer to a stack object or a global resource.
// Treat it as reachable in memory by default, to be safe.
//
// (vsaioc) TODO: we could possibly be more precise by only checking against the stacks
// of runnable goroutines. I don't think this is necessary, based on what we've seen, but
// let's keep the option open in case the runtime evolves.
// This will (naively) lead to quadratic blow-up for goroutine leak detection,
// but if it is only run on demand, maybe the extra cost is not a show-stopper.
return true
}

// advance advances the markBits to the next object in the span.
func (m *markBits) advance() {
if m.mask == 1<<7 {
Expand Down
Loading