Global panic recovery mechanism?

1,112 views
Skip to first unread message

Slava Vishnyakov

unread,
Feb 27, 2016, 9:45:37 AM2/27/16
to golang-nuts
Hello,

Recently I've stumbled into a problem that if one of the goroutines panics - there seems to be no way to free up resources that were allocated in other goroutine or in main().

Basically, I run the process via `exec.Command` with `defer cmd.Process.Kill()` and then one of the goroutines panic'd. The process stayed there.

After Dave Cheney pointed me to https://golang.org/ref/spec#Defer_statements I understand that this works as currently defined.

The question is this: in other languages you usually have a way to define some form of try-catch-finally in global scope so that if anything anywhere goes wrong - you can cleanup.

Is there such a mechanism in Go? I kind of expected defer-recover in main() to work like that, unfortunately it's not the case.

Thanks!

Roberto Zanotto

unread,
Feb 27, 2016, 11:08:58 AM2/27/16
to golang-nuts
There is nothing like that.
You can recover each goroutine separately, but not all goroutines at once.


In general, panics should be used for really exceptional error conditions, like bugs.
For regular errors, use error and you'll be able to do all the cleanup you want. 

mikespook

unread,
Feb 27, 2016, 5:19:04 PM2/27/16
to Roberto Zanotto, golang-nuts
First of all, I suggest you reading: http://blog.golang.org/defer-panic-and-recover. After reading that post, you should understand it won't have `all in one` for error handling in Golang. It's against the design principle of Golang. You should NOT `throw` them into a general, robust, supreme error handler, but handle every error separately.

--
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.
For more options, visit https://groups.google.com/d/optout.



--
Xing Xing (邢星)
mikespook <mike...@gmail.com>
http://mikespook.com


Reply all
Reply to author
Forward
0 new messages