Closed
Description
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.