Skip to content

Commit

Permalink
internal/ui: bug fix: reentering updateImpl caused double unlocking
Browse files Browse the repository at this point in the history
updateImpl can be invoked in multiple ways. This should have been
protected by a mutex, or this caused unexpected reentrance.

Closes #2339
  • Loading branch information
hajimehoshi committed Sep 20, 2022
1 parent e808f16 commit ef26267
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions internal/ui/ui_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package ui

import (
"sync"
"syscall/js"
"time"

Expand Down Expand Up @@ -84,6 +85,8 @@ type userInterfaceImpl struct {

context *context
input Input

m sync.Mutex
}

func init() {
Expand Down Expand Up @@ -273,6 +276,10 @@ func (u *userInterfaceImpl) update() error {
}

func (u *userInterfaceImpl) updateImpl(force bool) error {
// Guard updateImpl as this function cannot be invoked until this finishes (#2339).
u.m.Lock()
defer u.m.Unlock()

// context can be nil when an event is fired but the loop doesn't start yet (#1928).
if u.context == nil {
return nil
Expand Down Expand Up @@ -482,9 +489,14 @@ func init() {
func setWindowEventHandlers(v js.Value) {
v.Call("addEventListener", "resize", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
theUI.updateScreenSize()
if err := theUI.updateImpl(true); err != nil {
panic(err)
}

// updateImpl can block. Use goroutine.
// See https://pkg.go.dev/syscall/js#FuncOf.
go func() {
if err := theUI.updateImpl(true); err != nil {
panic(err)
}
}()
return nil
}))
}
Expand Down Expand Up @@ -579,7 +591,14 @@ func (u *userInterfaceImpl) forceUpdateOnMinimumFPSMode() {
if u.fpsMode != FPSModeVsyncOffMinimum {
return
}
u.updateImpl(true)

// updateImpl can block. Use goroutine.
// See https://pkg.go.dev/syscall/js#FuncOf.
go func() {
if err := u.updateImpl(true); err != nil {
panic(err)
}
}()
}

func (u *userInterfaceImpl) Run(game Game) error {
Expand Down

0 comments on commit ef26267

Please sign in to comment.