dannisun0412 runs magpie on KDE Plasma 6.6.6 (kwin_wayland, libwayland 1.24) and it quit three times in twelve minutes with "Error 22 (Invalid argument) dispatching to Wayland display" / "Error reading events from display: Invalid argument". The cause is the clipboard read-back probe (clipboard_gtk3.h). It made its own wl_data_device and, when done, called wl_data_device_destroy, which only frees the client side. The compositor still thinks the device is live and keeps sending it a fresh wl_data_offer on every later copy. libwayland drops those events for the dead proxy without reserving their new ids, so the client's id map falls behind the server's; the next server-created object whose id lands past the gap makes wl_map_reserve_new return EINVAL and GDK exits. The probe now binds the data device manager at version 3 at most and releases the device with wl_data_device_release (version 2+), so the compositor stops sending to it. On a version-1 manager it doesn't read back at all (returns -2, which the Go side takes as "copy held"). The probe test (testdata/clipboard_probe.c) gets an "again" mode: copy and read back eight times, then make the compositor hand out four offers at once (the "burst" that needs ids past the leak), then check the process is still alive. Run: - Fedora 44 container with kwin_wayland --virtual (SYS_NICE), gtk3/webkit2gtk-4.1, go 1.26.9: with origin/main's header the probe died with EINVAL 6 of 6 times (primary selection on and off) and TestClipboardReadBack failed 3 of 3 with "Error reading events from display: Invalid argument"; with this change the probe stayed alive 6 of 6 and TestClipboardReadBack passed 3 of 3. GOOS=linux go vet with the gtk3 build in the container was clean. - macOS: gofmt -l clean; go vet darwin, linux nogui, windows nogui; go test -tags nogui ./internal/gui passed. Not tried: a real Plasma desktop or the full app over Selkies.
99 lines
3.3 KiB
Go
99 lines
3.3 KiB
Go
//go:build linux && cgo && gtk3 && !nogui
|
|
|
|
package gui
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// TestClipboardReadBack builds testdata/clipboard_probe.c with the copy and
|
|
// the read back the app runs (clipboard_gtk3.h) and copies in a real GTK 3
|
|
// window on the Wayland compositor the test runs under. magpie's copy is
|
|
// read back whole. Wails' copy (CLIPBOARD, then PRIMARY) is read back empty
|
|
// where the compositor has no primary selection, as on #1881's KDE Plasma
|
|
// with middle-click paste off, and the read back then says no copy was
|
|
// made. It needs a Wayland session that gives a new window the focus
|
|
// (KWin, with kwinrc [Wayland] EnablePrimarySelection=false for the
|
|
// second case).
|
|
func TestClipboardReadBack(t *testing.T) {
|
|
if os.Getenv("WAYLAND_DISPLAY") == "" {
|
|
t.Skip("needs a Wayland session")
|
|
}
|
|
flags, err := exec.Command("pkg-config", "--cflags", "--libs", "gtk+-3.0", "wayland-client").Output()
|
|
if err != nil {
|
|
t.Skip("no GTK 3 / wayland-client headers:", err)
|
|
}
|
|
here, _ := filepath.Abs(".")
|
|
bin := filepath.Join(t.TempDir(), "probe")
|
|
args := append([]string{"testdata/clipboard_probe.c", "-I" + here, "-o", bin}, strings.Fields(string(flags))...)
|
|
if out, err := exec.Command("cc", args...).CombinedOutput(); err != nil {
|
|
t.Fatalf("build probe: %v\n%s", err, out)
|
|
}
|
|
run := func(t *testing.T, mode, text string) (primary, read int, held string, out string) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
cmd := exec.CommandContext(ctx, bin, mode, text)
|
|
cmd.Env = append(os.Environ(), "GDK_BACKEND=wayland")
|
|
b, err := cmd.CombinedOutput()
|
|
out = string(b)
|
|
t.Logf("%s: %s", mode, out)
|
|
if err != nil {
|
|
t.Fatalf("probe: %v", err)
|
|
}
|
|
primary, read = -2, -2
|
|
for _, l := range strings.Split(out, "\n") {
|
|
fmt.Sscanf(l, "primary=%d", &primary)
|
|
if strings.HasPrefix(l, "read=") {
|
|
var n int
|
|
fmt.Sscanf(l, "read=%d len=%d", &read, &n)
|
|
if i := strings.Index(l, " text="); i >= 0 {
|
|
held = l[i+len(" text="):]
|
|
}
|
|
}
|
|
}
|
|
if read == -1 {
|
|
t.Skip("the compositor sent the probe no selection (no focus?)")
|
|
}
|
|
return primary, read, held, out
|
|
}
|
|
text := "claude --resume 1881 «剪贴板»"
|
|
t.Run("magpie", func(t *testing.T) {
|
|
_, read, held, _ := run(t, "magpie", text)
|
|
if !copyHeld(read, held, text) {
|
|
t.Errorf("read back %d %q, want %q", read, held, text)
|
|
}
|
|
})
|
|
t.Run("wails", func(t *testing.T) {
|
|
primary, read, held, _ := run(t, "wails", text)
|
|
if primary != 0 {
|
|
t.Skip("the compositor has a primary selection: Wails' copy works there")
|
|
}
|
|
if read != 1 || held != "" {
|
|
t.Errorf("read back %d %q, want 1 and an empty text (GTK closing the pipe)", read, held)
|
|
}
|
|
if copyHeld(read, held, text) {
|
|
t.Error("an empty read back counts as a copy made")
|
|
}
|
|
})
|
|
// #2039: a read back that left its data device alive in the
|
|
// compositor broke GTK's connection on the next clipboard changes
|
|
// ("Error reading events from display: Invalid argument") and the app
|
|
// exited.
|
|
t.Run("copies after a read back", func(t *testing.T) {
|
|
_, read, held, out := run(t, "again", text)
|
|
if !copyHeld(read, held, text) {
|
|
t.Errorf("read back %d %q, want %q", read, held, text)
|
|
}
|
|
if !strings.Contains(out, "\nalive") {
|
|
t.Errorf("the probe died after more copies:\n%s", out)
|
|
}
|
|
})
|
|
}
|