detached ad674f5

fix(server): retry wl_event_loop_dispatch interrupted by signals

commit
ad674f5256764b698efd3fe6a0389ac5cc6f81ef
author
brice · · 2026-09-27 18:15 UTC
committer
brice · · 2026-09-27 18:47 UTC
parent
6f1d5dd
signature
GPG VerifiedCD88F9E564E0102646FB6077A1A07C7C7C3E349D · arch-gpg Verified for the committer email
refs
tag: v0.5.0
2 files · +29 -1
M server/display.go +9 -1
@@ -154,6 +154,7 @@ func (d *Display) Run(ctx context.Context) error {
154154 defer runtime.UnlockOSThread()
155155 defer close(d.stopped)
156156 defer d.destroy()
157+ consecutiveFailures := 0
157158 for {
158159 if ctx.Err() != nil {
159160 return nil
@@ -171,8 +172,15 @@ func (d *Display) Run(ctx context.Context) error {
171172 wlDisplayFlushClients(d.c)
172173 // The timeout only bounds how late ctx cancellation is seen.
173174 if wlEventLoopDispatch(d.loop, 100) < 0 {
174- return errors.New("purego-libwayland: wl_event_loop_dispatch failed")
175+ errno := lastErrno()
176+ consecutiveFailures++
177+ // epoll_wait is interrupted by Go's preemption signals (EINTR); a persistent failure (EBADF, EINVAL) repeats.
178+ if consecutiveFailures < 16 {
179+ continue
180+ }
181+ return fmt.Errorf("purego-libwayland: wl_event_loop_dispatch failed %d times in a row: %w", consecutiveFailures, errno)
175182 }
183+ consecutiveFailures = 0
176184 wlDisplayFlushClients(d.c)
177185 }
178186 }
@@ -154,6 +154,7 @@ func (d *Display) Run(ctx context.Context) error {
154 defer runtime.UnlockOSThread()154 defer runtime.UnlockOSThread()
155 defer close(d.stopped)155 defer close(d.stopped)
156 defer d.destroy()156 defer d.destroy()
157+ consecutiveFailures := 0
157 for {158 for {
158 if ctx.Err() != nil {159 if ctx.Err() != nil {
159 return nil160 return nil
@@ -171,8 +172,15 @@ func (d *Display) Run(ctx context.Context) error {
171 wlDisplayFlushClients(d.c)172 wlDisplayFlushClients(d.c)
172 // The timeout only bounds how late ctx cancellation is seen.173 // The timeout only bounds how late ctx cancellation is seen.
173 if wlEventLoopDispatch(d.loop, 100) < 0 {174 if wlEventLoopDispatch(d.loop, 100) < 0 {
174- return errors.New("purego-libwayland: wl_event_loop_dispatch failed")175+ errno := lastErrno()
176+ consecutiveFailures++
177+ // epoll_wait is interrupted by Go's preemption signals (EINTR); a persistent failure (EBADF, EINVAL) repeats.
178+ if consecutiveFailures < 16 {
179+ continue
180+ }
181+ return fmt.Errorf("purego-libwayland: wl_event_loop_dispatch failed %d times in a row: %w", consecutiveFailures, errno)
175 }182 }
183+ consecutiveFailures = 0
176 wlDisplayFlushClients(d.c)184 wlDisplayFlushClients(d.c)
177 }185 }
178 }186 }
M server/lib.go +20 -0
@@ -6,6 +6,7 @@ import (
66 "unsafe"
77
88 "github.com/bnema/purego"
9+ "golang.org/x/sys/unix"
910 )
1011
1112 // libwayland-server entry points. Only non-varargs functions are bound.
@@ -30,6 +31,7 @@ var (
3031
3132 symDisplayFlushClients, symEventLoopDispatch, symResourceCreate uintptr
3233 symResourceSetDispatcher, symResourcePostEventArr, symResourceDestroy, symResourceGetID uintptr
34+ symErrnoLocation uintptr
3335
3436 // Shared C callbacks. purego callbacks are never freed, so there is one
3537 // of each for the whole process.
@@ -82,6 +84,19 @@ func load() error {
8284 return
8385 }
8486 }
87+ symErrnoLocation, err = purego.Dlsym(lib, "__errno_location")
88+ if err != nil {
89+ libc, openErr := purego.Dlopen("libc.so.6", purego.RTLD_NOW|purego.RTLD_GLOBAL)
90+ if openErr != nil {
91+ loadErr = fmt.Errorf("purego-libwayland: open libc.so.6: %w", openErr)
92+ return
93+ }
94+ symErrnoLocation, err = purego.Dlsym(libc, "__errno_location")
95+ if err != nil {
96+ loadErr = fmt.Errorf("purego-libwayland: resolve __errno_location: %w", err)
97+ return
98+ }
99+ }
85100
86101 cbDispatcher = purego.NewCallbackInts(func(a *purego.CallbackArgs) uintptr {
87102 // C memory owned by libwayland; reinterpret pointer bits without uintptr-to-pointer conversion.
@@ -111,6 +126,11 @@ func wlEventLoopDispatch(loop uintptr, timeout int32) int32 {
111126 r, _, _ := purego.Syscall6(symEventLoopDispatch, loop, uintptr(timeout), 0, 0, 0, 0)
112127 return int32(r)
113128 }
129+func lastErrno() unix.Errno {
130+ p, _, _ := purego.Syscall6(symErrnoLocation, 0, 0, 0, 0, 0, 0)
131+ // C memory owned by libc; reinterpret pointer bits without uintptr-to-pointer conversion.
132+ return unix.Errno(**(**int32)(unsafe.Pointer(&p)))
133+}
114134 func wlResourceCreate(client, iface uintptr, version int32, id uint32) uintptr {
115135 r, _, _ := purego.Syscall6(symResourceCreate, client, iface, uintptr(version), uintptr(id), 0, 0)
116136 return r
@@ -6,6 +6,7 @@ import (
6 "unsafe"6 "unsafe"
7 7
8 "github.com/bnema/purego"8 "github.com/bnema/purego"
9+ "golang.org/x/sys/unix"
9 )10 )
10 11
11 // libwayland-server entry points. Only non-varargs functions are bound.12 // libwayland-server entry points. Only non-varargs functions are bound.
@@ -30,6 +31,7 @@ var (
30 31
31 symDisplayFlushClients, symEventLoopDispatch, symResourceCreate uintptr32 symDisplayFlushClients, symEventLoopDispatch, symResourceCreate uintptr
32 symResourceSetDispatcher, symResourcePostEventArr, symResourceDestroy, symResourceGetID uintptr33 symResourceSetDispatcher, symResourcePostEventArr, symResourceDestroy, symResourceGetID uintptr
34+ symErrnoLocation uintptr
33 35
34 // Shared C callbacks. purego callbacks are never freed, so there is one36 // Shared C callbacks. purego callbacks are never freed, so there is one
35 // of each for the whole process.37 // of each for the whole process.
@@ -82,6 +84,19 @@ func load() error {
82 return84 return
83 }85 }
84 }86 }
87+ symErrnoLocation, err = purego.Dlsym(lib, "__errno_location")
88+ if err != nil {
89+ libc, openErr := purego.Dlopen("libc.so.6", purego.RTLD_NOW|purego.RTLD_GLOBAL)
90+ if openErr != nil {
91+ loadErr = fmt.Errorf("purego-libwayland: open libc.so.6: %w", openErr)
92+ return
93+ }
94+ symErrnoLocation, err = purego.Dlsym(libc, "__errno_location")
95+ if err != nil {
96+ loadErr = fmt.Errorf("purego-libwayland: resolve __errno_location: %w", err)
97+ return
98+ }
99+ }
85 100
86 cbDispatcher = purego.NewCallbackInts(func(a *purego.CallbackArgs) uintptr {101 cbDispatcher = purego.NewCallbackInts(func(a *purego.CallbackArgs) uintptr {
87 // C memory owned by libwayland; reinterpret pointer bits without uintptr-to-pointer conversion.102 // C memory owned by libwayland; reinterpret pointer bits without uintptr-to-pointer conversion.
@@ -111,6 +126,11 @@ func wlEventLoopDispatch(loop uintptr, timeout int32) int32 {
111 r, _, _ := purego.Syscall6(symEventLoopDispatch, loop, uintptr(timeout), 0, 0, 0, 0)126 r, _, _ := purego.Syscall6(symEventLoopDispatch, loop, uintptr(timeout), 0, 0, 0, 0)
112 return int32(r)127 return int32(r)
113 }128 }
129+func lastErrno() unix.Errno {
130+ p, _, _ := purego.Syscall6(symErrnoLocation, 0, 0, 0, 0, 0, 0)
131+ // C memory owned by libc; reinterpret pointer bits without uintptr-to-pointer conversion.
132+ return unix.Errno(**(**int32)(unsafe.Pointer(&p)))
133+}
114 func wlResourceCreate(client, iface uintptr, version int32, id uint32) uintptr {134 func wlResourceCreate(client, iface uintptr, version int32, id uint32) uintptr {
115 r, _, _ := purego.Syscall6(symResourceCreate, client, iface, uintptr(version), uintptr(id), 0, 0)135 r, _, _ := purego.Syscall6(symResourceCreate, client, iface, uintptr(version), uintptr(id), 0, 0)
116 return r136 return r

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 14

↑↓ moveenter openesc close