go: runtime: confusing panic on parallel calls to yield function
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
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 24 (14 by maintainers)
@lmittmann I’m not sure precisely what you are asking. A single iterator function, that takes a yield function as an argument, can’t call that yield function concurrently. There is no restrictions on different yield functions passed to different iterator functions.
I actually meant what you so succinctly put into words, where I utterly failed with mine.
Yes: you can’t call yield until the previous call to yield has returned. In the language spec this is written as (https://go.dev/ref/spec#For_statements):
I think it’s too early to say whether we need a vet check for this. We only want vet checks for errors that are frequently made. This strikes me as unlikely to be frequent.
I also don’t know how we could write an accurate vet check here.
@ianlancetaylor Ok, you mean the yield calls are not independent, they must be called sequential or at least sequential access some internal shared state right? As it is supper hiden rule here, do we consider to add a vet? I believe lot’s of people would do it similar。
@thediveo I agree that some panic would be acceptable for this program, but “range function continued iteration after loop body panic” is just inaccurate. The loop body did not panic.
Related Issues and Documentation
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)