Skip to content

wrap glfw.PostEmptyEvent funcion to prevent panic #579

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

Merged
merged 1 commit into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"runtime"
"runtime/debug"
"time"
"unsafe"

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
})
Expand All @@ -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)
})
Expand Down Expand Up @@ -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 {
Expand Down