From c179aa3059d022aaee2cb010df3450cdbc299933 Mon Sep 17 00:00:00 2001 From: pchampio Date: Mon, 15 Mar 2021 11:47:48 +0100 Subject: [PATCH] wrap glfw.PostEmptyEvent funcion to prevent panic --- application.go | 18 +++++++++++++++--- messenger.go | 7 +++---- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/application.go b/application.go index 14422e7f..22f7dea5 100644 --- a/application.go +++ b/application.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "runtime" + "runtime/debug" "time" "unsafe" @@ -62,6 +63,17 @@ func NewApplication(opt ...Option) *Application { return app } +func postEmptyEvent() { + defer func() { + p := recover() + if p != nil { + fmt.Printf("go-flutter: recovered from panic 'glfw.PostEmptyEvent()': %v\n", p) + debug.PrintStack() + } + }() + glfw.PostEmptyEvent() +} + // createResourceWindow creates an invisible GLFW window that shares the 'view' // window's resource context. This window is used to upload resources in the // background. Must be call after the 'view' window is created. @@ -123,7 +135,7 @@ func (a *Application) Run() error { // TODO(drakirus): Delete this when https://github.com/go-gl/glfw/issues/272 is resolved. // Post an empty event from the main thread before it can happen in a non-main thread, // to work around https://github.com/glfw/glfw/issues/1649. - glfw.PostEmptyEvent() + postEmptyEvent() } if a.config.windowInitialLocation.xpos != 0 { @@ -203,8 +215,8 @@ func (a *Application) Run() error { // Create a new eventloop eventLoop := newEventLoop( - glfw.PostEmptyEvent, // Wakeup GLFW - a.engine.RunTask, // Flush tasks + postEmptyEvent, // Wakeup GLFW + a.engine.RunTask, // Flush tasks ) // Attach TaskRunner callback functions onto the engine a.engine.TaskRunnerRunOnCurrentThread = eventLoop.RunOnCurrentThread diff --git a/messenger.go b/messenger.go index aef87874..f9dee9f6 100644 --- a/messenger.go +++ b/messenger.go @@ -8,7 +8,6 @@ import ( "github.com/go-flutter-desktop/go-flutter/embedder" "github.com/go-flutter-desktop/go-flutter/internal/tasker" "github.com/go-flutter-desktop/go-flutter/plugin" - "github.com/go-gl/glfw/v3.3/glfw" ) type messenger struct { @@ -62,7 +61,7 @@ func (m *messenger) SendWithReply(channel string, binaryMessage []byte) (binaryR replyErr := make(chan error) defer close(replyErr) - glfw.PostEmptyEvent() + postEmptyEvent() go m.engineTasker.Do(func() { replyErr <- m.engine.SendPlatformMessage(msg) }) @@ -87,7 +86,7 @@ func (m *messenger) Send(channel string, binaryMessage []byte) (err error) { replyErr := make(chan error) defer close(replyErr) - glfw.PostEmptyEvent() + postEmptyEvent() go m.engineTasker.Do(func() { replyErr <- m.engine.SendPlatformMessage(msg) }) @@ -147,7 +146,7 @@ func (r responseSender) Send(binaryReply []byte) { // TODO: detect multiple responses on the same message and spam the log // about it. - glfw.PostEmptyEvent() + postEmptyEvent() go r.engineTasker.Do(func() { err := r.engine.SendPlatformMessageResponse(r.message.ResponseHandle, binaryReply) if err != nil {