| 1 | package main |
| 2 | |
| 3 | import "os" |
| 4 | |
| 5 | // init applies the NVIDIA/Wayland WebKit workaround before the Wails runtime |
| 6 | // initializes WebKitGTK. On KDE Plasma Wayland with NVIDIA GPUs the webview |
| 7 | // crashes due to an upstream WebKit explicit-sync bug (WebKit bugs #280210 and |
| 8 | // #317089). The underlying interaction is between WebKit's ANGLE library and |
| 9 | // NVIDIA's egl-wayland: ANGLE advertises explicit-sync (wp_linux_drm_syncobj) |
| 10 | // but fails to set an acquire point before committing the buffer, which violates |
| 11 | // the Wayland protocol and causes the compositor to disconnect the client. |
| 12 | // |
| 13 | // Setting __NV_DISABLE_EXPLICIT_SYNC=1 is the official NVIDIA EGL API to |
| 14 | // disable the explicit-sync protocol path. It preserves GPU acceleration |
| 15 | // (unlike WEBKIT_DISABLE_DMABUF_RENDERER=1 which disables DMA-BUF entirely |
| 16 | // and severely degrades performance) and keeps the native Wayland session |
| 17 | // (unlike GDK_BACKEND=x11 which falls back to XWayland). |
| 18 | // |
| 19 | // The fix only applies when all three conditions are true: |
| 20 | // 1. Wayland session (WAYLAND_DISPLAY set or XDG_SESSION_TYPE=wayland) |
| 21 | // 2. NVIDIA GPU present (/sys/module/nvidia exists) |
| 22 | // 3. User has not already explicitly set __NV_DISABLE_EXPLICIT_SYNC |
| 23 | func init() { |
| 24 | // Only apply under Wayland — the explicit-sync bug is Wayland-specific. |
| 25 | if os.Getenv("WAYLAND_DISPLAY") == "" && os.Getenv("XDG_SESSION_TYPE") != "wayland" { |
| 26 | return |
| 27 | } |
| 28 | // Only apply when an NVIDIA GPU is present — the env var is NVIDIA-specific. |
| 29 | if !hasNVIDIAGPU() { |
| 30 | return |
| 31 | } |
| 32 | // Respect an explicit user choice so the workaround can be opted out of. |
| 33 | if _, ok := os.LookupEnv("__NV_DISABLE_EXPLICIT_SYNC"); ok { |
| 34 | return |
| 35 | } |
| 36 | os.Setenv("__NV_DISABLE_EXPLICIT_SYNC", "1") |
| 37 | } |
| 38 | |
| 39 | // hasNVIDIAGPU checks whether the NVIDIA kernel module is loaded by looking for |
| 40 | // /sys/module/nvidia. This is the most reliable detection method that works |
| 41 | // across all Linux distributions and doesn't require external tools. |
| 42 | func hasNVIDIAGPU() bool { |
| 43 | _, err := os.Stat("/sys/module/nvidia") |
| 44 | return err == nil |
| 45 | } |
| 46 |