Skip to content

go/compile: Panics involving defer statements do not consistently display line with defer in stack traces #69761

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

Closed
varungandhi-src opened this issue Oct 3, 2024 · 2 comments

Comments

@varungandhi-src
Copy link

varungandhi-src commented Oct 3, 2024

Go version

go version go1.23.0 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='arm64'
GOBIN='/Users/varun/.local/share/mise/installs/go/1.23.0/bin'
GOCACHE='/Users/varun/Library/Caches/go-build'
GOENV='/Users/varun/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/varun/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/varun/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Users/varun/.local/share/mise/installs/go/1.23.0'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/varun/.local/share/mise/installs/go/1.23.0/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.23.0'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/varun/Library/Application Support/go/telemetry'
GCCGO='gccgo'
GOARM64='v8.0'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/tr/gylnxkts3bn0gyhhlrv8z9_m0000gn/T/go-build4046955043=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

When you have defer func() { /* code */ () and there is a nil pointer dereference in the body of func, then the stack trace (correctly) shows the line inside the func() where the nil pointer dereference happened. (Example: https://go.dev/play/p/qjGg11jBLwD)

func main() {
	s := struct{ f func() }{}
	defer func() {
		s.f()
	}()
}
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x4665ae]

goroutine 1 [running]:
main.main.func1()
	/tmp/sandbox2783684081/prog.go:8 +0xe
main.main()
	/tmp/sandbox2783684081/prog.go:10 +0x48

However, if you don't have a func() then the defer line is not shown in the panic (Example: https://go.dev/play/p/1tvIBFvpdss)


import "fmt"

func main() {
	s := struct{ f func() }{}
	defer s.f()
	fmt.Printf("Hello World\n!")
}
main.main()
	/tmp/sandbox3104930147/prog.go:11 +0x5a

This can make debugging quite confusing, as the final line in the stack trace might actually not have any code at all, or it might have some code which can never have a nil pointer exception.

What did you see happen?

Missing entry in the stack trace. See the section above.

What did you expect to see?

There should be an extra entry in the stack trace pointing to the appropriate line in the defer statement where the nil pointer dereference actually happened.

Aside

This issue is based on a confusing stack trace that my colleague @antonsviridov-src debugged earlier today.

image

@varungandhi-src
Copy link
Author

Closing this as a duplicate of #29797

@varungandhi-src varungandhi-src closed this as not planned Won't fix, can't repro, duplicate, stale Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants