From 7fde80e8050b25df4f0592c0b8d8e25b66b4645d Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Tue, 22 Aug 2023 20:30:51 +0000 Subject: [PATCH] app: [Wayland] avoid a race on the send side of the wakeup pipe Discovered while debugging #528 with -race. References: https://todo.sr.ht/~eliasnaur/gio/528 Signed-off-by: Elias Naur --- app/os_wayland.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/app/os_wayland.go b/app/os_wayland.go index 9d4cb285..159861a6 100644 --- a/app/os_wayland.go +++ b/app/os_wayland.go @@ -94,7 +94,10 @@ type wlDisplay struct { // Notification pipe fds. notify struct { - read, write int + read int + + mu sync.Mutex + write int } repeat repeatState @@ -1442,6 +1445,11 @@ func (w *window) SetAnimating(anim bool) { // Wakeup wakes up the event loop through the notification pipe. func (d *wlDisplay) wakeup() { oneByte := make([]byte, 1) + d.notify.mu.Lock() + defer d.notify.mu.Unlock() + if d.notify.write == 0 { + return + } if _, err := syscall.Write(d.notify.write, oneByte); err != nil && err != syscall.EAGAIN { panic(fmt.Errorf("failed to write to pipe: %v", err)) } @@ -1820,10 +1828,12 @@ func newWLDisplay() (*wlDisplay, error) { } func (d *wlDisplay) destroy() { + d.notify.mu.Lock() if d.notify.write != 0 { syscall.Close(d.notify.write) d.notify.write = 0 } + d.notify.mu.Unlock() if d.notify.read != 0 { syscall.Close(d.notify.read) d.notify.read = 0