Created
May 7, 2025 17:10
-
-
Save paulja/b55c33677646a44290045897b53562ef to your computer and use it in GitHub Desktop.
Go Dump Goroutines on Exit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"os/signal" | |
"runtime" | |
"syscall" | |
"time" | |
) | |
func main() { | |
sig := make(chan os.Signal, 1) | |
signal.Notify(sig, syscall.SIGINT, syscall.SIGQUIT) | |
cancel := make(chan bool) | |
go func() { | |
fmt.Print("running") | |
for { | |
<-time.After(time.Second) | |
fmt.Print(".") | |
} | |
}() | |
go func() { | |
<-sig | |
fmt.Printf("\nstopping...\n") | |
buf := make([]byte, 1<<20) | |
n := runtime.Stack(buf, true) | |
fmt.Printf("\n*** goroutine dump\n%s\n*** end\n", buf[:n]) | |
cancel <- true | |
}() | |
<-cancel | |
fmt.Println("done") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The output of this prints something like: