Skip to content

Commit

Permalink
Fixed event deadlock for macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
leaanthony committed Feb 2, 2025
1 parent 6bbac4a commit ee24099
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/src/content/docs/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Pass build flags to binding generator by [@fbbdev](https://github.com/fbbdev) in [#4023](https://github.com/wailsapp/wails/pull/4023)
- Change paths in windows Taskfile to forward slashes to ensure it works on non-Windows platforms by [@leaanthony](https://github.com/leaanthony)
- Mac + Mac JS events now fixed by [@leaanthony](https://github.com/leaanthony)
- Fixed event deadlock for macOS by [@leaanthony](https://github.com/leaanthony)

### Changed

Expand Down
18 changes: 11 additions & 7 deletions v3/pkg/application/webview_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"runtime"
"slices"
"strings"
"sync"

Expand Down Expand Up @@ -738,20 +739,21 @@ func (w *WebviewWindow) Center() {
// OnWindowEvent registers a callback for the given window event
func (w *WebviewWindow) OnWindowEvent(eventType events.WindowEventType, callback func(event *WindowEvent)) func() {
eventID := uint(eventType)
w.eventListenersLock.Lock()
defer w.eventListenersLock.Unlock()
windowEventListener := &WindowEventListener{
callback: callback,
}
w.eventListenersLock.Lock()
w.eventListeners[eventID] = append(w.eventListeners[eventID], windowEventListener)
w.eventListenersLock.Unlock()
if w.impl != nil {
w.impl.on(eventID)
}

return func() {
// Check if eventListener is already locked
w.eventListenersLock.Lock()
defer w.eventListenersLock.Unlock()
w.eventListeners[eventID] = lo.Without(w.eventListeners[eventID], windowEventListener)
w.eventListenersLock.Unlock()
}
}

Expand All @@ -773,9 +775,6 @@ func (w *WebviewWindow) RegisterHook(eventType events.WindowEventType, callback
}

func (w *WebviewWindow) HandleWindowEvent(id uint) {
w.eventListenersLock.RLock()
defer w.eventListenersLock.RUnlock()

// Get hooks
w.eventHooksLock.RLock()
hooks := w.eventHooks[id]
Expand All @@ -791,7 +790,12 @@ func (w *WebviewWindow) HandleWindowEvent(id uint) {
}
}

for _, listener := range w.eventListeners[id] {
// Copy the w.eventListeners
w.eventListenersLock.RLock()
var tempListeners = slices.Clone(w.eventListeners[id])
w.eventListenersLock.RUnlock()

for _, listener := range tempListeners {
go func() {
defer handlePanic()
listener.callback(thisEvent)
Expand Down

0 comments on commit ee24099

Please sign in to comment.