Skip to content

runtime: confusing panic on parallel calls to yield function #68897

@hauntedness

Description

@hauntedness

Go version

go1.23.0-windows-amd64

Output of go env in your module/workspace:

set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\hauntedness\AppData\Local\go-build
set GOENV=C:\Users\hauntedness\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=rangefunc
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\hauntedness\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\hauntedness\go
set GOPRIVATE=
set GOPROXY=https://goproxy.cn,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.23.0
set GODEBUG=
set GOTELEMETRY=on
set GOTELEMETRYDIR=C:\Users\hauntedness\AppData\Roaming\go\telemetry
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=D:\temp\dot\go.mod
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\HAUNTE~1\AppData\Local\Temp\go-build2719328347=/tmp/go-build -gno-record-gcc-switches

What did you do?

package main

import (
	"fmt"
	"sync"
)

func main() {
	for i := range WaitAll(10) {
		fmt.Println(i)
	}
}

func WaitAll(total int) func(yield func(i int) bool) {
	return func(yield func(i int) bool) {
		wg := &sync.WaitGroup{}
		wg.Add(total)
		for i := range total {
			go func(j int) {
				defer wg.Done()
				if !yield(j) {
					panic("Shouldn't be broken.")
				}
			}(i)
		}
		wg.Wait()
	}
}

See https://go.dev/play/p/_X0iHMy6lH1

What did you see happen?

==================
panic: runtime error: range function continued iteration after loop body panic

goroutine 21 [running]:
main.main-range1(0x2)
D:/temp/dot/play/iter.go:9 +0xf3
main.main.WaitAll.func1.1(0x2)
D:/temp/dot/play/iter.go:21 +0x8c
created by main.main.WaitAll.func1 in goroutine 1
D:/temp/dot/play/iter.go:19 +0x8c
exit status 2

What did you expect to see?

No panic

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.compiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    Status

    Todo

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions