Skip to content
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

v3.3/glfw: CreateWindow crashes after WaitEvents on macOS #262

Open
dmitshur opened this issue Nov 27, 2019 · 0 comments
Open

v3.3/glfw: CreateWindow crashes after WaitEvents on macOS #262

dmitshur opened this issue Nov 27, 2019 · 0 comments

Comments

@dmitshur
Copy link
Member

@dmitshur dmitshur commented Nov 27, 2019

This is the golang.org/go-gl/glfw/v3.3/glfw tracking bug for upstream GLFW issue glfw/glfw#1543. It can be reproduced with the following Go program:

package main

import (
	"log"
	"runtime"

	"github.com/go-gl/glfw/v3.3/glfw"
)

func init() { runtime.LockOSThread() }

func main() {
	err := glfw.Init()
	if err != nil {
		log.Fatalln(err)
	}
	defer glfw.Terminate()

	glfw.PostEmptyEvent()
	glfw.WaitEvents()

	glfw.WindowHint(glfw.ClientAPI, glfw.NoAPI)
	_, err = glfw.CreateWindow(640, 480, "Issue glfw/glfw#1543", nil, nil)
	if err != nil {
		log.Fatalln(err)
	}
}

Running it on macOS 10.15.1 with Xcode 11.2.1 produces the following crash:


$ go run .
2019-11-26 22:17:27.594 31[21710:337570] *** Assertion failure in -[NSApplication run], /BuildRoot/Library/Caches/com.apple.xbs/Sources/AppKit/AppKit-1894.10.126/AppKit.subproj/NSApplication.m:3313
2019-11-26 22:17:27.594 31[21710:337570] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSApp with wrong _running count'
*** First throw call stack:
(
	0   CoreFoundation                      0x00007fff30503f53 __exceptionPreprocess + 250
	1   libobjc.A.dylib                     0x00007fff665c9835 objc_exception_throw + 48
	2   CoreFoundation                      0x00007fff3051f810 +[NSException raise:format:arguments:] + 88
	3   Foundation                          0x00007fff32bff5d1 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191
	4   AppKit                              0x00007fff2d657ed3 -[NSApplication run] + 1007
	5   31                                  0x00000000040b21ad _glfwPlatformCreateWindow + 77
	6   31                                  0x00000000040ab3b5 glfwCreateWindow + 485
	7   31                                  0x00000000040b818b _cgo_78603e0816ec_Cfunc_glfwCreateWindow + 43
	8   31                                  0x0000000004055a40 runtime.asmcgocall + 112
)
libc++abi.dylib: terminating with uncaught exception of type NSException
SIGABRT: abort
PC=0x7fff67a7b49a m=0 sigcode=0
signal arrived during cgo execution

goroutine 1 [syscall, locked to thread]:
runtime.cgocall(0x40b8160, 0xc000059de8, 0x400e288)
	/usr/local/go/src/runtime/cgocall.go:128 +0x5b fp=0xc000059db8 sp=0xc000059d80 pc=0x4004fdb
github.com/go-gl/glfw/v3.3/glfw._Cfunc_glfwCreateWindow(0x1e000000280, 0x441fd00, 0x0, 0x0, 0x0)
	_cgo_gotypes.go:630 +0x4e fp=0xc000059de8 sp=0xc000059db8 pc=0x40a1cae
github.com/go-gl/glfw/v3.3/glfw.CreateWindow.func2(0x280, 0x1e0, 0x441fd00, 0x0, 0x0, 0xc000059e78)
	/Users/gopher/go/src/github.com/go-gl/glfw/v3.3/glfw/window.go:348 +0xab fp=0xc000059e20 sp=0xc000059de8 pc=0x40a481b
github.com/go-gl/glfw/v3.3/glfw.CreateWindow(0x280, 0x1e0, 0x40f8c5a, 0x14, 0x0, 0x0, 0x0, 0x0, 0x0)
	/Users/gopher/go/src/github.com/go-gl/glfw/v3.3/glfw/window.go:348 +0xff fp=0xc000059ec0 sp=0xc000059e20 pc=0x40a446f
main.main()
	/Users/gopher/go/src/github.com/shurcooL/play/31/main.go:21 +0x103 fp=0xc000059f60 sp=0xc000059ec0 pc=0x40a4cc3
runtime.main()
	/usr/local/go/src/runtime/proc.go:203 +0x21e fp=0xc000059fe0 sp=0xc000059f60 pc=0x402dc0e
runtime.goexit()
	/usr/local/go/src/runtime/asm_amd64.s:1357 +0x1 fp=0xc000059fe8 sp=0xc000059fe0 pc=0x40562a1

rax    0x0
rbx    0xf63fdc0
rcx    0x7ffeefbfdb68
rdx    0x0
rdi    0x307
rsi    0x6
rbp    0x7ffeefbfdb90
rsp    0x7ffeefbfdb68
r8     0x7ffeefbfda30
r9     0x7ffeefbfdc00
r10    0x0
r11    0x246
r12    0x307
r13    0x3000000008
r14    0x6
r15    0x16
rip    0x7fff67a7b49a
rflags 0x246
cs     0x7
fs     0x0
gs     0x0
exit status 2

To make progress here, we need to resolve glfw/glfw#1543 first and then pull in the fix into golang.org/go-gl/glfw/v3.3/glfw.

gopherbot pushed a commit to golang/exp that referenced this issue Nov 27, 2019
GLFW v3.2 was old and started generating some warnings on macOS 10.15
Catalina (and warnings get upgraded to errors on builders). Update to
GLFW v3.3 that was recently released to resolve the problem.

There is a small regression affecting the newer version of GLFW that is
being tracked in issues glfw/glfw#1543 and
go-gl/glfw#262. It causes the application to
crash when glfwWaitEvents is called before the first window is opened:

	$ go run -tags='example metal' .../shiny/example/basic
	2019-11-25 22:19:24.715 basic[9412:69556] *** Assertion failure in -[NSApplication run], /BuildRoot/Library/Caches/com.apple.xbs/Sources/AppKit/AppKit-1894.10.126/AppKit.subproj/NSApplication.m:3313
	2019-11-25 22:19:24.715 basic[9412:69556] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSApp with wrong _running count'
	*** First throw call stack:
	(
		0   CoreFoundation                      0x00007fff30503f53 __exceptionPreprocess + 250
		1   libobjc.A.dylib                     0x00007fff665c9835 objc_exception_throw + 48
		2   CoreFoundation                      0x00007fff3051f810 +[NSException raise:format:arguments:] + 88
		3   Foundation                          0x00007fff32bff5d1 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191
		4   AppKit                              0x00007fff2d657ed3 -[NSApplication run] + 1007
		5   basic                               0x00000000040d473d _glfwPlatformCreateWindow + 77
		6   basic                               0x00000000040cd945 glfwCreateWindow + 485
		7   basic                               0x00000000040da71b _cgo_78603e0816ec_Cfunc_glfwCreateWindow + 43
		8   basic                               0x0000000004058430 runtime.asmcgocall + 112
	)
	libc++abi.dylib: terminating with uncaught exception of type NSException
	SIGABRT: abort
	PC=0x7fff67a7b49a m=0 sigcode=0
	signal arrived during cgo execution
	[...]

Work around it by waiting for the first window open request before
entering the main loop. That way, glfwWaitEvents is not called
before a window has been created, and the crash does not occur.
This temporary workaround can be removed after the bug is fixed.

Fixes golang/go#35766
Updates go-gl/glfw#256
Updates glfw/glfw#1543
Updates go-gl/glfw#262

Change-Id: Ie9b15ab6632af39871d94993a3e3097ea798a7e2
Reviewed-on: https://go-review.googlesource.com/c/exp/+/208877
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.