From 1cb346f2dc448e3484301c7232bc4ed4df69f49a Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 4 Nov 2026 04:47:02 +0800 Subject: [PATCH] gui: on Linux the folder and Backups buttons start the opener and return (pkh1585) pkh1585 on Discord: on Linux, clicking the Library's folder / Backups button froze magpie and the file manager that opened closed by itself. Cause: Linux openFolder was Wails' OpenFileManager, which runs the desktop's file manager (the inode/directory .desktop Exec, or xdg-open) with cmd.Run under a 10 second context. A file manager that isn't running yet stays in the foreground, so the click waited the full 10 seconds and then the context killed the window that had just opened. Windows had the same shape and was fixed earlier (folder_windows.go); macOS's `open` returns at once. Fix: proc.OpenFolder (Linux) starts xdg-open (else gio open, exo-open) with no stdio and a session of its own, reaps it in a goroutine, and returns. All callers go through Host.OpenFolder (Library folder, Backups reveal, usage and backup saves, logs/config reveals, browser reveal), so every sibling is reached. macOS keeps Wails' way (folder_other.go is now !linux). Run: - mote-run go test ./internal/proc, wsl-run (magpie-desktop) go test ./internal/proc: ok. New TestOpenFolderDoesNotWaitForTheFileManager: with the Wait made synchronous it fails on WSL (waited 1m0s); with Setsid off it fails (shares magpie's session); green with the fix, -count=3. - Real XFCE (magpie-desktop, DISPLAY :0, real Thunar): the old way (`timeout 10 thunar dir`) blocked 10s and killed Thunar (rc 124, no thunar left); `setsid xdg-open dir` returned at once and Thunar stayed up 12s later. - gofmt clean; go vet ./internal/proc for linux, windows (mote), linux (wsl); go vet ./internal/gui on macOS. Not tried: a built Linux magpie GUI clicking the real button (WSL has no gtk4/webkitgtk-6.0, so internal/gui can't build there, and linux gui vet wasn't run); Omarchy/Hyprland; AppImage env (LD_LIBRARY_PATH) leaking into the opener, which is unchanged; the full go test ./... suite and GUI tests (no GUI or JS file changed). --- internal/gui/folder_linux.go | 12 +++++ internal/gui/folder_other.go | 2 +- internal/proc/folder_linux.go | 50 +++++++++++++++++++++ internal/proc/folder_linux_test.go | 72 ++++++++++++++++++++++++++++++ 4 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 internal/gui/folder_linux.go create mode 100644 internal/proc/folder_linux.go create mode 100644 internal/proc/folder_linux_test.go diff --git a/internal/gui/folder_linux.go b/internal/gui/folder_linux.go new file mode 100644 index 000000000..921cc925b --- /dev/null +++ b/internal/gui/folder_linux.go @@ -0,0 +1,12 @@ +//go:build linux && !nogui + +package gui + +import ( + "github.com/wailsapp/wails/v3/pkg/application" + "github.com/yetone/magpie/internal/proc" +) + +// openFolder starts the desktop's opener and leaves the file manager to open; +// Wails' OpenFileManager waits for it and kills it after ten seconds. +func openFolder(_ *application.App, path string) error { return proc.OpenFolder(path) } diff --git a/internal/gui/folder_other.go b/internal/gui/folder_other.go index 1f0a2cbfe..1da92b805 100644 --- a/internal/gui/folder_other.go +++ b/internal/gui/folder_other.go @@ -1,4 +1,4 @@ -//go:build !windows && !nogui +//go:build !windows && !linux && !nogui package gui diff --git a/internal/proc/folder_linux.go b/internal/proc/folder_linux.go new file mode 100644 index 000000000..1b41cf99c --- /dev/null +++ b/internal/proc/folder_linux.go @@ -0,0 +1,50 @@ +package proc + +import ( + "errors" + "os" + "os/exec" + "path/filepath" + "syscall" +) + +// OpenFolder shows path in the desktop's file manager and returns once the +// opener is started, not when the file manager is done. Wails' own way runs +// the opener to its end under a ten-second context: a file manager that +// isn't running yet stays in the foreground of what xdg-open starts, so the +// click hung for ten seconds and then killed the window that had just opened +// (pkh1585 on Discord). The opener gets a session of its own and no stdio, +// so it outlives the request and the click. +func OpenFolder(path string) error { + st, err := os.Stat(path) + if err != nil { + return err + } + if !st.IsDir() { + path = filepath.Dir(path) + } + var last error + for _, c := range [][]string{{"xdg-open"}, {"gio", "open"}, {"exo-open"}} { + bin, err := exec.LookPath(c[0]) + if err != nil { + last = err + continue + } + cmd := Command(bin, append(c[1:], path)...) + cmd.Stdin, cmd.Stdout, cmd.Stderr = nil, nil, nil + if cmd.SysProcAttr == nil { + cmd.SysProcAttr = &syscall.SysProcAttr{} + } + cmd.SysProcAttr.Setsid = true + if err := cmd.Start(); err != nil { + last = err + continue + } + go cmd.Wait() //nolint:errcheck + return nil + } + if last == nil { + last = errors.New("no opener") + } + return last +} diff --git a/internal/proc/folder_linux_test.go b/internal/proc/folder_linux_test.go new file mode 100644 index 000000000..ea569725d --- /dev/null +++ b/internal/proc/folder_linux_test.go @@ -0,0 +1,72 @@ +package proc + +import ( + "os" + "path/filepath" + "strconv" + "strings" + "syscall" + "testing" + "time" +) + +// A file manager that stays in the foreground (a first window) must not hold +// the call, and must live on after it returns. +func TestOpenFolderDoesNotWaitForTheFileManager(t *testing.T) { + bin, dir, pidf := t.TempDir(), t.TempDir(), filepath.Join(t.TempDir(), "pid") + script := "#!/bin/sh\necho $$ > " + pidf + "\necho \"$1\" > " + pidf + ".arg\nexec sleep 60\n" + if err := os.WriteFile(filepath.Join(bin, "xdg-open"), []byte(script), 0o755); err != nil { + t.Fatal(err) + } + t.Setenv("PATH", bin+":/bin:/usr/bin") + start := time.Now() + if err := OpenFolder(dir); err != nil { + t.Fatal(err) + } + if d := time.Since(start); d > 5*time.Second { + t.Fatalf("OpenFolder waited %v for the file manager", d) + } + var pid int + for i := 0; i < 500 && pid == 0; i++ { + if b, err := os.ReadFile(pidf); err == nil && strings.Contains(string(b), "\n") { + pid, _ = strconv.Atoi(strings.TrimSpace(string(b))) + } else { + time.Sleep(10 * time.Millisecond) + } + } + if pid == 0 { + t.Fatal("the opener never ran") + } + t.Cleanup(func() { syscall.Kill(pid, syscall.SIGKILL) }) + if sid, err := sidOf(pid); err != nil || sid == mustSid() { + t.Fatalf("the opener shares magpie's session: %d %v", sid, err) + } + if err := syscall.Kill(pid, 0); err != nil { + t.Fatalf("the opener is gone: %v", err) + } + if b, _ := os.ReadFile(pidf + ".arg"); strings.TrimSpace(string(b)) != dir { + t.Fatalf("opened %q, want %q", b, dir) + } +} + +func mustSid() int { s, _ := sidOf(os.Getpid()); return s } + +// sidOf is the session id, the sixth field after the command in /proc/pid/stat. +func sidOf(pid int) (int, error) { + b, err := os.ReadFile("/proc/" + strconv.Itoa(pid) + "/stat") + if err != nil { + return 0, err + } + f := strings.Fields(string(b[strings.LastIndex(string(b), ")")+1:])) + return strconv.Atoi(f[3]) +} + +func TestOpenFolderNeedsAFolderAndAnOpener(t *testing.T) { + t.Setenv("PATH", t.TempDir()) + if err := OpenFolder(t.TempDir()); err == nil { + t.Fatal("no opener on PATH should be an error") + } + if err := OpenFolder(filepath.Join(t.TempDir(), "gone")); err == nil { + t.Fatal("a missing folder should be an error") + } +}