Skip to content

Commit 81c0e5f

Browse files
authored
wrap glfw.PostEmptyEvent funcion to prevent panic (#579)
1 parent 729801f commit 81c0e5f

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

application.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"runtime"
7+
"runtime/debug"
78
"time"
89
"unsafe"
910

@@ -62,6 +63,17 @@ func NewApplication(opt ...Option) *Application {
6263
return app
6364
}
6465

66+
func postEmptyEvent() {
67+
defer func() {
68+
p := recover()
69+
if p != nil {
70+
fmt.Printf("go-flutter: recovered from panic 'glfw.PostEmptyEvent()': %v\n", p)
71+
debug.PrintStack()
72+
}
73+
}()
74+
glfw.PostEmptyEvent()
75+
}
76+
6577
// createResourceWindow creates an invisible GLFW window that shares the 'view'
6678
// window's resource context. This window is used to upload resources in the
6779
// background. Must be call after the 'view' window is created.
@@ -124,7 +136,7 @@ func (a *Application) Run() error {
124136
// TODO(drakirus): Delete this when https://github.com/go-gl/glfw/issues/272 is resolved.
125137
// Post an empty event from the main thread before it can happen in a non-main thread,
126138
// to work around https://github.com/glfw/glfw/issues/1649.
127-
glfw.PostEmptyEvent()
139+
postEmptyEvent()
128140
}
129141

130142
if a.config.windowInitialLocation.xpos != 0 {
@@ -204,8 +216,8 @@ func (a *Application) Run() error {
204216

205217
// Create a new eventloop
206218
eventLoop := newEventLoop(
207-
glfw.PostEmptyEvent, // Wakeup GLFW
208-
a.engine.RunTask, // Flush tasks
219+
postEmptyEvent, // Wakeup GLFW
220+
a.engine.RunTask, // Flush tasks
209221
)
210222
// Attach TaskRunner callback functions onto the engine
211223
a.engine.TaskRunnerRunOnCurrentThread = eventLoop.RunOnCurrentThread

messenger.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/go-flutter-desktop/go-flutter/embedder"
99
"github.com/go-flutter-desktop/go-flutter/internal/tasker"
1010
"github.com/go-flutter-desktop/go-flutter/plugin"
11-
"github.com/go-gl/glfw/v3.3/glfw"
1211
)
1312

1413
type messenger struct {
@@ -62,7 +61,7 @@ func (m *messenger) SendWithReply(channel string, binaryMessage []byte) (binaryR
6261
replyErr := make(chan error)
6362
defer close(replyErr)
6463

65-
glfw.PostEmptyEvent()
64+
postEmptyEvent()
6665
go m.engineTasker.Do(func() {
6766
replyErr <- m.engine.SendPlatformMessage(msg)
6867
})
@@ -87,7 +86,7 @@ func (m *messenger) Send(channel string, binaryMessage []byte) (err error) {
8786
replyErr := make(chan error)
8887
defer close(replyErr)
8988

90-
glfw.PostEmptyEvent()
89+
postEmptyEvent()
9190
go m.engineTasker.Do(func() {
9291
replyErr <- m.engine.SendPlatformMessage(msg)
9392
})
@@ -147,7 +146,7 @@ func (r responseSender) Send(binaryReply []byte) {
147146
// TODO: detect multiple responses on the same message and spam the log
148147
// about it.
149148

150-
glfw.PostEmptyEvent()
149+
postEmptyEvent()
151150
go r.engineTasker.Do(func() {
152151
err := r.engine.SendPlatformMessageResponse(r.message.ResponseHandle, binaryReply)
153152
if err != nil {

0 commit comments

Comments
 (0)