Given:
func main() {
timeout := time.NewTimer(n)
go longRunningFn()
<-timeout.C
}
Is it possible to get a stacktrace of longRunningFn
when main
times out?
Yes, you can use the runtime.Stack to print the stack of all goroutines:
Stack formats a stack trace of the calling goroutine into buf and returns the number of bytes written to buf. If all is true, Stack formats stack traces of all other goroutines into buf after the trace for the current goroutine.
Simple GoPlay here:
https://play.golang.org/p/sB-ynAVwmU
It also looks like you can print out the stack of a specific goroutine by using debug.PrintStack in conjunction with the runtime library. Credit from another S.O. answer here: How to dump goroutine stacktraces?