Skip to content

runtime: recover added in range-over-func loop body doesn't stop panic propagation / segfaults printing error #71675

@newacorn

Description

@newacorn

Go version

go version go1.23.2 darwin/amd64

Output of go env in your module/workspace:

GO111MODULE='on'
GOARCH='amd64'
GOBIN=''
GOCACHE='/Users/acorn/Library/Caches/go-build'
GOENV='/Users/acorn/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/acorn/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/acorn/go'
GOPRIVATE=''
GOPROXY='direct'
GOROOT='/Users/acorn/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.23.2.darwin-amd64'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/acorn/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.23.2.darwin-amd64/pkg/tool/darwin_amd64'
GOVCS=''
GOVERSION='go1.23.2'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/acorn/Library/Application Support/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/acorn/go-demo/go.mod'
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 x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/6c/6skkl1zn2p9fcx2r70v0kyc00000gn/T/go-build362480746=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

When traversing an iterative function, there are some peculiar behaviors that arise concerning the defer within the loop, the panic in the outer function surrounding the loop, and the panic within the iterative function itself.

Here is a simple example that can trigger a segmentation fault. Below, I provide several links containing reproductions of a few peculiar behaviors.

func main() {
	for range yieldInts {
		defer func() {
			log.Println("recover:called")
			recover()
		}()
	}
}

func yieldInts(yield func(int) bool) {
	if !yield(0) {
		return
	}
	log.Println("will panic")
	panic("stop")
}

output:

2025/02/12 14:34:15 will panic
2025/02/12 14:34:15 recover:called
fatal error: panic while printing panic value: type runtime.errorString
[signal SIGSEGV: segmentation violation code=0x1 addr=0x29 pc=0x9e0d613]
...

What did you see happen?

2025/02/12 14:34:15 will panic
2025/02/12 14:34:15 recover:called
fatal error: panic while printing panic value: type runtime.errorString
[signal SIGSEGV: segmentation violation code=0x1 addr=0x29 pc=0x9e0d613]
...

Bizarre Behavior One
Bizarre Behavior two
Bizarre Behavior three

This issue likely arises from the compiler's handling of the stack when transforming the for loop body into a range function.

What did you expect to see?

I hope to observe behavior that aligns exactly with what is described in this official documentation. It should not include those unpredictable and peculiar behaviors that it does not describe.

Metadata

Metadata

Assignees

Labels

BugReportIssues describing a possible bug in the Go implementation.CriticalA critical problem that affects the availability or correctness of production systems built using GoNeedsFixThe path to resolution is known, but the work has not been done.compiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions