detached c5f3abc

feat(server): add Display.Close for cleanup before Run

commit
c5f3abcfd4719b490ed2dba1c0f8281033e13c4f
author
brice · · 2026-09-23 12:26 UTC
parent
2717424
signature
GPG VerifiedCD88F9E564E0102646FB6077A1A07C7C7C3E349D · arch-gpg Verified for the committer email
2 files · +39 -0
M server/display.go +16 -0
@@ -6,6 +6,7 @@ import (
66 "fmt"
77 "runtime"
88 "strings"
9+ "sync/atomic"
910 "unsafe"
1011
1112 "golang.org/x/sys/unix"
@@ -47,6 +48,7 @@ type Display struct {
4748 loop uintptr
4849 calls chan func()
4950 stopped chan struct{}
51+ state atomic.Uint32
5052 }
5153
5254 func NewDisplay() (*Display, error) {
@@ -79,8 +81,22 @@ func (d *Display) CreateGlobal(iface *Interface, version int32, bindFn BindFunc)
7981 return nil
8082 }
8183
84+// Close destroys a display that has not started Run. It is a no-op once Run
85+// has started; Run owns destruction in that case. Call before Run starts.
86+func (d *Display) Close() {
87+ if !d.state.CompareAndSwap(0, 1) {
88+ return
89+ }
90+ wlDisplayDestroyClients(d.c)
91+ wlDisplayDestroy(d.c)
92+ close(d.stopped)
93+}
94+
8295 // Run dispatches libwayland on one OS-locked goroutine until ctx is done.
8396 func (d *Display) Run(ctx context.Context) error {
97+ if !d.state.CompareAndSwap(0, 2) {
98+ return errors.New("purego-libwayland: display already started or closed")
99+ }
84100 runtime.LockOSThread()
85101 defer runtime.UnlockOSThread()
86102 defer close(d.stopped)
@@ -6,6 +6,7 @@ import (
6 "fmt"6 "fmt"
7 "runtime"7 "runtime"
8 "strings"8 "strings"
9+ "sync/atomic"
9 "unsafe"10 "unsafe"
10 11
11 "golang.org/x/sys/unix"12 "golang.org/x/sys/unix"
@@ -47,6 +48,7 @@ type Display struct {
47 loop uintptr48 loop uintptr
48 calls chan func()49 calls chan func()
49 stopped chan struct{}50 stopped chan struct{}
51+ state atomic.Uint32
50 }52 }
51 53
52 func NewDisplay() (*Display, error) {54 func NewDisplay() (*Display, error) {
@@ -79,8 +81,22 @@ func (d *Display) CreateGlobal(iface *Interface, version int32, bindFn BindFunc)
79 return nil81 return nil
80 }82 }
81 83
84+// Close destroys a display that has not started Run. It is a no-op once Run
85+// has started; Run owns destruction in that case. Call before Run starts.
86+func (d *Display) Close() {
87+ if !d.state.CompareAndSwap(0, 1) {
88+ return
89+ }
90+ wlDisplayDestroyClients(d.c)
91+ wlDisplayDestroy(d.c)
92+ close(d.stopped)
93+}
94+
82 // Run dispatches libwayland on one OS-locked goroutine until ctx is done.95 // Run dispatches libwayland on one OS-locked goroutine until ctx is done.
83 func (d *Display) Run(ctx context.Context) error {96 func (d *Display) Run(ctx context.Context) error {
97+ if !d.state.CompareAndSwap(0, 2) {
98+ return errors.New("purego-libwayland: display already started or closed")
99+ }
84 runtime.LockOSThread()100 runtime.LockOSThread()
85 defer runtime.UnlockOSThread()101 defer runtime.UnlockOSThread()
86 defer close(d.stopped)102 defer close(d.stopped)
M server/display_test.go +23 -0
@@ -111,3 +111,26 @@ func TestDoBlockedDuringCancellation(t *testing.T) {
111111 t.Fatal("blocked Do leaked")
112112 }
113113 }
114+
115+func TestCloseBeforeRun(t *testing.T) {
116+ d, err := NewDisplay()
117+ if err != nil {
118+ t.Fatal(err)
119+ }
120+ fd, err := unix.Socket(unix.AF_UNIX, unix.SOCK_STREAM, 0)
121+ if err != nil {
122+ t.Fatal(err)
123+ }
124+ defer unix.Close(fd)
125+ if err := unix.Bind(fd, &unix.SockaddrUnix{Name: "\x00purego-close-test"}); err != nil {
126+ t.Fatal(err)
127+ }
128+ if err := unix.Listen(fd, 1); err != nil {
129+ t.Fatal(err)
130+ }
131+ if err := d.AddSocketFD(fd); err != nil {
132+ t.Fatal(err)
133+ }
134+ d.Close()
135+ d.Close()
136+}
@@ -111,3 +111,26 @@ func TestDoBlockedDuringCancellation(t *testing.T) {
111 t.Fatal("blocked Do leaked")111 t.Fatal("blocked Do leaked")
112 }112 }
113 }113 }
114+
115+func TestCloseBeforeRun(t *testing.T) {
116+ d, err := NewDisplay()
117+ if err != nil {
118+ t.Fatal(err)
119+ }
120+ fd, err := unix.Socket(unix.AF_UNIX, unix.SOCK_STREAM, 0)
121+ if err != nil {
122+ t.Fatal(err)
123+ }
124+ defer unix.Close(fd)
125+ if err := unix.Bind(fd, &unix.SockaddrUnix{Name: "\x00purego-close-test"}); err != nil {
126+ t.Fatal(err)
127+ }
128+ if err := unix.Listen(fd, 1); err != nil {
129+ t.Fatal(err)
130+ }
131+ if err := d.AddSocketFD(fd); err != nil {
132+ t.Fatal(err)
133+ }
134+ d.Close()
135+ d.Close()
136+}

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