Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

builtin: recover returns nil in an anonymous function from an variable #56254

Closed
maranqz opened this issue Oct 16, 2022 · 5 comments
Closed

builtin: recover returns nil in an anonymous function from an variable #56254

maranqz opened this issue Oct 16, 2022 · 5 comments

Comments

@maranqz
Copy link

maranqz commented Oct 16, 2022

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

1.19, 1.18

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/maranqz/Library/Caches/go-build"
GOENV="/Users/maranqz/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/maranqz/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/iasergunin/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/iasergunin/go/go1.19"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/maranqz/go/go1.19/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.19"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/g_/t4zy6gcj5hj6g9_hrykqswwr0000gn/T/go-build3751429232=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I creates and puts an anonymous function in a variable. Then, I uses the variable in defer function to recover panic.

https://go.dev/play/p/w5yIvY-q5BL

What did you expect to see?

Recovery returns a message from the panic.

What did you see instead?

Recovery returns nil and program shutdown with the panic.

@maranqz
Copy link
Author

maranqz commented Oct 16, 2022

Use the code to fix it.
https://go.dev/play/p/ZLHAxy4yMoF

@domenicomastrangelo
Copy link

domenicomastrangelo commented Oct 16, 2022

This fixes your issue without modifying the code too much

package main

import "fmt"

func printRecoverer() func() {
	return func() {
	    	fmt.Println("print", recover())
   	}
}

func main() {
    d := printRecoverer()

    defer d()

    panic("panic")
}

@maranqz
Copy link
Author

maranqz commented Oct 16, 2022

I needs to commit or rollback of a transaction and return err if it happens.

That is why I needs to func(){}() in defer.

https://go.dev/play/p/pGFLbrRWe58

@maranqz
Copy link
Author

maranqz commented Oct 16, 2022

I think I misunderstands recover because my case differs from docs explanation, isn't it?

Docs says:

The return value of recover is nil if any of the following conditions holds:
...
* recover was not called directly by a deferred function.

https://go.dev/ref/spec#Handling_panics

@seankhliao
Copy link
Member

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
@golang golang locked and limited conversation to collaborators Oct 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants