Go version
1.24.4
Output of go env
in your module/workspace:
AR='ar'
CC='cc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='c++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN='$HOME/go/bin'
GOCACHE='$HOME/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='$HOME/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/tmp/go-build=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD='$PROJECT_PATH/go.mod'
GOMODCACHE='$HOME/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='$HOME/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='$HOME/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.24.4'
GOWORK=''
PKG_CONFIG='pkg-config'
What did you do?
I tried two code snippets. The first one:
package main
import "fmt"
func main() {
defer func() {
recoverHelper()
}()
panic("oops")
}
func recoverHelper() {
if r := recover(); r != nil {
fmt.Println("Recovered:", r)
}
}
The second snippet adds an argument to the anonymous function (which forces the compiler to generate a defer wrapper):
package main
import "fmt"
func main() {
defer func(i int) {
recoverHelper()
}(1)
panic("oops")
}
func recoverHelper() {
if r := recover(); r != nil {
fmt.Println("Recovered:", r)
}
}
What did you see happen?
The first snippet panics, but the second one does not.
My suspicion is that the second snippet creates a defer wrapper and changes _panic.argp
to point to the wrapper's stack frame itself. The anonymous function is inlined with a large budget (800), making the wrapper think it is calling recoverHelper
directly. As a result, recoverHelper
has the same argument pointer value as _panic.argp
, so when gorecover
is called, it successfully recovers.
What did you expect to see?
Both of them panic.
Comment From: zigo101
The second doesn't panic since Go toolchain 1.22.
Comment From: gabyhelp
Related Issues
- cmd/compile: incorrect recover behavior due to defer wrapper #73916
- builtin: recover returns nil in an anonymous function from an variable #56254 (closed)
- `defer recover()` does not recover from panic #53169 (closed)
- affected/package: builtin Inlining causes spec violation in recover() #54119 (closed)
- cmd/compile: yet another incorrect recover behavior due to defer a method wrapper #73920
- go/compile: Panics involving defer statements do not consistently display line with defer in stack traces #69761 (closed)
- builtin: can't "defer recover()" without an anonymous func #20768 (closed)
- `bad defer entry in panic` with panic in defer #61059 (closed)
- runtime: can't recover #50512 (closed)
- Documentation: deferring a recover directly is a no-op (spec) #25815 (closed)
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
Comment From: func25
Closed as duplicate of https://github.com/golang/go/issues/73916.