Skip to content

No way to catch errors from goroutines automatically #20161

@muravjov

Description

@muravjov

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

$ go version
go version go1.7.5 linux/amd64

What operating system and processor architecture are you using (go env)?

$ go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH=""
GORACE=""
GOROOT="/opt/programming/golang/git"
GOTOOLDIR="/opt/programming/golang/git/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build930744821=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"

What did you do?

Thanks to go keyword it is easy to write concurrent algorithms, but also it is easy to make errors, which one cannot catch. For example:

package main

func main() {
	go func(){
		// = panic() = exit(2) + print(stacktrace)
		i := 0
		i = 1 / i
	}()
}

And you cannot catch it unless you manually add defer to every go invocation, like that:

func main() {
	go func(){
		defer func() {
			err := recover()
			...
			panic(err)
		}()

		// = panic() = exit(2) + print(stacktrace)
		i := 0
		i = 1 / i
	}()
}

You may catch such errors only by reading stderr from target process and guessing it is a Go stacktrace. Which is a hack.

What did you expect to see?

I need a mechanism to catch errors and send them with tracebacks (e.g. into https://sentry.io ). Like postmortem library Breakpad for C++ .

But there is no way to hook a defer to every new goroutine. It may be realized via special API to call newdefer/deferproc internal function from runtime/panic.go .

P.S. Similar discussion is about atexit , where Ian Lance Taylor writes that
"The only fully reliable mechanism is a wrapper program ..."

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions