Open hajimehoshi opened 1 month ago
The problem is that Ebitengine calls LockOSThread
in an internal init function, so the goroutine for the main function already locks an OS thread.
Actually, this works without crashing:
package main
import (
"iter"
"github.com/hajimehoshi/ebiten/v2"
)
type Game struct {
next func() (int, bool)
}
func (g *Game) Update() error {
g.next()
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
return outsideWidth, outsideHeight
}
func main() {
var next func() (int, bool)
ch := make(chan struct{})
go func() {
seq := func(yield func(int) bool) {
for {
if !yield(0) {
break
}
}
}
next, _ = iter.Pull(seq)
close(ch)
}()
<-ch
g := &Game{
next: next,
}
if err := ebiten.RunGame(g); err != nil {
panic(err)
}
}
Related: https://github.com/golang/go/issues/67694
We might not need LockOSThread
for main. See https://github.com/golang/go/issues/64777#issuecomment-2163641860
Sorry, 3d385ef0aa9ec5dabb41076c79e1290c15da32a0 doesn't fix the issue.
I think this is not a fixable issue unless mainthread
package is introduced... (golang/go#64777). Especially, some APIs for macOS must be called from the main thread, and there is no way to do it without LockOSThread
in an init
function.
Ebitengine Version
52820e2b43903f5460f21f46eeb9038cd0973260
Operating System
Go Version (
go version
)go version go1.23rc1 darwin/arm64
What steps will reproduce the problem?
Run this program with Go 1.23 (rc1):
What is the expected result?
No crash
What happens instead?
Crash:
Anything else you feel useful to add?
Reported by @eihigh
If needed, let's backport the fix to 2.7