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 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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 @@ -120,4 +120,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
17 changes: 17 additions & 0 deletions src/runtime/crash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,23 @@ func buildTestProg(t *testing.T, binary string, flags ...string) (string, error)
t.Logf("running %v", cmd)
cmd.Dir = "testdata/" + binary
cmd = testenv.CleanCmdEnv(cmd)

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

out, err := cmd.CombinedOutput()
if err != nil {
target.err = fmt.Errorf("building %s %v: %v\n%s", binary, flags, err, out)
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 @@ -1219,7 +1219,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
Loading