uber handler to collect stacktrace when program crashing

157 views
Skip to first unread message

Aggarwal Sre

unread,
May 27, 2022, 1:42:43 PM5/27/22
to golan...@googlegroups.com
Hi ,

Is there a recommended pattern besides adding a defer call ( with recover call, to collect debug stack trace) to each goroutine, for collecting a stack trace when a golang is crashing due to any sort of panic.

In other words, is there a way to register an uber handler ( probably using OS signals) at the main function level, to indicate that a golang program crashed and stacktrace to clearly specify where exactly?

Thanks in advance,

Regards,
Vivek Aggarwal

Ian Lance Taylor

unread,
May 27, 2022, 10:09:40 PM5/27/22
to Aggarwal Sre, golan...@googlegroups.com
On Fri, May 27, 2022 at 10:42 AM Aggarwal Sre <sre.ag...@gmail.com> wrote:
>
> Is there a recommended pattern besides adding a defer call ( with recover call, to collect debug stack trace) to each goroutine, for collecting a stack trace when a golang is crashing due to any sort of panic.
>
> In other words, is there a way to register an uber handler ( probably using OS signals) at the main function level, to indicate that a golang program crashed and stacktrace to clearly specify where exactly?

A panic handle is run at the bottom of the stack. If you write this:

defer func() {
if recover() != nil {
fmt.Printf("%s", debug.Stack())
}
}()

in your main function, it will print a stack trace showing the point
of the panic.

Ian

Sean Liao

unread,
May 28, 2022, 4:27:48 AM5/28/22
to golang-nuts
I believe this is asking for a single handler that will apply to all goroutines, to which the answer is no such feature exists

- sean

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAOyqgcV4d9HxDbdp_in95j6aKFF_m%2BN_hFdeyUMuMOWRt5%2B6dw%40mail.gmail.com.

Aggarwal Sre

unread,
May 30, 2022, 11:11:47 AM5/30/22
to Sean Liao, golang-nuts
Yes, I was asking about a generic handler.  

I am thinking of writing a wrapper function which takes a function and spuns up a goroutine with the above mentioned defer function by default.  

Brian Candler

unread,
May 30, 2022, 12:14:45 PM5/30/22
to golang-nuts
Would it be possible to run your go program from a separate supervisor process which captures the stderr output?

Harri L

unread,
May 30, 2022, 2:09:33 PM5/30/22
to golang-nuts
If you write a wrapper, please look at the err2-package. It's an error-handling package, but we needed to build a panic-safe system with automatic stack tracing. Traces are context optimized as well. Here's a playground demo.

You add the following one-liners to your goroutines, and that's it.

go func() {
     defer err2.CatchTrace(func(err error) {})
     var b []byte
     b[1] = 0
}()

Reply all
Reply to author
Forward
0 new messages