From 15a185957bc5c9b8ecce5e87317fd3ab8bb1d50d Mon Sep 17 00:00:00 2001 From: Maheswar Kumar <110882203+mahiatlinux@users.noreply.github.com> Date: Sat, 15 Aug 2026 11:59:59 +1200 Subject: [PATCH 01/11] desktop: disable the webkitgtk dmabuf renderer on nvidia, not only on wayland WebKit's renderer selection has no session branch: AcceleratedBackingStore::rendererBufferTransportMode reads WEBKIT_DISABLE_DMABUF_RENDERER and WEBKIT_DMABUF_RENDERER_FORCE_SHM and picks the hardware transport on X11 the same way it does on Wayland, so an X11 session on the proprietary driver got no workaround. configure_linux_renderer now probes /proc/driver/nvidia/version before GTK initialization and applies the workaround on either display server, matching WebKit's own isNVIDIA fix for bug 262607. nouveau publishes nothing there and stays on the defaults. --- studio/src-tauri/src/linux_webkit.rs | 94 +++++++++++++++++++++++----- studio/src-tauri/src/main.rs | 10 ++- 2 files changed, 84 insertions(+), 20 deletions(-) diff --git a/studio/src-tauri/src/linux_webkit.rs b/studio/src-tauri/src/linux_webkit.rs index 1e7f44b3aab6..e5d04fcba75c 100644 --- a/studio/src-tauri/src/linux_webkit.rs +++ b/studio/src-tauri/src/linux_webkit.rs @@ -8,6 +8,11 @@ const GDK_BACKEND: &str = "GDK_BACKEND"; const FORCE_SHARED_MEMORY: &str = "WEBKIT_DMABUF_RENDERER_FORCE_SHM"; const DISABLE_DMABUF: &str = "WEBKIT_DISABLE_DMABUF_RENDERER"; const FORCE_SHARED_MEMORY_MIN_VERSION: (u32, u32) = (2, 44); +// both the proprietary and open nvidia modules publish this; nouveau does not and is unaffected +const NVIDIA_DRIVER_VERSION_PATH: &str = "/proc/driver/nvidia/version"; + +const NVIDIA_REASON: &str = "NVIDIA driver loaded"; +const WAYLAND_REASON: &str = "Wayland session"; #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum RenderingWorkaround { @@ -26,7 +31,8 @@ impl RenderingWorkaround { #[derive(Debug, PartialEq, Eq)] enum RenderingPlan { - Apply(RenderingWorkaround), + /// The workaround to apply, and why, for the startup log. + Apply(RenderingWorkaround, &'static str), PreserveEnvironment, } @@ -44,6 +50,7 @@ fn supports_force_shared_memory((major, minor, _micro): (u32, u32, u32)) -> bool fn rendering_plan( env: impl Fn(&str) -> Option, webkit_version: (u32, u32, u32), + nvidia_driver_loaded: bool, ) -> RenderingPlan { // Presence is an operator override, including `=0` and an empty value. Do // not combine the modern shared-memory switch with legacy instructions a @@ -52,6 +59,11 @@ fn rendering_plan( return RenderingPlan::PreserveEnvironment; } + // webkit's isNVIDIA fix (bug 262607) is session-independent, so x11 needs this too + if nvidia_driver_loaded { + return RenderingPlan::Apply(RenderingWorkaround::DisableDmabuf, NVIDIA_REASON); + } + let configured_backends = env(GDK_BACKEND); let explicit_wayland = configured_backends .as_deref() @@ -79,7 +91,11 @@ fn rendering_plan( // it, so use the renderer-disable workaround supported by 2.42. RenderingWorkaround::DisableDmabuf }; - RenderingPlan::Apply(workaround) + RenderingPlan::Apply(workaround, WAYLAND_REASON) +} + +fn nvidia_driver_loaded() -> bool { + std::path::Path::new(NVIDIA_DRIVER_VERSION_PATH).exists() } fn runtime_webkit_version() -> (u32, u32, u32) { @@ -96,13 +112,17 @@ fn runtime_webkit_version() -> (u32, u32, u32) { /// Select a compatible WebKitGTK rendering transport before GTK initialization. /// -/// Returns the variable applied by this launch, if any. -pub fn configure_wayland_renderer() -> Option<&'static str> { - match rendering_plan(|name| std::env::var_os(name), runtime_webkit_version()) { - RenderingPlan::Apply(workaround) => { +/// Returns the variable applied by this launch and the reason for it, if any. +pub fn configure_linux_renderer() -> Option<(&'static str, &'static str)> { + match rendering_plan( + |name| std::env::var_os(name), + runtime_webkit_version(), + nvidia_driver_loaded(), + ) { + RenderingPlan::Apply(workaround, reason) => { let variable = workaround.variable(); std::env::set_var(variable, "1"); - Some(variable) + Some((variable, reason)) } RenderingPlan::PreserveEnvironment => None, } @@ -114,7 +134,11 @@ mod tests { const MODERN_WEBKIT: (u32, u32, u32) = (2, 44, 0); - fn plan_with_version(vars: &[(&str, &str)], webkit_version: (u32, u32, u32)) -> RenderingPlan { + fn plan_on_host( + vars: &[(&str, &str)], + webkit_version: (u32, u32, u32), + nvidia_driver_loaded: bool, + ) -> RenderingPlan { rendering_plan( |name| { vars.iter() @@ -122,18 +146,27 @@ mod tests { .map(|(_, value)| OsString::from(value)) }, webkit_version, + nvidia_driver_loaded, ) } + fn plan_with_version(vars: &[(&str, &str)], webkit_version: (u32, u32, u32)) -> RenderingPlan { + plan_on_host(vars, webkit_version, false) + } + fn plan(vars: &[(&str, &str)]) -> RenderingPlan { plan_with_version(vars, MODERN_WEBKIT) } + fn plan_on_nvidia(vars: &[(&str, &str)]) -> RenderingPlan { + plan_on_host(vars, MODERN_WEBKIT, true) + } + #[test] fn wayland_uses_shared_memory_transport() { assert_eq!( plan(&[(WAYLAND_DISPLAY, "wayland-0")]), - RenderingPlan::Apply(RenderingWorkaround::ForceSharedMemory) + RenderingPlan::Apply(RenderingWorkaround::ForceSharedMemory, WAYLAND_REASON) ); } @@ -141,15 +174,48 @@ mod tests { fn explicit_wayland_without_a_display_variable_still_applies() { assert_eq!( plan(&[(GDK_BACKEND, "wayland")]), - RenderingPlan::Apply(RenderingWorkaround::ForceSharedMemory) + RenderingPlan::Apply(RenderingWorkaround::ForceSharedMemory, WAYLAND_REASON) ); } #[test] - fn x11_session_keeps_webkit_defaults() { + fn an_x11_session_off_nvidia_keeps_webkit_defaults() { assert_eq!(plan(&[]), RenderingPlan::PreserveEnvironment); } + #[test] + fn nvidia_on_x11_disables_the_dmabuf_renderer() { + assert_eq!( + plan_on_nvidia(&[]), + RenderingPlan::Apply(RenderingWorkaround::DisableDmabuf, NVIDIA_REASON) + ); + } + + #[test] + fn nvidia_under_an_explicit_x11_backend_still_applies() { + assert_eq!( + plan_on_nvidia(&[(GDK_BACKEND, "x11")]), + RenderingPlan::Apply(RenderingWorkaround::DisableDmabuf, NVIDIA_REASON) + ); + } + + #[test] + fn nvidia_on_wayland_takes_the_stronger_switch_over_shared_memory() { + // isNVIDIA returns before SharedMemory is added, so FORCE_SHM is not equivalent + assert_eq!( + plan_on_nvidia(&[(WAYLAND_DISPLAY, "wayland-0")]), + RenderingPlan::Apply(RenderingWorkaround::DisableDmabuf, NVIDIA_REASON) + ); + } + + #[test] + fn an_operator_override_outranks_the_nvidia_default() { + assert_eq!( + plan_on_nvidia(&[(FORCE_SHARED_MEMORY, "0")]), + RenderingPlan::PreserveEnvironment + ); + } + #[test] fn an_empty_wayland_display_is_not_a_wayland_session() { assert_eq!( @@ -170,7 +236,7 @@ mod tests { fn a_gdk_wildcard_can_select_wayland_without_a_display_variable() { assert_eq!( plan(&[(GDK_BACKEND, "*")]), - RenderingPlan::Apply(RenderingWorkaround::ForceSharedMemory) + RenderingPlan::Apply(RenderingWorkaround::ForceSharedMemory, WAYLAND_REASON) ); } @@ -178,7 +244,7 @@ mod tests { fn old_webkit_uses_the_legacy_renderer_switch() { assert_eq!( plan_with_version(&[(WAYLAND_DISPLAY, "wayland-0")], (2, 42, 7)), - RenderingPlan::Apply(RenderingWorkaround::DisableDmabuf) + RenderingPlan::Apply(RenderingWorkaround::DisableDmabuf, WAYLAND_REASON) ); } @@ -186,7 +252,7 @@ mod tests { fn webkit_244_uses_force_shm() { assert_eq!( plan_with_version(&[(WAYLAND_DISPLAY, "wayland-0")], (2, 44, 0)), - RenderingPlan::Apply(RenderingWorkaround::ForceSharedMemory) + RenderingPlan::Apply(RenderingWorkaround::ForceSharedMemory, WAYLAND_REASON) ); } diff --git a/studio/src-tauri/src/main.rs b/studio/src-tauri/src/main.rs index 6faea846fb04..73dbc46bb8cd 100644 --- a/studio/src-tauri/src/main.rs +++ b/studio/src-tauri/src/main.rs @@ -1768,11 +1768,9 @@ fn main() { // process drives X from several threads. See x11_threads for the crash. x11_threads::init_x11_threads(); - // WebKitGTK's hardware dmabuf path can violate Wayland explicit-sync - // protocol on current NVIDIA/Mesa stacks. Select a compatible fallback - // before any GTK/WebKit object can be initialized. + // webkitgtk's dmabuf path breaks on nvidia on either display server; pick a fallback pre-gtk #[cfg(target_os = "linux")] - let webkit_rendering_workaround = linux_webkit::configure_wayland_renderer(); + let webkit_rendering_workaround = linux_webkit::configure_linux_renderer(); // Fix PATH for GUI apps (macOS .app bundles, Linux AppImage, Windows) // GUI apps don't inherit shell dotfile PATH — this spawns the user's // login shell to source .zshrc/.bashrc/.profile and sets PATH properly. @@ -1782,8 +1780,8 @@ fn main() { info!("Unsloth desktop app starting"); #[cfg(target_os = "linux")] - if let Some(variable) = webkit_rendering_workaround { - info!("Wayland detected; set {variable}=1 for WebKitGTK compatibility"); + if let Some((variable, reason)) = webkit_rendering_workaround { + info!("{reason}; set {variable}=1 for WebKitGTK compatibility"); } windows_job::initialize(); From 69781084c1f29d2fe69cf0f8d760a6665c3faba6 Mon Sep 17 00:00:00 2001 From: wasimysaid Date: Mon, 17 Aug 2026 02:46:03 +0200 Subject: [PATCH 02/11] desktop: replace inherited force-shm on nvidia relaunch --- studio/src-tauri/src/linux_webkit.rs | 42 +++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/studio/src-tauri/src/linux_webkit.rs b/studio/src-tauri/src/linux_webkit.rs index e5d04fcba75c..a2991f71fd57 100644 --- a/studio/src-tauri/src/linux_webkit.rs +++ b/studio/src-tauri/src/linux_webkit.rs @@ -52,18 +52,32 @@ fn rendering_plan( webkit_version: (u32, u32, u32), nvidia_driver_loaded: bool, ) -> RenderingPlan { - // Presence is an operator override, including `=0` and an empty value. Do - // not combine the modern shared-memory switch with legacy instructions a - // user may already carry in a launcher or environment.d file. - if env(FORCE_SHARED_MEMORY).is_some() || env(DISABLE_DMABUF).is_some() { + // DISABLE_DMABUF is the explicit operator override, including `=0` and an + // empty value. It also disambiguates a deliberate FORCE_SHM=1 on NVIDIA. + if env(DISABLE_DMABUF).is_some() { return RenderingPlan::PreserveEnvironment; } + let force_shared_memory = env(FORCE_SHARED_MEMORY); + // webkit's isNVIDIA fix (bug 262607) is session-independent, so x11 needs this too if nvidia_driver_loaded { + // Previous Wayland builds injected exactly FORCE_SHM=1. Tauri relaunches + // inherit it, so treating that lone value as an override leaves the first + // post-update process on the old workaround. Other values remain operator + // overrides; use DISABLE_DMABUF=0 alongside FORCE_SHM=1 to preserve an + // intentional shared-memory choice on NVIDIA. + if force_shared_memory.is_some() && force_shared_memory.as_deref() != Some(OsStr::new("1")) + { + return RenderingPlan::PreserveEnvironment; + } return RenderingPlan::Apply(RenderingWorkaround::DisableDmabuf, NVIDIA_REASON); } + if force_shared_memory.is_some() { + return RenderingPlan::PreserveEnvironment; + } + let configured_backends = env(GDK_BACKEND); let explicit_wayland = configured_backends .as_deref() @@ -121,6 +135,10 @@ pub fn configure_linux_renderer() -> Option<(&'static str, &'static str)> { ) { RenderingPlan::Apply(workaround, reason) => { let variable = workaround.variable(); + + if workaround == RenderingWorkaround::DisableDmabuf { + std::env::remove_var(FORCE_SHARED_MEMORY); + } std::env::set_var(variable, "1"); Some((variable, reason)) } @@ -208,6 +226,22 @@ mod tests { ); } + #[test] + fn nvidia_relaunch_replaces_inherited_force_shm() { + assert_eq!( + plan_on_nvidia(&[(WAYLAND_DISPLAY, "wayland-0"), (FORCE_SHARED_MEMORY, "1"),]), + RenderingPlan::Apply(RenderingWorkaround::DisableDmabuf, NVIDIA_REASON) + ); + } + + #[test] + fn explicit_disable_override_preserves_force_shm_on_nvidia() { + assert_eq!( + plan_on_nvidia(&[(FORCE_SHARED_MEMORY, "1"), (DISABLE_DMABUF, "0"),]), + RenderingPlan::PreserveEnvironment + ); + } + #[test] fn an_operator_override_outranks_the_nvidia_default() { assert_eq!( From 94d2c954bae41db92a621afb3709f1c9dc355bba Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Thu, 20 Aug 2026 18:36:10 -0300 Subject: [PATCH 03/11] Ignore force-shm on nvidia and record the display server in the startup log --- studio/src-tauri/src/linux_webkit.rs | 163 +++++++++++++++------------ studio/src-tauri/src/main.rs | 4 +- 2 files changed, 94 insertions(+), 73 deletions(-) diff --git a/studio/src-tauri/src/linux_webkit.rs b/studio/src-tauri/src/linux_webkit.rs index 88d80872beda..a8a5a414fd55 100644 --- a/studio/src-tauri/src/linux_webkit.rs +++ b/studio/src-tauri/src/linux_webkit.rs @@ -14,7 +14,8 @@ const FORCE_SHARED_MEMORY_MIN_VERSION: (u32, u32) = (2, 44); // both the proprietary and open nvidia modules publish this; nouveau does not and is unaffected const NVIDIA_DRIVER_VERSION_PATH: &str = "/proc/driver/nvidia/version"; -const NVIDIA_REASON: &str = "NVIDIA driver loaded"; +const NVIDIA_REASON: &str = "NVIDIA driver loaded (no Wayland session)"; +const NVIDIA_WAYLAND_REASON: &str = "NVIDIA driver loaded (Wayland session)"; const WAYLAND_REASON: &str = "Wayland session"; const APPIMAGE_GLES_REASON: &str = "AppImage without a usable GLES library"; @@ -57,55 +58,73 @@ fn rendering_plan( gles_usable: bool, nvidia_driver_loaded: bool, ) -> RenderingPlan { - // DISABLE_DMABUF is the explicit operator override, including `=0` and an - // empty value. It also disambiguates a deliberate FORCE_SHM=1 on NVIDIA. + // Presence is an operator override, `=0` and an empty value included. It is also the + // only variable WebKit accepts as "keep the DMA-BUF renderer", so on NVIDIA it is how + // an operator asks for shared memory instead of the empty transport set below. if env(DISABLE_DMABUF).is_some() { return RenderingPlan::PreserveEnvironment; } - let force_shared_memory = env(FORCE_SHARED_MEMORY); - - // webkit's isNVIDIA fix (bug 262607) is session-independent, so x11 needs this too + let is_appimage = env(APPIMAGE).is_some(); + let configured_backends = env(GDK_BACKEND); + let explicit_wayland = configured_backends + .as_deref() + .map(backend_allows_wayland) + .unwrap_or(false); + // GDK can connect to the default wayland-0 socket when its backend is explicitly + // Wayland even if WAYLAND_DISPLAY is absent. Conversely, WAYLAND_DISPLAY is inherited + // by XWayland apps, so an X11-only GDK_BACKEND must take precedence. + let wayland_session = explicit_wayland + || (configured_backends.is_none() + && env(WAYLAND_DISPLAY) + .map(|display| !display.is_empty()) + .unwrap_or(false)); + + // The DMA-BUF transport breaks on the proprietary NVIDIA driver on either display + // server, so this cannot be gated on a Wayland session. Upstream declined the fix + // (bug 262607 is WONTFIX, WebKit PR 18614 closed unmerged), so Debian and Ubuntu + // carry disable-nvidia-dmabuf.patch while Fedora, Arch and the upstream tarballs do + // not. Where it is present it already empties the transport set on NVIDIA by itself: + // isNVIDIA() sits before mode.add(SharedMemory) in 2.50.4 and 2.52.6, and 2.53.90 + // moved it after, leaving {SharedMemory}. So this branch is what protects the + // unpatched hosts, and on 2.54 it will be stronger than the library's own choice. + // + // Stronger, because DISABLE_DMABUF is not a transport tweak. WebKit reads it before + // the SharedMemory add, so the set stays empty, checkRequirements() is false and + // accelerated compositing is off for the process. FORCE_SHM leaves compositing on and + // only degrades the buffer transport (#9393). The heavier switch is used anyway + // because it is the one with reports of fixing this crash. + // + // The probe is kernel-module presence, not the GPU that will render. A PRIME or + // Optimus laptop presenting on the integrated GPU reads as NVIDIA here and takes a + // workaround it does not need. That is deliberate: reading the rendering GPU needs a + // GL context, and this runs before GTK is initialized precisely so that no GL state + // exists yet. Those hosts opt out with WEBKIT_DISABLE_DMABUF_RENDERER=0. if nvidia_driver_loaded { - // Previous Wayland builds injected exactly FORCE_SHM=1. Tauri relaunches - // inherit it, so treating that lone value as an override leaves the first - // post-update process on the old workaround. Other values remain operator - // overrides; use DISABLE_DMABUF=0 alongside FORCE_SHM=1 to preserve an - // intentional shared-memory choice on NVIDIA. - if force_shared_memory.is_some() && force_shared_memory.as_deref() != Some(OsStr::new("1")) - { - return RenderingPlan::PreserveEnvironment; - } - return RenderingPlan::Apply(RenderingWorkaround::DisableDmabuf, NVIDIA_REASON); - } - - if force_shared_memory.is_some() { + let reason = if wayland_session { + NVIDIA_WAYLAND_REASON + } else { + NVIDIA_REASON + }; + return RenderingPlan::Apply(RenderingWorkaround::DisableDmabuf, reason); + } + + // Presence is an operator override off NVIDIA, `=0` and an empty value included: do + // not combine the modern shared-memory switch with legacy instructions a user may + // already carry in a launcher or environment.d file. On NVIDIA it is not consulted at + // all, because neither value states an intent worth honouring there. WebKit ignores + // `=0` outright (`forceSHM && g_strcmp0(forceSHM, "0")`), and `=1` is what a previous + // Wayland build set, which a Tauri relaunch inherits. + if env(FORCE_SHARED_MEMORY).is_some() { return RenderingPlan::PreserveEnvironment; } - let is_appimage = env(APPIMAGE).is_some(); // libepoxy loads GLES at runtime; disabling DMA-BUF handles a missing copy (#8343). if is_appimage && !gles_usable { return RenderingPlan::Apply(RenderingWorkaround::DisableDmabuf, APPIMAGE_GLES_REASON); } - let configured_backends = env(GDK_BACKEND); - let explicit_wayland = configured_backends - .as_deref() - .map(backend_allows_wayland) - .unwrap_or(false); - let wayland_display = env(WAYLAND_DISPLAY) - .map(|display| !display.is_empty()) - .unwrap_or(false); - - // GDK can connect to the default wayland-0 socket when its backend is - // explicitly Wayland even if WAYLAND_DISPLAY is absent. Conversely, - // WAYLAND_DISPLAY is inherited by XWayland apps, so an X11-only GDK_BACKEND - // must take precedence. - if configured_backends.is_some() && !explicit_wayland { - return RenderingPlan::PreserveEnvironment; - } - if !wayland_display && !explicit_wayland { + if !wayland_session { return RenderingPlan::PreserveEnvironment; } @@ -164,10 +183,6 @@ pub fn configure_renderer() -> Option<(&'static str, &'static str)> { ) { RenderingPlan::Apply(workaround, reason) => { let variable = workaround.variable(); - - if workaround == RenderingWorkaround::DisableDmabuf { - std::env::remove_var(FORCE_SHARED_MEMORY); - } std::env::set_var(variable, "1"); Some((variable, reason)) } @@ -258,37 +273,65 @@ mod tests { #[test] fn nvidia_on_wayland_takes_the_stronger_switch_over_shared_memory() { - // isNVIDIA returns before SharedMemory is added, so FORCE_SHM is not equivalent assert_eq!( plan_on_nvidia(&[(WAYLAND_DISPLAY, "wayland-0")]), - RenderingPlan::Apply(RenderingWorkaround::DisableDmabuf, NVIDIA_REASON) + RenderingPlan::Apply(RenderingWorkaround::DisableDmabuf, NVIDIA_WAYLAND_REASON) ); } + #[test] + fn no_force_shm_value_suppresses_the_nvidia_workaround() { + // WebKit reads `=0` as unset and `=1` is what a previous build left behind, so + // neither is an operator override. DISABLE_DMABUF=0 is the one that is. + for value in ["0", "1", "", "true"] { + assert_eq!( + plan_on_nvidia(&[(FORCE_SHARED_MEMORY, value)]), + RenderingPlan::Apply(RenderingWorkaround::DisableDmabuf, NVIDIA_REASON), + "FORCE_SHM={value} must not suppress the NVIDIA workaround" + ); + } + } + #[test] fn nvidia_relaunch_replaces_inherited_force_shm() { assert_eq!( - plan_on_nvidia(&[(WAYLAND_DISPLAY, "wayland-0"), (FORCE_SHARED_MEMORY, "1"),]), - RenderingPlan::Apply(RenderingWorkaround::DisableDmabuf, NVIDIA_REASON) + plan_on_nvidia(&[(WAYLAND_DISPLAY, "wayland-0"), (FORCE_SHARED_MEMORY, "1")]), + RenderingPlan::Apply(RenderingWorkaround::DisableDmabuf, NVIDIA_WAYLAND_REASON) ); } #[test] fn explicit_disable_override_preserves_force_shm_on_nvidia() { assert_eq!( - plan_on_nvidia(&[(FORCE_SHARED_MEMORY, "1"), (DISABLE_DMABUF, "0"),]), + plan_on_nvidia(&[(FORCE_SHARED_MEMORY, "1"), (DISABLE_DMABUF, "0")]), + RenderingPlan::PreserveEnvironment + ); + } + + #[test] + fn an_empty_wayland_display_is_not_a_wayland_session() { + assert_eq!( + plan(&[(WAYLAND_DISPLAY, "")]), RenderingPlan::PreserveEnvironment ); } #[test] - fn an_operator_override_outranks_the_nvidia_default() { + fn an_explicit_x11_gdk_backend_wins() { assert_eq!( - plan_on_nvidia(&[(FORCE_SHARED_MEMORY, "0")]), + plan(&[(WAYLAND_DISPLAY, "wayland-0"), (GDK_BACKEND, "x11")]), RenderingPlan::PreserveEnvironment ); } + #[test] + fn a_gdk_wildcard_can_select_wayland_without_a_display_variable() { + assert_eq!( + plan(&[(GDK_BACKEND, "*")]), + RenderingPlan::Apply(RenderingWorkaround::ForceSharedMemory, WAYLAND_REASON) + ); + } + #[test] fn appimage_without_usable_gles_disables_dmabuf_on_x11() { assert_eq!( @@ -320,30 +363,6 @@ mod tests { ); } - #[test] - fn an_empty_wayland_display_is_not_a_wayland_session() { - assert_eq!( - plan(&[(WAYLAND_DISPLAY, "")]), - RenderingPlan::PreserveEnvironment - ); - } - - #[test] - fn an_explicit_x11_gdk_backend_wins() { - assert_eq!( - plan(&[(WAYLAND_DISPLAY, "wayland-0"), (GDK_BACKEND, "x11")]), - RenderingPlan::PreserveEnvironment - ); - } - - #[test] - fn a_gdk_wildcard_can_select_wayland_without_a_display_variable() { - assert_eq!( - plan(&[(GDK_BACKEND, "*")]), - RenderingPlan::Apply(RenderingWorkaround::ForceSharedMemory, WAYLAND_REASON) - ); - } - #[test] fn old_webkit_uses_the_legacy_renderer_switch() { assert_eq!( diff --git a/studio/src-tauri/src/main.rs b/studio/src-tauri/src/main.rs index 28f465d31e8d..1778cc35c48e 100644 --- a/studio/src-tauri/src/main.rs +++ b/studio/src-tauri/src/main.rs @@ -1803,7 +1803,9 @@ fn main() { // process drives X from several threads. See x11_threads for the crash. x11_threads::init_x11_threads(); - // webkitgtk's dmabuf path breaks on nvidia on either display server; pick a fallback pre-gtk + // WebKitGTK's hardware dmabuf path breaks on the proprietary NVIDIA driver on + // either display server, and on an AppImage that cannot load GLES. Select a + // compatible fallback before any GTK/WebKit object can be initialized. #[cfg(target_os = "linux")] let webkit_rendering_workaround = linux_webkit::configure_renderer(); // Fix PATH for GUI apps (macOS .app bundles, Linux AppImage, Windows) From aec95dcd4aa0ecfb6e52bbf78db39ae99f396781 Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Mon, 24 Aug 2026 05:18:48 +0000 Subject: [PATCH 04/11] Pick the renderer workaround per failure mode and restore the operator overrides Three things, all found while checking this branch against the WebKit sources rather than against descriptions of them. ## The two switches are not interchangeable, and X11 wants the lighter one Traced in webkitgtk-2.50.4, the version the AppImage bundles: DISABLE_DMABUF returns before mode.add(SharedMemory), so the set stays empty, checkRequirements() is false, AcceleratedBackingStore::create() returns nullptr and webkitWebViewBaseEnterAcceleratedCompositingMode() dereferences that behind an ASSERT that release builds compile out. FORCE_SHM returns after the add and leaves a valid backing store. That is not only a smoothness question. block/buzz#3654 reports a SIGSEGV on Ubuntu 24.04, WebKitGTK 2.52.3, X11, NVIDIA module loaded but EGL on Mesa, with DISABLE_DMABUF, and no crash with FORCE_SHM. That is exactly the iGPU-presenting topology the module probe over-triggers on. So the branch now picks per failure mode rather than per GPU. Wayland keeps DISABLE_DMABUF: the failure there is the explicit-sync disconnect, FORCE_SHM routes every commit down the wl_shm path that trips it (WebKit bug 315436), and DISABLE_DMABUF is the switch with reports of fixing Error 71. X11 on 2.44+ takes FORCE_SHM, which drops the hardware transport that fails there without emptying the set. Old WebKitGTK and the missing-GLES AppImage keep the stronger switch. ## Operator overrides win again, without pinning a stale decision Ignoring every FORCE_SHM value on NVIDIA also discarded a deliberate setting, and it was the setting that avoids the crash above. Both variables are operator overrides again. The relaunch case that motivated ignoring them is handled by naming the variable we set in UNSLOTH_WEBKIT_RENDERER_WORKAROUND, so a launch can tell its own inherited output from an instruction, re-decide, and clear the previous variable when the new decision differs. Without that a launch reads its own output back and the first decision is pinned for the life of the process tree; a new test asserts the plan is a fixed point instead. WEBKIT_FORCE_DMABUF_RENDERER is honoured too. It is the opt-out disable-nvidia-dmabuf.patch ships for itself, checked inside isNVIDIA(), which WebKit never reaches once DISABLE_DMABUF is set, so this branch was silently outranking the only lever a Debian or Ubuntu user has. It stands the NVIDIA branch down and nothing else: the missing-GLES fallback answers a packaging failure, not a GPU policy, and that launch cannot render without it. ## Diagnostics An NVIDIA AppImage that cannot load GLES reported only NVIDIA and hid the packaging defect. It now reports both. Two harness docstrings still said this module forces SHM on Wayland; both switches and both display servers are now in scope, so they say so. 19 tests to 27; the whole crate is 415 passing. Verified against the extracted Ubuntu 2.50.4 and Debian 2.52.6 patches (isNVIDIA() before the SharedMemory add, empty set) and 2.53.91 (after it, {SharedMemory}). A differential over 4860 host, session, packaging, inherited-environment and version combinations shows no host losing a workaround it had, and no operator override ignored. --- studio/src-tauri/src/linux_webkit.rs | 271 ++++++++++++++++++--- tests/studio/playwright_heavy_thread.py | 9 +- tests/studio/playwright_research_freeze.py | 6 +- 3 files changed, 247 insertions(+), 39 deletions(-) diff --git a/studio/src-tauri/src/linux_webkit.rs b/studio/src-tauri/src/linux_webkit.rs index a8a5a414fd55..56472db2d94c 100644 --- a/studio/src-tauri/src/linux_webkit.rs +++ b/studio/src-tauri/src/linux_webkit.rs @@ -10,12 +10,23 @@ const WAYLAND_DISPLAY: &str = "WAYLAND_DISPLAY"; const GDK_BACKEND: &str = "GDK_BACKEND"; const FORCE_SHARED_MEMORY: &str = "WEBKIT_DMABUF_RENDERER_FORCE_SHM"; const DISABLE_DMABUF: &str = "WEBKIT_DISABLE_DMABUF_RENDERER"; +// disable-nvidia-dmabuf.patch reads this first inside isNVIDIA() and stands the +// distribution workaround down for it, so on Debian and Ubuntu it is the only way +// a user can ask for the hardware transport back. WebKit reads DISABLE_DMABUF +// before it ever calls isNVIDIA(), so anything set here outranks it silently. +const FORCE_DMABUF: &str = "WEBKIT_FORCE_DMABUF_RENDERER"; +// Written next to the workaround so a relaunch can tell state this application +// wrote from a value the operator set. Tauri's process::restart spawns without +// env_clear, so the replacement inherits both. WebKit never reads this. +const APPLIED_WORKAROUND: &str = "UNSLOTH_WEBKIT_RENDERER_WORKAROUND"; const FORCE_SHARED_MEMORY_MIN_VERSION: (u32, u32) = (2, 44); // both the proprietary and open nvidia modules publish this; nouveau does not and is unaffected const NVIDIA_DRIVER_VERSION_PATH: &str = "/proc/driver/nvidia/version"; const NVIDIA_REASON: &str = "NVIDIA driver loaded (no Wayland session)"; const NVIDIA_WAYLAND_REASON: &str = "NVIDIA driver loaded (Wayland session)"; +const NVIDIA_APPIMAGE_GLES_REASON: &str = + "NVIDIA driver loaded; AppImage without a usable GLES library"; const WAYLAND_REASON: &str = "Wayland session"; const APPIMAGE_GLES_REASON: &str = "AppImage without a usable GLES library"; @@ -52,16 +63,51 @@ fn supports_force_shared_memory((major, minor, _micro): (u32, u32, u32)) -> bool (major, minor) >= FORCE_SHARED_MEMORY_MIN_VERSION } +/// Match `forceDMABuf && *forceDMABuf != '0'` from disable-nvidia-dmabuf.patch: +/// the first byte only, so an empty value asks for the hardware transport and +/// `0` does not. +fn force_dmabuf_requested(value: &OsStr) -> bool { + value.as_encoded_bytes().first() != Some(&b'0') +} + fn rendering_plan( env: impl Fn(&str) -> Option, webkit_version: (u32, u32, u32), gles_usable: bool, nvidia_driver_loaded: bool, ) -> RenderingPlan { - // Presence is an operator override, `=0` and an empty value included. It is also the - // only variable WebKit accepts as "keep the DMA-BUF renderer", so on NVIDIA it is how - // an operator asks for shared memory instead of the empty transport set below. - if env(DISABLE_DMABUF).is_some() { + // Either renderer variable is an operator override when it is present, `=0` and an + // empty value included, unless the marker says this application wrote it for an + // earlier launch of the same process tree. Tauri's process::restart inherits the + // environment, so without the marker a launch reads its own output back as an + // instruction and the first decision is pinned for the life of the process tree, + // even after the display server or the library under it changes. + // + // DISABLE_DMABUF=0 is how a host the probe below over-triggers on asks for its + // accelerated path back: WebKit reads it as unset (`g_strcmp0(v, "0")`), and on a + // patched distribution the library's own GL_VENDOR probe then declines on an + // iGPU-presenting host, so the hardware transport really is restored. An empty + // value is not that request; WebKit reads it as set. + let applied_by_this_app = env(APPLIED_WORKAROUND); + let ours = |variable: &str| applied_by_this_app.as_deref() == Some(OsStr::new(variable)); + + if env(DISABLE_DMABUF).is_some() && !ours(DISABLE_DMABUF) { + return RenderingPlan::PreserveEnvironment; + } + + // The distribution patch's own opt-out. It has to be read here, because WebKit + // reads DISABLE_DMABUF before isNVIDIA() and so would never reach it once a + // workaround is applied below. It speaks only to the NVIDIA question the patch + // asks, so it stands the NVIDIA branch down and nothing else; the missing-GLES + // fallback below is a packaging failure, not a GPU policy, and an operator asking + // for the hardware transport back is not asking for a launch that cannot render. + let force_dmabuf_requested_by_operator = env(FORCE_DMABUF) + .as_deref() + .is_some_and(force_dmabuf_requested); + + // Same rule for the shared-memory switch: do not combine it with legacy + // instructions a user may already carry in a launcher or environment.d file. + if env(FORCE_SHARED_MEMORY).is_some() && !ours(FORCE_SHARED_MEMORY) { return RenderingPlan::PreserveEnvironment; } @@ -86,37 +132,59 @@ fn rendering_plan( // carry disable-nvidia-dmabuf.patch while Fedora, Arch and the upstream tarballs do // not. Where it is present it already empties the transport set on NVIDIA by itself: // isNVIDIA() sits before mode.add(SharedMemory) in 2.50.4 and 2.52.6, and 2.53.90 - // moved it after, leaving {SharedMemory}. So this branch is what protects the - // unpatched hosts, and on 2.54 it will be stronger than the library's own choice. + // moved it after, leaving {SharedMemory}. + // + // Both shipped artifacts run a patched library, though: the .deb resolves the host + // one and the AppImage bundles Ubuntu 22.04's. So on those the branch below matters + // where the library's GL_VENDOR probe and this module probe disagree, and it also + // skips the GBM device open and throwaway GL context isNVIDIA() performs at startup. + // Building against an unpatched system library is the case the branch covers outright. // - // Stronger, because DISABLE_DMABUF is not a transport tweak. WebKit reads it before - // the SharedMemory add, so the set stays empty, checkRequirements() is false and - // accelerated compositing is off for the process. FORCE_SHM leaves compositing on and - // only degrades the buffer transport (#9393). The heavier switch is used anyway - // because it is the one with reports of fixing this crash. + // The two switches are not interchangeable. DISABLE_DMABUF is read before the + // SharedMemory add, so the set stays empty, checkRequirements() is false, + // AcceleratedBackingStore::create() returns nullptr, and + // webkitWebViewBaseEnterAcceleratedCompositingMode() dereferences that behind an + // ASSERT that release builds compile out. FORCE_SHM is read after the add, leaves + // {SharedMemory}, and keeps a valid backing store and accelerated compositing. + // + // So the choice is per failure mode rather than per GPU: + // Wayland DISABLE_DMABUF. The failure there is the explicit-sync protocol + // disconnect, and FORCE_SHM routes every commit down the wl_shm path + // that trips it (WebKit bug 315436). It is also the switch with + // reports of fixing Error 71. + // X11 FORCE_SHM where the runtime supports it. X11 has no explicit-sync + // protocol, the failure there is hardware DMA-BUF allocation, and + // dropping to shared memory fixes that without the empty set. + // The empty set is not merely slower on hosts where the library's own probe does + // not fire: block/buzz#3654 reports a SIGSEGV on NVIDIA X11 with DISABLE_DMABUF + // that FORCE_SHM does not reproduce, on exactly the iGPU-presenting topology the + // probe below over-triggers on. // // The probe is kernel-module presence, not the GPU that will render. A PRIME or // Optimus laptop presenting on the integrated GPU reads as NVIDIA here and takes a // workaround it does not need. That is deliberate: reading the rendering GPU needs a // GL context, and this runs before GTK is initialized precisely so that no GL state // exists yet. Those hosts opt out with WEBKIT_DISABLE_DMABUF_RENDERER=0. - if nvidia_driver_loaded { - let reason = if wayland_session { + if nvidia_driver_loaded && !force_dmabuf_requested_by_operator { + let missing_appimage_gles = is_appimage && !gles_usable; + let reason = if missing_appimage_gles { + NVIDIA_APPIMAGE_GLES_REASON + } else if wayland_session { NVIDIA_WAYLAND_REASON } else { NVIDIA_REASON }; - return RenderingPlan::Apply(RenderingWorkaround::DisableDmabuf, reason); - } - - // Presence is an operator override off NVIDIA, `=0` and an empty value included: do - // not combine the modern shared-memory switch with legacy instructions a user may - // already carry in a launcher or environment.d file. On NVIDIA it is not consulted at - // all, because neither value states an intent worth honouring there. WebKit ignores - // `=0` outright (`forceSHM && g_strcmp0(forceSHM, "0")`), and `=1` is what a previous - // Wayland build set, which a Tauri relaunch inherits. - if env(FORCE_SHARED_MEMORY).is_some() { - return RenderingPlan::PreserveEnvironment; + // FORCE_SHM still reaches the failing libepoxy path in AppImages, and WebKitGTK + // before 2.44 ignores it outright, so both keep the stronger switch. + let workaround = if wayland_session + || missing_appimage_gles + || !supports_force_shared_memory(webkit_version) + { + RenderingWorkaround::DisableDmabuf + } else { + RenderingWorkaround::ForceSharedMemory + }; + return RenderingPlan::Apply(workaround, reason); } // libepoxy loads GLES at runtime; disabling DMA-BUF handles a missing copy (#8343). @@ -183,10 +251,27 @@ pub fn configure_renderer() -> Option<(&'static str, &'static str)> { ) { RenderingPlan::Apply(workaround, reason) => { let variable = workaround.variable(); + // A relaunch that re-decides the other way has inherited the previous + // choice. WebKit reads DISABLE_DMABUF before FORCE_SHM, so leaving the old + // one set would silently outrank the new one. Only a variable this + // application claimed is cleared; an operator's value never reaches here. + if let Some(previous) = std::env::var_os(APPLIED_WORKAROUND) { + if previous != OsStr::new(variable) { + std::env::remove_var(previous); + } + } std::env::set_var(variable, "1"); + // Claim it, so the next launch re-decides instead of reading it back as an + // operator override. + std::env::set_var(APPLIED_WORKAROUND, variable); Some((variable, reason)) } - RenderingPlan::PreserveEnvironment => None, + RenderingPlan::PreserveEnvironment => { + // Nothing here is ours any more; a stale claim would suppress an operator + // override on the next relaunch. + std::env::remove_var(APPLIED_WORKAROUND); + None + } } } @@ -256,10 +341,12 @@ mod tests { } #[test] - fn nvidia_on_x11_disables_the_dmabuf_renderer() { + fn nvidia_on_x11_drops_to_shared_memory_rather_than_the_empty_transport_set() { + // X11 has no explicit-sync protocol to violate, and DISABLE_DMABUF leaves a null + // backing store that release builds dereference (block/buzz#3654). assert_eq!( plan_on_nvidia(&[]), - RenderingPlan::Apply(RenderingWorkaround::DisableDmabuf, NVIDIA_REASON) + RenderingPlan::Apply(RenderingWorkaround::ForceSharedMemory, NVIDIA_REASON) ); } @@ -267,10 +354,82 @@ mod tests { fn nvidia_under_an_explicit_x11_backend_still_applies() { assert_eq!( plan_on_nvidia(&[(GDK_BACKEND, "x11")]), + RenderingPlan::Apply(RenderingWorkaround::ForceSharedMemory, NVIDIA_REASON) + ); + } + + #[test] + fn nvidia_on_x11_below_244_keeps_the_legacy_renderer_switch() { + // FORCE_SHM arrived in 2.44; older libraries ignore it entirely. + assert_eq!( + plan_on_host(&[], (2, 42, 7), true, true), RenderingPlan::Apply(RenderingWorkaround::DisableDmabuf, NVIDIA_REASON) ); } + #[test] + fn nvidia_appimage_without_gles_reports_both_causes() { + assert_eq!( + plan_on_host( + &[(APPIMAGE, "/tmp/Unsloth.AppImage")], + MODERN_WEBKIT, + false, + true + ), + RenderingPlan::Apply( + RenderingWorkaround::DisableDmabuf, + NVIDIA_APPIMAGE_GLES_REASON + ) + ); + } + + #[test] + fn the_distribution_force_dmabuf_opt_out_outranks_the_nvidia_default() { + // disable-nvidia-dmabuf.patch checks this inside isNVIDIA(), which WebKit never + // reaches once DISABLE_DMABUF is set, so it has to be honoured here instead. + for value in ["1", "", "yes"] { + assert_eq!( + plan_on_nvidia(&[(FORCE_DMABUF, value)]), + RenderingPlan::PreserveEnvironment, + "WEBKIT_FORCE_DMABUF_RENDERER={value} must stand the workaround down" + ); + } + } + + #[test] + fn force_dmabuf_does_not_defeat_the_missing_gles_fallback() { + // That fallback answers a packaging failure, not the NVIDIA question this + // variable asks. Without it the AppImage cannot render at all (#8343). + assert_eq!( + plan_on_host( + &[(FORCE_DMABUF, "1"), (APPIMAGE, "/tmp/Unsloth.AppImage")], + MODERN_WEBKIT, + false, + true + ), + RenderingPlan::Apply(RenderingWorkaround::DisableDmabuf, APPIMAGE_GLES_REASON) + ); + } + + #[test] + fn force_dmabuf_still_leaves_the_wayland_workaround_in_place() { + // The variable speaks to the NVIDIA patch, not to the Wayland transport rule + // that predates it. + assert_eq!( + plan_on_nvidia(&[(FORCE_DMABUF, "1"), (WAYLAND_DISPLAY, "wayland-0")]), + RenderingPlan::Apply(RenderingWorkaround::ForceSharedMemory, WAYLAND_REASON) + ); + } + + #[test] + fn a_zero_force_dmabuf_value_does_not_stand_the_workaround_down() { + // The patch tests the first byte against '0', so "0" is not a request. + assert_eq!( + plan_on_nvidia(&[(FORCE_DMABUF, "0")]), + RenderingPlan::Apply(RenderingWorkaround::ForceSharedMemory, NVIDIA_REASON) + ); + } + #[test] fn nvidia_on_wayland_takes_the_stronger_switch_over_shared_memory() { assert_eq!( @@ -280,26 +439,70 @@ mod tests { } #[test] - fn no_force_shm_value_suppresses_the_nvidia_workaround() { - // WebKit reads `=0` as unset and `=1` is what a previous build left behind, so - // neither is an operator override. DISABLE_DMABUF=0 is the one that is. + fn an_operator_force_shm_value_is_preserved_on_nvidia_too() { + // Unmarked, so it came from a launcher or environment.d rather than from us. for value in ["0", "1", "", "true"] { assert_eq!( plan_on_nvidia(&[(FORCE_SHARED_MEMORY, value)]), - RenderingPlan::Apply(RenderingWorkaround::DisableDmabuf, NVIDIA_REASON), - "FORCE_SHM={value} must not suppress the NVIDIA workaround" + RenderingPlan::PreserveEnvironment, + "operator FORCE_SHM={value} must not be overridden on NVIDIA" ); } } #[test] - fn nvidia_relaunch_replaces_inherited_force_shm() { + fn nvidia_relaunch_re_decides_over_its_own_inherited_force_shm() { + // Marked, so it is this application's own state from an earlier launch. On + // Wayland the re-decision escalates it rather than inheriting it. assert_eq!( - plan_on_nvidia(&[(WAYLAND_DISPLAY, "wayland-0"), (FORCE_SHARED_MEMORY, "1")]), + plan_on_nvidia(&[ + (WAYLAND_DISPLAY, "wayland-0"), + (FORCE_SHARED_MEMORY, "1"), + (APPLIED_WORKAROUND, FORCE_SHARED_MEMORY), + ]), RenderingPlan::Apply(RenderingWorkaround::DisableDmabuf, NVIDIA_WAYLAND_REASON) ); } + #[test] + fn a_relaunch_re_reaches_the_same_decision_it_made_first_time() { + // Tauri's process::restart inherits the environment, so the plan has to be a + // fixed point: feed a launch's own output back in and it must not drift. + let hosts = [ + (&[][..], true), + (&[(WAYLAND_DISPLAY, "wayland-0")][..], true), + (&[(GDK_BACKEND, "x11")][..], true), + (&[][..], false), + (&[(WAYLAND_DISPLAY, "wayland-0")][..], false), + ]; + for (session, nvidia) in hosts { + let first = plan_on_host(session, MODERN_WEBKIT, true, nvidia); + let RenderingPlan::Apply(workaround, reason) = first else { + continue; + }; + let mut inherited = session.to_vec(); + inherited.push((workaround.variable(), "1")); + inherited.push((APPLIED_WORKAROUND, workaround.variable())); + assert_eq!( + plan_on_host(&inherited, MODERN_WEBKIT, true, nvidia), + RenderingPlan::Apply(workaround, reason), + "relaunch drifted for session {session:?} nvidia={nvidia}" + ); + } + } + + #[test] + fn a_marker_for_the_other_variable_does_not_release_force_shm() { + // Only a claim naming FORCE_SHM identifies FORCE_SHM as ours. + assert_eq!( + plan_on_nvidia(&[ + (FORCE_SHARED_MEMORY, "1"), + (APPLIED_WORKAROUND, DISABLE_DMABUF), + ]), + RenderingPlan::PreserveEnvironment + ); + } + #[test] fn explicit_disable_override_preserves_force_shm_on_nvidia() { assert_eq!( diff --git a/tests/studio/playwright_heavy_thread.py b/tests/studio/playwright_heavy_thread.py index a0e5e0d434f0..8f561dd79921 100644 --- a/tests/studio/playwright_heavy_thread.py +++ b/tests/studio/playwright_heavy_thread.py @@ -60,9 +60,12 @@ Playwright, driven headless, with no Tauri IPC layer and no WebKitGTK compositor. Read it as "JavaScriptCore plus WebKit layout", not as "Unsloth Desktop on macOS". -Desktop Linux under Wayland is WORSE than any number this file can produce, and not by a little: -studio/src-tauri/src/linux_webkit.rs sets WEBKIT_DMABUF_RENDERER_FORCE_SHM=1, which forces the -software rendering transport. Everything below runs on a normal compositor path. +Desktop Linux is WORSE than any number this file can produce, and not by a little: +studio/src-tauri/src/linux_webkit.rs drops the webview off the hardware DMA-BUF transport on +Wayland and on NVIDIA under either display server. It forces the shared-memory transport with +WEBKIT_DMABUF_RENDERER_FORCE_SHM=1, or, on Wayland and on old WebKitGTK, disables the renderer +outright with WEBKIT_DISABLE_DMABUF_RENDERER=1, which turns accelerated compositing off for the +whole process. Everything below runs on a normal compositor path. THIS HARNESS MEASURES, IT DOES NOT GATE. It prints the table and exits 0 on any timing. It exits non-zero only when the harness itself is broken: the seed did not land, an element it drives went diff --git a/tests/studio/playwright_research_freeze.py b/tests/studio/playwright_research_freeze.py index e7ed9dd7bc95..20e9db3a7ef0 100644 --- a/tests/studio/playwright_research_freeze.py +++ b/tests/studio/playwright_research_freeze.py @@ -7,8 +7,10 @@ again while closing the research detail pane -- spinner stopped, nothing clickable, force quit. What this measures, and what it cannot: Chromium is not the WebKitGTK webview the desktop app -embeds on Linux, and `studio/src-tauri/src/linux_webkit.rs` forces that webview onto the SHM -software renderer on Wayland, where the frame budget is far tighter than anything measured here. +embeds on Linux, and `studio/src-tauri/src/linux_webkit.rs` takes that webview off the hardware +DMA-BUF transport on Wayland and on NVIDIA under either display server -- onto shared memory, or +off accelerated compositing entirely -- where the frame budget is far tighter than anything +measured here. So an absolute pass here does not prove the reporter's machine is fixed. What transfers is the *work*: long tasks, forced layouts and style recalcs during the stream, and whether the window still takes clicks afterwards. Those are the quantities the fixes move. From 4318b70d7d9800b42107bba328b2d53793c6f5a9 Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Mon, 24 Aug 2026 05:31:04 +0000 Subject: [PATCH 05/11] Tighten the linux_webkit comments for PR #8884 Same decisions, fewer lines: 94 comment lines out, 58 back. Verified behaviour preserving by comparing the plan and the reason string against the previous commit over 420000 input combinations, 0 differences. 415 crate tests pass, rustfmt clean. --- studio/src-tauri/src/linux_webkit.rs | 152 ++++++++++----------------- 1 file changed, 58 insertions(+), 94 deletions(-) diff --git a/studio/src-tauri/src/linux_webkit.rs b/studio/src-tauri/src/linux_webkit.rs index 56472db2d94c..78534b0f8a1a 100644 --- a/studio/src-tauri/src/linux_webkit.rs +++ b/studio/src-tauri/src/linux_webkit.rs @@ -10,14 +10,11 @@ const WAYLAND_DISPLAY: &str = "WAYLAND_DISPLAY"; const GDK_BACKEND: &str = "GDK_BACKEND"; const FORCE_SHARED_MEMORY: &str = "WEBKIT_DMABUF_RENDERER_FORCE_SHM"; const DISABLE_DMABUF: &str = "WEBKIT_DISABLE_DMABUF_RENDERER"; -// disable-nvidia-dmabuf.patch reads this first inside isNVIDIA() and stands the -// distribution workaround down for it, so on Debian and Ubuntu it is the only way -// a user can ask for the hardware transport back. WebKit reads DISABLE_DMABUF -// before it ever calls isNVIDIA(), so anything set here outranks it silently. +// disable-nvidia-dmabuf.patch's own opt-out, read inside isNVIDIA(). WebKit returns on +// DISABLE_DMABUF first, so it never gets there unless we honour it ourselves. const FORCE_DMABUF: &str = "WEBKIT_FORCE_DMABUF_RENDERER"; -// Written next to the workaround so a relaunch can tell state this application -// wrote from a value the operator set. Tauri's process::restart spawns without -// env_clear, so the replacement inherits both. WebKit never reads this. +// Names the variable we set, so a relaunch tells our own inherited output from an +// operator's value. Tauri's process::restart does not env_clear. WebKit never reads it. const APPLIED_WORKAROUND: &str = "UNSLOTH_WEBKIT_RENDERER_WORKAROUND"; const FORCE_SHARED_MEMORY_MIN_VERSION: (u32, u32) = (2, 44); // both the proprietary and open nvidia modules publish this; nouveau does not and is unaffected @@ -63,9 +60,8 @@ fn supports_force_shared_memory((major, minor, _micro): (u32, u32, u32)) -> bool (major, minor) >= FORCE_SHARED_MEMORY_MIN_VERSION } -/// Match `forceDMABuf && *forceDMABuf != '0'` from disable-nvidia-dmabuf.patch: -/// the first byte only, so an empty value asks for the hardware transport and -/// `0` does not. +/// `forceDMABuf && *forceDMABuf != '0'` from disable-nvidia-dmabuf.patch: first byte +/// only, so an empty value is a request and `0` is not. fn force_dmabuf_requested(value: &OsStr) -> bool { value.as_encoded_bytes().first() != Some(&b'0') } @@ -76,41 +72,30 @@ fn rendering_plan( gles_usable: bool, nvidia_driver_loaded: bool, ) -> RenderingPlan { - // Either renderer variable is an operator override when it is present, `=0` and an - // empty value included, unless the marker says this application wrote it for an - // earlier launch of the same process tree. Tauri's process::restart inherits the - // environment, so without the marker a launch reads its own output back as an - // instruction and the first decision is pinned for the life of the process tree, - // even after the display server or the library under it changes. + // Either renderer variable is an operator override when present, `=0` and an empty + // value included, unless the marker says we wrote it. Without that test a launch + // reads its own inherited output back as an instruction and the first decision is + // pinned for the life of the process tree, even after the display server changes. // - // DISABLE_DMABUF=0 is how a host the probe below over-triggers on asks for its - // accelerated path back: WebKit reads it as unset (`g_strcmp0(v, "0")`), and on a - // patched distribution the library's own GL_VENDOR probe then declines on an - // iGPU-presenting host, so the hardware transport really is restored. An empty - // value is not that request; WebKit reads it as set. + // DISABLE_DMABUF=0 is how a host this over-triggers on gets its accelerated path + // back: WebKit reads it as unset (`g_strcmp0(v, "0")`), and a patched library's own + // GL_VENDOR probe then declines on an iGPU. An empty value is not that request. let applied_by_this_app = env(APPLIED_WORKAROUND); let ours = |variable: &str| applied_by_this_app.as_deref() == Some(OsStr::new(variable)); if env(DISABLE_DMABUF).is_some() && !ours(DISABLE_DMABUF) { return RenderingPlan::PreserveEnvironment; } - - // The distribution patch's own opt-out. It has to be read here, because WebKit - // reads DISABLE_DMABUF before isNVIDIA() and so would never reach it once a - // workaround is applied below. It speaks only to the NVIDIA question the patch - // asks, so it stands the NVIDIA branch down and nothing else; the missing-GLES - // fallback below is a packaging failure, not a GPU policy, and an operator asking - // for the hardware transport back is not asking for a launch that cannot render. - let force_dmabuf_requested_by_operator = env(FORCE_DMABUF) - .as_deref() - .is_some_and(force_dmabuf_requested); - - // Same rule for the shared-memory switch: do not combine it with legacy - // instructions a user may already carry in a launcher or environment.d file. if env(FORCE_SHARED_MEMORY).is_some() && !ours(FORCE_SHARED_MEMORY) { return RenderingPlan::PreserveEnvironment; } + // Stands the NVIDIA branch down and nothing else: it answers that patch's question, + // not the missing-GLES one below, and that launch cannot render without its fallback. + let force_dmabuf = env(FORCE_DMABUF) + .as_deref() + .is_some_and(force_dmabuf_requested); + let is_appimage = env(APPIMAGE).is_some(); let configured_backends = env(GDK_BACKEND); let explicit_wayland = configured_backends @@ -126,46 +111,31 @@ fn rendering_plan( .map(|display| !display.is_empty()) .unwrap_or(false)); - // The DMA-BUF transport breaks on the proprietary NVIDIA driver on either display - // server, so this cannot be gated on a Wayland session. Upstream declined the fix - // (bug 262607 is WONTFIX, WebKit PR 18614 closed unmerged), so Debian and Ubuntu - // carry disable-nvidia-dmabuf.patch while Fedora, Arch and the upstream tarballs do - // not. Where it is present it already empties the transport set on NVIDIA by itself: - // isNVIDIA() sits before mode.add(SharedMemory) in 2.50.4 and 2.52.6, and 2.53.90 - // moved it after, leaving {SharedMemory}. - // - // Both shipped artifacts run a patched library, though: the .deb resolves the host - // one and the AppImage bundles Ubuntu 22.04's. So on those the branch below matters - // where the library's GL_VENDOR probe and this module probe disagree, and it also - // skips the GBM device open and throwaway GL context isNVIDIA() performs at startup. - // Building against an unpatched system library is the case the branch covers outright. - // - // The two switches are not interchangeable. DISABLE_DMABUF is read before the - // SharedMemory add, so the set stays empty, checkRequirements() is false, - // AcceleratedBackingStore::create() returns nullptr, and - // webkitWebViewBaseEnterAcceleratedCompositingMode() dereferences that behind an - // ASSERT that release builds compile out. FORCE_SHM is read after the add, leaves - // {SharedMemory}, and keeps a valid backing store and accelerated compositing. + // The DMA-BUF transport breaks on the proprietary driver on either display server, so + // this cannot be gated on a Wayland session. Upstream declined the fix (bug 262607 + // WONTFIX, PR 18614 closed), so Debian and Ubuntu carry disable-nvidia-dmabuf.patch + // and Fedora, Arch and the tarballs do not. Both shipped artifacts get a patched + // library anyway (the .deb from the host, the AppImage bundles Ubuntu 22.04's), so + // here this covers hosts where that patch's GL_VENDOR probe disagrees with the module + // probe, plus the GBM open and throwaway GL context isNVIDIA() does at startup. // - // So the choice is per failure mode rather than per GPU: - // Wayland DISABLE_DMABUF. The failure there is the explicit-sync protocol - // disconnect, and FORCE_SHM routes every commit down the wl_shm path - // that trips it (WebKit bug 315436). It is also the switch with - // reports of fixing Error 71. - // X11 FORCE_SHM where the runtime supports it. X11 has no explicit-sync - // protocol, the failure there is hardware DMA-BUF allocation, and - // dropping to shared memory fixes that without the empty set. - // The empty set is not merely slower on hosts where the library's own probe does - // not fire: block/buzz#3654 reports a SIGSEGV on NVIDIA X11 with DISABLE_DMABUF - // that FORCE_SHM does not reproduce, on exactly the iGPU-presenting topology the - // probe below over-triggers on. + // The two switches are not interchangeable, so pick per failure mode, not per GPU: + // Wayland DISABLE_DMABUF. The failure is the explicit-sync disconnect, and + // FORCE_SHM routes every commit down the wl_shm path that trips it + // (bug 315436). It is also the switch reported to fix Error 71. + // X11 FORCE_SHM. No explicit-sync protocol there, the failure is hardware + // DMA-BUF allocation, and shared memory fixes it without the empty set. + // The empty set is not just slower: DISABLE_DMABUF returns before the SharedMemory + // add, so checkRequirements() is false, AcceleratedBackingStore::create() returns + // nullptr, and webkitWebViewBaseEnterAcceleratedCompositingMode() dereferences it + // behind an ASSERT release builds drop. block/buzz#3654 hits that SIGSEGV on NVIDIA + // X11, on the same iGPU-presenting topology the probe below over-triggers on. // - // The probe is kernel-module presence, not the GPU that will render. A PRIME or - // Optimus laptop presenting on the integrated GPU reads as NVIDIA here and takes a - // workaround it does not need. That is deliberate: reading the rendering GPU needs a - // GL context, and this runs before GTK is initialized precisely so that no GL state - // exists yet. Those hosts opt out with WEBKIT_DISABLE_DMABUF_RENDERER=0. - if nvidia_driver_loaded && !force_dmabuf_requested_by_operator { + // That probe is module presence, not the GPU that will render, so a PRIME laptop on + // its iGPU takes a workaround it does not need. Deliberate: reading the rendering GPU + // needs a GL context and this runs before GTK init so that none exists. Those hosts + // opt out with WEBKIT_DISABLE_DMABUF_RENDERER=0. + if nvidia_driver_loaded && !force_dmabuf { let missing_appimage_gles = is_appimage && !gles_usable; let reason = if missing_appimage_gles { NVIDIA_APPIMAGE_GLES_REASON @@ -174,8 +144,8 @@ fn rendering_plan( } else { NVIDIA_REASON }; - // FORCE_SHM still reaches the failing libepoxy path in AppImages, and WebKitGTK - // before 2.44 ignores it outright, so both keep the stronger switch. + // FORCE_SHM still reaches the failing libepoxy path in AppImages, and 2.44 is + // where WebKitGTK started reading it at all, so both keep the stronger switch. let workaround = if wayland_session || missing_appimage_gles || !supports_force_shared_memory(webkit_version) @@ -251,24 +221,21 @@ pub fn configure_renderer() -> Option<(&'static str, &'static str)> { ) { RenderingPlan::Apply(workaround, reason) => { let variable = workaround.variable(); - // A relaunch that re-decides the other way has inherited the previous - // choice. WebKit reads DISABLE_DMABUF before FORCE_SHM, so leaving the old - // one set would silently outrank the new one. Only a variable this - // application claimed is cleared; an operator's value never reaches here. + // A relaunch deciding the other way inherited the previous choice, and WebKit + // reads DISABLE_DMABUF before FORCE_SHM, so a stale one would outrank it. + // Only a variable we claimed is cleared; an operator's never reaches here. if let Some(previous) = std::env::var_os(APPLIED_WORKAROUND) { if previous != OsStr::new(variable) { std::env::remove_var(previous); } } std::env::set_var(variable, "1"); - // Claim it, so the next launch re-decides instead of reading it back as an - // operator override. + // Claim it, so the next launch re-decides rather than reading it as an override. std::env::set_var(APPLIED_WORKAROUND, variable); Some((variable, reason)) } RenderingPlan::PreserveEnvironment => { - // Nothing here is ours any more; a stale claim would suppress an operator - // override on the next relaunch. + // Nothing is ours now; a stale claim would suppress an override next relaunch. std::env::remove_var(APPLIED_WORKAROUND); None } @@ -342,8 +309,8 @@ mod tests { #[test] fn nvidia_on_x11_drops_to_shared_memory_rather_than_the_empty_transport_set() { - // X11 has no explicit-sync protocol to violate, and DISABLE_DMABUF leaves a null - // backing store that release builds dereference (block/buzz#3654). + // No explicit-sync protocol to violate here, and DISABLE_DMABUF leaves the null + // backing store release builds dereference (block/buzz#3654). assert_eq!( plan_on_nvidia(&[]), RenderingPlan::Apply(RenderingWorkaround::ForceSharedMemory, NVIDIA_REASON) @@ -385,8 +352,7 @@ mod tests { #[test] fn the_distribution_force_dmabuf_opt_out_outranks_the_nvidia_default() { - // disable-nvidia-dmabuf.patch checks this inside isNVIDIA(), which WebKit never - // reaches once DISABLE_DMABUF is set, so it has to be honoured here instead. + // Checked inside isNVIDIA(), which WebKit never reaches once DISABLE_DMABUF is set. for value in ["1", "", "yes"] { assert_eq!( plan_on_nvidia(&[(FORCE_DMABUF, value)]), @@ -398,8 +364,8 @@ mod tests { #[test] fn force_dmabuf_does_not_defeat_the_missing_gles_fallback() { - // That fallback answers a packaging failure, not the NVIDIA question this - // variable asks. Without it the AppImage cannot render at all (#8343). + // That fallback answers a packaging failure, not this variable's question, and the + // AppImage cannot render without it (#8343). assert_eq!( plan_on_host( &[(FORCE_DMABUF, "1"), (APPIMAGE, "/tmp/Unsloth.AppImage")], @@ -413,8 +379,7 @@ mod tests { #[test] fn force_dmabuf_still_leaves_the_wayland_workaround_in_place() { - // The variable speaks to the NVIDIA patch, not to the Wayland transport rule - // that predates it. + // It speaks to the NVIDIA patch, not the Wayland rule that predates it. assert_eq!( plan_on_nvidia(&[(FORCE_DMABUF, "1"), (WAYLAND_DISPLAY, "wayland-0")]), RenderingPlan::Apply(RenderingWorkaround::ForceSharedMemory, WAYLAND_REASON) @@ -452,8 +417,7 @@ mod tests { #[test] fn nvidia_relaunch_re_decides_over_its_own_inherited_force_shm() { - // Marked, so it is this application's own state from an earlier launch. On - // Wayland the re-decision escalates it rather than inheriting it. + // Marked, so it is our own state from an earlier launch, and Wayland escalates it. assert_eq!( plan_on_nvidia(&[ (WAYLAND_DISPLAY, "wayland-0"), @@ -466,8 +430,8 @@ mod tests { #[test] fn a_relaunch_re_reaches_the_same_decision_it_made_first_time() { - // Tauri's process::restart inherits the environment, so the plan has to be a - // fixed point: feed a launch's own output back in and it must not drift. + // process::restart inherits the environment, so the plan must be a fixed point: + // feed a launch's own output back in and it must not drift. let hosts = [ (&[][..], true), (&[(WAYLAND_DISPLAY, "wayland-0")][..], true), From c437e3c7435a4a66a41e4ae268bd93cbcdb92862 Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Mon, 24 Aug 2026 05:49:05 +0000 Subject: [PATCH 06/11] Pair the distro opt-out with FORCE_SHM and clear our own state on stand-down for PR #8884 Two genuine Codex findings, both reproduced against the shipped sources. FORCE_SHM alone never reached selection on a patched library: isNVIDIA() returns before mode.add(SharedMemory) in Ubuntu 2.50.4 and Debian 2.52.6, so the X11 branch still handed NVIDIA an empty transport set. Its own opt-out is checked first inside isNVIDIA(), so ForceSharedMemory now sets WEBKIT_FORCE_DMABUF_RENDERER alongside WEBKIT_DMABUF_RENDERER_FORCE_SHM. Unpatched libraries ignore it, and 2.53.91, which moved isNVIDIA() after the add, lands on the same {SharedMemory}. Standing down dropped the ownership marker but left the variable set, so a relaunch that no longer wanted a workaround kept it, and the launch after that read the now unmarked value as an operator override and preserved it for good. release_claimed clears the values we set, never an operator's. The marker holds the variable list rather than one name, so ownership still covers a workaround that takes two, and our own FORCE_DMABUF no longer reads back as an operator opting out. 419 crate tests pass. The 4860-case host matrix still shows no regression, and the NVIDIA X11 rows now reach {SharedMemory} instead of the empty set. --- studio/src-tauri/src/linux_webkit.rs | 144 ++++++++++++++++++++++----- studio/src-tauri/src/main.rs | 9 +- 2 files changed, 124 insertions(+), 29 deletions(-) diff --git a/studio/src-tauri/src/linux_webkit.rs b/studio/src-tauri/src/linux_webkit.rs index 78534b0f8a1a..50c65183f0e2 100644 --- a/studio/src-tauri/src/linux_webkit.rs +++ b/studio/src-tauri/src/linux_webkit.rs @@ -13,8 +13,8 @@ const DISABLE_DMABUF: &str = "WEBKIT_DISABLE_DMABUF_RENDERER"; // disable-nvidia-dmabuf.patch's own opt-out, read inside isNVIDIA(). WebKit returns on // DISABLE_DMABUF first, so it never gets there unless we honour it ourselves. const FORCE_DMABUF: &str = "WEBKIT_FORCE_DMABUF_RENDERER"; -// Names the variable we set, so a relaunch tells our own inherited output from an -// operator's value. Tauri's process::restart does not env_clear. WebKit never reads it. +// Comma-joined list of the variables we set, so a relaunch tells our own inherited output +// from an operator's value. Tauri's process::restart does not env_clear. WebKit never reads it. const APPLIED_WORKAROUND: &str = "UNSLOTH_WEBKIT_RENDERER_WORKAROUND"; const FORCE_SHARED_MEMORY_MIN_VERSION: (u32, u32) = (2, 44); // both the proprietary and open nvidia modules publish this; nouveau does not and is unaffected @@ -34,10 +34,14 @@ enum RenderingWorkaround { } impl RenderingWorkaround { - fn variable(self) -> &'static str { + /// Forcing shared memory takes two variables on a patched library: isNVIDIA() returns + /// before mode.add(SharedMemory) in 2.50.4 and 2.52.6, so FORCE_SHM alone is never + /// read and the set is empty anyway. FORCE_DMABUF is what makes isNVIDIA() stand down + /// so selection reaches FORCE_SHM. Unpatched libraries ignore it. + fn variables(self) -> &'static [&'static str] { match self { - Self::ForceSharedMemory => FORCE_SHARED_MEMORY, - Self::DisableDmabuf => DISABLE_DMABUF, + Self::ForceSharedMemory => &[FORCE_SHARED_MEMORY, FORCE_DMABUF], + Self::DisableDmabuf => &[DISABLE_DMABUF], } } } @@ -80,8 +84,13 @@ fn rendering_plan( // DISABLE_DMABUF=0 is how a host this over-triggers on gets its accelerated path // back: WebKit reads it as unset (`g_strcmp0(v, "0")`), and a patched library's own // GL_VENDOR probe then declines on an iGPU. An empty value is not that request. - let applied_by_this_app = env(APPLIED_WORKAROUND); - let ours = |variable: &str| applied_by_this_app.as_deref() == Some(OsStr::new(variable)); + let applied_by_this_app = env(APPLIED_WORKAROUND).unwrap_or_default(); + let ours = |variable: &str| { + applied_by_this_app + .to_string_lossy() + .split(',') + .any(|claimed| claimed == variable) + }; if env(DISABLE_DMABUF).is_some() && !ours(DISABLE_DMABUF) { return RenderingPlan::PreserveEnvironment; @@ -92,9 +101,10 @@ fn rendering_plan( // Stands the NVIDIA branch down and nothing else: it answers that patch's question, // not the missing-GLES one below, and that launch cannot render without its fallback. - let force_dmabuf = env(FORCE_DMABUF) - .as_deref() - .is_some_and(force_dmabuf_requested); + let force_dmabuf = !ours(FORCE_DMABUF) + && env(FORCE_DMABUF) + .as_deref() + .is_some_and(force_dmabuf_requested); let is_appimage = env(APPIMAGE).is_some(); let configured_backends = env(GDK_BACKEND); @@ -208,10 +218,22 @@ fn runtime_webkit_version() -> (u32, u32, u32) { } } +/// Unset the variables an earlier launch of this process tree claimed and `drop` selects, +/// then retire the claim. Only claimed names are touched, never an operator's value. +fn release_claimed(drop: impl Fn(&str) -> bool) { + let claimed = std::env::var_os(APPLIED_WORKAROUND).unwrap_or_default(); + for name in claimed.to_string_lossy().split(',') { + if !name.is_empty() && drop(name) { + std::env::remove_var(name); + } + } + std::env::remove_var(APPLIED_WORKAROUND); +} + /// Select a compatible WebKitGTK rendering transport before GTK initialization. /// /// Returns the variable applied by this launch and the reason for it, if any. -pub fn configure_renderer() -> Option<(&'static str, &'static str)> { +pub fn configure_renderer() -> Option<(&'static [&'static str], &'static str)> { let gles_usable = std::env::var_os(APPIMAGE).is_none() || gles_is_usable(); match rendering_plan( |name| std::env::var_os(name), @@ -220,23 +242,20 @@ pub fn configure_renderer() -> Option<(&'static str, &'static str)> { nvidia_driver_loaded(), ) { RenderingPlan::Apply(workaround, reason) => { - let variable = workaround.variable(); - // A relaunch deciding the other way inherited the previous choice, and WebKit - // reads DISABLE_DMABUF before FORCE_SHM, so a stale one would outrank it. - // Only a variable we claimed is cleared; an operator's never reaches here. - if let Some(previous) = std::env::var_os(APPLIED_WORKAROUND) { - if previous != OsStr::new(variable) { - std::env::remove_var(previous); - } + let variables = workaround.variables(); + release_claimed(|name| !variables.contains(&name)); + for variable in variables { + std::env::set_var(variable, "1"); } - std::env::set_var(variable, "1"); - // Claim it, so the next launch re-decides rather than reading it as an override. - std::env::set_var(APPLIED_WORKAROUND, variable); - Some((variable, reason)) + // Claim them, so the next launch re-decides rather than reading them as overrides. + std::env::set_var(APPLIED_WORKAROUND, variables.join(",")); + Some((variables, reason)) } RenderingPlan::PreserveEnvironment => { - // Nothing is ours now; a stale claim would suppress an override next relaunch. - std::env::remove_var(APPLIED_WORKAROUND); + // Drop the values too, not just the claim. Leaving one set keeps a workaround + // this launch decided against, and the next launch, seeing it unmarked, would + // read it as an operator override and preserve it for good. + release_claimed(|_| true); None } } @@ -445,8 +464,11 @@ mod tests { continue; }; let mut inherited = session.to_vec(); - inherited.push((workaround.variable(), "1")); - inherited.push((APPLIED_WORKAROUND, workaround.variable())); + let claimed = workaround.variables().join(","); + for variable in workaround.variables() { + inherited.push((variable, "1")); + } + inherited.push((APPLIED_WORKAROUND, &claimed)); assert_eq!( plan_on_host(&inherited, MODERN_WEBKIT, true, nvidia), RenderingPlan::Apply(workaround, reason), @@ -455,6 +477,74 @@ mod tests { } } + // release_claimed touches the real process environment, so serialise the tests that do. + static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + + #[test] + fn standing_down_clears_the_value_we_set_not_only_the_claim() { + let _guard = ENV_LOCK + .lock() + .unwrap_or_else(|poisoned| poisoned.into_inner()); + std::env::set_var(DISABLE_DMABUF, "1"); + std::env::set_var(APPLIED_WORKAROUND, DISABLE_DMABUF); + + release_claimed(|_| true); + + // Leaving it set would keep a workaround this launch decided against, and the + // next launch would read the unmarked value as an operator override for good. + assert!(std::env::var_os(DISABLE_DMABUF).is_none()); + assert!(std::env::var_os(APPLIED_WORKAROUND).is_none()); + } + + #[test] + fn switching_workaround_clears_only_the_variable_being_dropped() { + let _guard = ENV_LOCK + .lock() + .unwrap_or_else(|poisoned| poisoned.into_inner()); + std::env::set_var(FORCE_SHARED_MEMORY, "1"); + std::env::set_var(FORCE_DMABUF, "1"); + std::env::set_var( + APPLIED_WORKAROUND, + [FORCE_SHARED_MEMORY, FORCE_DMABUF].join(","), + ); + + let keep = [FORCE_DMABUF]; + release_claimed(|name| !keep.contains(&name)); + + assert!(std::env::var_os(FORCE_SHARED_MEMORY).is_none()); + assert_eq!(std::env::var_os(FORCE_DMABUF), Some(OsString::from("1"))); + std::env::remove_var(FORCE_DMABUF); + } + + #[test] + fn forcing_shared_memory_also_opts_out_of_the_distro_patch() { + // isNVIDIA() returns before mode.add(SharedMemory) on 2.50.4 and 2.52.6, so + // FORCE_SHM on its own is never read there and the set is empty regardless. + assert_eq!( + RenderingWorkaround::ForceSharedMemory.variables(), + &[FORCE_SHARED_MEMORY, FORCE_DMABUF] + ); + assert_eq!( + RenderingWorkaround::DisableDmabuf.variables(), + &[DISABLE_DMABUF] + ); + } + + #[test] + fn our_own_force_dmabuf_does_not_read_back_as_an_opt_out() { + // We set it as half of ForceSharedMemory; a relaunch must not see its own output + // as an operator standing the NVIDIA branch down. + let claimed = [FORCE_SHARED_MEMORY, FORCE_DMABUF].join(","); + assert_eq!( + plan_on_nvidia(&[ + (FORCE_SHARED_MEMORY, "1"), + (FORCE_DMABUF, "1"), + (APPLIED_WORKAROUND, &claimed), + ]), + RenderingPlan::Apply(RenderingWorkaround::ForceSharedMemory, NVIDIA_REASON) + ); + } + #[test] fn a_marker_for_the_other_variable_does_not_release_force_shm() { // Only a claim naming FORCE_SHM identifies FORCE_SHM as ours. diff --git a/studio/src-tauri/src/main.rs b/studio/src-tauri/src/main.rs index 1778cc35c48e..1065d6dd9d64 100644 --- a/studio/src-tauri/src/main.rs +++ b/studio/src-tauri/src/main.rs @@ -1817,8 +1817,13 @@ fn main() { info!("Unsloth desktop app starting"); #[cfg(target_os = "linux")] - if let Some((variable, reason)) = webkit_rendering_workaround { - info!("{reason}; set {variable}=1 for WebKitGTK compatibility"); + if let Some((variables, reason)) = webkit_rendering_workaround { + let applied = variables + .iter() + .map(|variable| format!("{variable}=1")) + .collect::>() + .join(" "); + info!("{reason}; set {applied} for WebKitGTK compatibility"); } windows_job::initialize(); From 59d7e9b153091a46485ce35b3bc0dff90fac5afa Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Mon, 24 Aug 2026 06:00:30 +0000 Subject: [PATCH 07/11] Resolve GDK_BACKEND in GDK's own order before choosing the workaround for PR #8884 Membership was not selection. backend_allows_wayland returned true if wayland appeared anywhere in GDK_BACKEND, so x11,wayland read as a Wayland session even though GDK runs on X11 whenever the X display opens. That was only a wrong log line until this branch made the flag pick between the two switches; now it hands an X11 session the empty transport set, which is the configuration with the null backing store report. selected_backend_is_wayland follows gdk_display_manager_open_display in gtk-3-24: g_strsplit on ',' with no trimming, g_str_equal so matching is exact and case sensitive, entries tried in order with the first that opens winning, and '*' expanding to the built-in order, wayland before x11 on Linux. A display cannot be opened before GTK init, so DISPLAY and WAYLAND_DISPLAY stand in for what GDK's own openers read; an explicitly named wayland still counts without WAYLAND_DISPLAY because GDK falls back to the default wayland-0 socket. Four GDK_BACKEND values move against the previous commit and nothing else does: x11,wayland now resolves to X11 when DISPLAY is set, '*' needs a wayland display to pick wayland, and ' wayland' and 'Wayland' match no backend at all. 399 of 15552 combinations, all of them cases where the old rule disagreed with GDK. 421 crate tests pass, three new. The 4860-case host matrix still reports no regression. --- studio/src-tauri/src/linux_webkit.rs | 103 ++++++++++++++++++++++----- 1 file changed, 84 insertions(+), 19 deletions(-) diff --git a/studio/src-tauri/src/linux_webkit.rs b/studio/src-tauri/src/linux_webkit.rs index 50c65183f0e2..f563a7202d9b 100644 --- a/studio/src-tauri/src/linux_webkit.rs +++ b/studio/src-tauri/src/linux_webkit.rs @@ -7,6 +7,7 @@ const APPIMAGE: &str = "APPIMAGE"; const GLES_V2: &[u8] = b"libGLESv2.so.2\0"; const WAYLAND_DISPLAY: &str = "WAYLAND_DISPLAY"; +const X11_DISPLAY: &str = "DISPLAY"; const GDK_BACKEND: &str = "GDK_BACKEND"; const FORCE_SHARED_MEMORY: &str = "WEBKIT_DMABUF_RENDERER_FORCE_SHM"; const DISABLE_DMABUF: &str = "WEBKIT_DISABLE_DMABUF_RENDERER"; @@ -53,11 +54,27 @@ enum RenderingPlan { PreserveEnvironment, } -fn backend_allows_wayland(backends: &OsStr) -> bool { - backends.to_string_lossy().split(',').any(|backend| { - let backend = backend.trim(); - backend == "*" || backend.eq_ignore_ascii_case("wayland") - }) +/// Resolve GDK_BACKEND the way gdk_display_manager_open_display does: g_strsplit on ',', +/// no trimming, g_str_equal so matching is exact and case sensitive, entries tried in +/// order, and the first backend whose display opens wins. '*' expands to the built-in +/// order, which is wayland before x11 on Linux. Membership is not selection: `x11,wayland` +/// runs on X11 whenever an X display opens, and on NVIDIA that decides between the +/// shared-memory switch and the empty transport set. +/// +/// A display cannot be opened here, so "opens" is the same environment GDK's own openers +/// read. An explicitly named wayland still counts without WAYLAND_DISPLAY, because GDK +/// falls back to the default wayland-0 socket. +fn selected_backend_is_wayland(backends: &OsStr, wayland_display: bool, x11_display: bool) -> bool { + for backend in backends.to_string_lossy().split(',') { + match backend { + "wayland" => return true, + "x11" if x11_display => return false, + "*" if wayland_display => return true, + "*" if x11_display => return false, + _ => continue, + } + } + false } fn supports_force_shared_memory((major, minor, _micro): (u32, u32, u32)) -> bool { @@ -107,19 +124,19 @@ fn rendering_plan( .is_some_and(force_dmabuf_requested); let is_appimage = env(APPIMAGE).is_some(); - let configured_backends = env(GDK_BACKEND); - let explicit_wayland = configured_backends - .as_deref() - .map(backend_allows_wayland) + let wayland_display = env(WAYLAND_DISPLAY) + .map(|display| !display.is_empty()) .unwrap_or(false); - // GDK can connect to the default wayland-0 socket when its backend is explicitly - // Wayland even if WAYLAND_DISPLAY is absent. Conversely, WAYLAND_DISPLAY is inherited - // by XWayland apps, so an X11-only GDK_BACKEND must take precedence. - let wayland_session = explicit_wayland - || (configured_backends.is_none() - && env(WAYLAND_DISPLAY) - .map(|display| !display.is_empty()) - .unwrap_or(false)); + // WAYLAND_DISPLAY is inherited by XWayland clients, so it only decides the backend + // when GDK_BACKEND leaves the choice open. + let wayland_session = match env(GDK_BACKEND) { + Some(backends) => selected_backend_is_wayland( + &backends, + wayland_display, + env(X11_DISPLAY).is_some_and(|display| !display.is_empty()), + ), + None => wayland_display, + }; // The DMA-BUF transport breaks on the proprietary driver on either display server, so // this cannot be gated on a Wayland session. Upstream declined the fix (bug 262607 @@ -582,11 +599,59 @@ mod tests { } #[test] - fn a_gdk_wildcard_can_select_wayland_without_a_display_variable() { + fn a_gdk_wildcard_selects_wayland_only_when_a_wayland_display_is_there() { + // '*' expands to the built-in order, wayland before x11, and takes the first that + // opens. With no WAYLAND_DISPLAY the wayland opener has nothing to connect to. assert_eq!( - plan(&[(GDK_BACKEND, "*")]), + plan(&[(GDK_BACKEND, "*"), (WAYLAND_DISPLAY, "wayland-0")]), RenderingPlan::Apply(RenderingWorkaround::ForceSharedMemory, WAYLAND_REASON) ); + assert_eq!( + plan(&[(GDK_BACKEND, "*"), (X11_DISPLAY, ":0")]), + RenderingPlan::PreserveEnvironment + ); + } + + #[test] + fn an_ordered_backend_list_takes_the_first_that_opens() { + // GDK tries entries in order, so x11,wayland runs on X11 whenever DISPLAY opens. + // Membership said Wayland, which on NVIDIA picked the empty transport set for a + // session that is actually X11. + assert_eq!( + plan_on_nvidia(&[ + (GDK_BACKEND, "x11,wayland"), + (WAYLAND_DISPLAY, "wayland-0"), + (X11_DISPLAY, ":0"), + ]), + RenderingPlan::Apply(RenderingWorkaround::ForceSharedMemory, NVIDIA_REASON) + ); + // Reverse the order and Wayland is what opens first. + assert_eq!( + plan_on_nvidia(&[ + (GDK_BACKEND, "wayland,x11"), + (WAYLAND_DISPLAY, "wayland-0"), + (X11_DISPLAY, ":0"), + ]), + RenderingPlan::Apply(RenderingWorkaround::DisableDmabuf, NVIDIA_WAYLAND_REASON) + ); + // x11 named first but no X display: GDK falls through to the next entry. + assert_eq!( + plan_on_nvidia(&[(GDK_BACKEND, "x11,wayland"), (WAYLAND_DISPLAY, "wayland-0")]), + RenderingPlan::Apply(RenderingWorkaround::DisableDmabuf, NVIDIA_WAYLAND_REASON) + ); + } + + #[test] + fn backend_matching_is_exact_like_g_str_equal() { + // g_strsplit does not trim and g_str_equal is case sensitive, so neither of these + // names a backend GDK will match; it falls through to the next entry. + for value in [" wayland", "Wayland", "wayland-egl"] { + assert_eq!( + plan(&[(GDK_BACKEND, value), (WAYLAND_DISPLAY, "wayland-0")]), + RenderingPlan::PreserveEnvironment, + "GDK_BACKEND={value} matches no GDK backend" + ); + } } #[test] From 90db270fedc02f7326547162723ebf289c69a135 Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Mon, 24 Aug 2026 06:19:10 +0000 Subject: [PATCH 08/11] Scope the distro opt-out to confirmed NVIDIA and let a dead Wayland backend fall through for PR #8884 Two more from the last round, both on code from this branch. The distro opt-out was attached to the workaround, not to the branch that knows why it is needed. The generic Wayland plan uses the same ForceSharedMemory, so a host whose /proc/driver/nvidia is hidden, a container or a confined launch, took that plan and stood the distribution patch down anyway. WebKit would have emptied the transport set there on its own; instead it was pushed onto the wl_shm commit path bug 315436 disconnects on. ForceSharedMemoryOnNvidia now carries the pair and only the confirmed-NVIDIA branch selects it; the generic plan sets FORCE_SHM alone and leaves WebKit's own detection intact. selected_backend_is_wayland also returned on a named wayland without asking whether it could open. GDK skips a backend whose opener fails and tries the next, so wayland,x11 with no compositor runs on X11, and calling it Wayland handed an X11 host the empty transport set. A named wayland now needs a display to win, falling through otherwise, and wl_display_connect(NULL)'s default wayland-0 socket counts as one: wayland_socket_present stats XDG_RUNTIME_DIR before GTK init. A lone wayland with no fallback entry still takes the Wayland workaround, since GTK either finds a compositor or does not start at all. 424 crate tests pass, three new. 144 of 15552 combinations move against 59d7e9b15, all of them GDK_BACKEND values naming wayland or the generic Wayland plan; the 4860-case host matrix still reports no regression. --- studio/src-tauri/src/linux_webkit.rs | 146 +++++++++++++++++++++++---- 1 file changed, 127 insertions(+), 19 deletions(-) diff --git a/studio/src-tauri/src/linux_webkit.rs b/studio/src-tauri/src/linux_webkit.rs index f563a7202d9b..e13b4bc92da5 100644 --- a/studio/src-tauri/src/linux_webkit.rs +++ b/studio/src-tauri/src/linux_webkit.rs @@ -8,6 +8,7 @@ const GLES_V2: &[u8] = b"libGLESv2.so.2\0"; const WAYLAND_DISPLAY: &str = "WAYLAND_DISPLAY"; const X11_DISPLAY: &str = "DISPLAY"; +const DEFAULT_WAYLAND_SOCKET: &str = "wayland-0"; const GDK_BACKEND: &str = "GDK_BACKEND"; const FORCE_SHARED_MEMORY: &str = "WEBKIT_DMABUF_RENDERER_FORCE_SHM"; const DISABLE_DMABUF: &str = "WEBKIT_DISABLE_DMABUF_RENDERER"; @@ -31,17 +32,19 @@ const APPIMAGE_GLES_REASON: &str = "AppImage without a usable GLES library"; #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum RenderingWorkaround { ForceSharedMemory, + /// Only for a host the probe confirmed is NVIDIA. On a patched library isNVIDIA() + /// returns before mode.add(SharedMemory) in 2.50.4 and 2.52.6, so FORCE_SHM alone is + /// never read there; FORCE_DMABUF stands that check down so selection reaches it. + /// Unpatched libraries ignore the variable. + ForceSharedMemoryOnNvidia, DisableDmabuf, } impl RenderingWorkaround { - /// Forcing shared memory takes two variables on a patched library: isNVIDIA() returns - /// before mode.add(SharedMemory) in 2.50.4 and 2.52.6, so FORCE_SHM alone is never - /// read and the set is empty anyway. FORCE_DMABUF is what makes isNVIDIA() stand down - /// so selection reaches FORCE_SHM. Unpatched libraries ignore it. fn variables(self) -> &'static [&'static str] { match self { - Self::ForceSharedMemory => &[FORCE_SHARED_MEMORY, FORCE_DMABUF], + Self::ForceSharedMemory => &[FORCE_SHARED_MEMORY], + Self::ForceSharedMemoryOnNvidia => &[FORCE_SHARED_MEMORY, FORCE_DMABUF], Self::DisableDmabuf => &[DISABLE_DMABUF], } } @@ -62,19 +65,25 @@ enum RenderingPlan { /// shared-memory switch and the empty transport set. /// /// A display cannot be opened here, so "opens" is the same environment GDK's own openers -/// read. An explicitly named wayland still counts without WAYLAND_DISPLAY, because GDK -/// falls back to the default wayland-0 socket. +/// read: `wayland_display` covers WAYLAND_DISPLAY and the default wayland-0 socket +/// wl_display_connect(NULL) falls back to. A named backend that cannot open is not the +/// selection, it is skipped, so wayland,x11 lands on X11 when no compositor answers. fn selected_backend_is_wayland(backends: &OsStr, wayland_display: bool, x11_display: bool) -> bool { + let mut named_wayland = false; for backend in backends.to_string_lossy().split(',') { match backend { - "wayland" => return true, + "wayland" if wayland_display => return true, + "wayland" => named_wayland = true, "x11" if x11_display => return false, "*" if wayland_display => return true, "*" if x11_display => return false, _ => continue, } } - false + // Wayland was asked for and nothing else could open. GTK will fail to start if the + // compositor really is absent, so the workaround choice is moot either way; keep the + // Wayland one so a socket we cannot see from here is not treated as an X11 session. + named_wayland } fn supports_force_shared_memory((major, minor, _micro): (u32, u32, u32)) -> bool { @@ -92,6 +101,7 @@ fn rendering_plan( webkit_version: (u32, u32, u32), gles_usable: bool, nvidia_driver_loaded: bool, + wayland_socket: bool, ) -> RenderingPlan { // Either renderer variable is an operator override when present, `=0` and an empty // value included, unless the marker says we wrote it. Without that test a launch @@ -127,12 +137,14 @@ fn rendering_plan( let wayland_display = env(WAYLAND_DISPLAY) .map(|display| !display.is_empty()) .unwrap_or(false); - // WAYLAND_DISPLAY is inherited by XWayland clients, so it only decides the backend - // when GDK_BACKEND leaves the choice open. + // WAYLAND_DISPLAY is inherited by XWayland clients, so it only decides the backend when + // GDK_BACKEND leaves the choice open. When GDK_BACKEND names backends, what matters is + // which one opens, and wl_display_connect(NULL) also falls back to the default + // wayland-0 socket, so an unset WAYLAND_DISPLAY alone does not mean wayland cannot open. let wayland_session = match env(GDK_BACKEND) { Some(backends) => selected_backend_is_wayland( &backends, - wayland_display, + wayland_display || wayland_socket, env(X11_DISPLAY).is_some_and(|display| !display.is_empty()), ), None => wayland_display, @@ -179,7 +191,7 @@ fn rendering_plan( { RenderingWorkaround::DisableDmabuf } else { - RenderingWorkaround::ForceSharedMemory + RenderingWorkaround::ForceSharedMemoryOnNvidia }; return RenderingPlan::Apply(workaround, reason); } @@ -197,6 +209,9 @@ fn rendering_plan( // FORCE_SHM still reaches the failing libepoxy path in AppImages. RenderingWorkaround::DisableDmabuf } else if supports_force_shared_memory(webkit_version) { + // Plain FORCE_SHM: nothing here confirmed NVIDIA, so leave the library's own + // detection alone. Standing it down would force wl_shm on a host it would have + // taken off DMA-BUF itself, which is the commit path bug 315436 disconnects on. RenderingWorkaround::ForceSharedMemory } else { // FORCE_SHM was added with WebKitGTK 2.44. Older host libraries ignore @@ -206,6 +221,16 @@ fn rendering_plan( RenderingPlan::Apply(workaround, WAYLAND_REASON) } +/// The socket wl_display_connect(NULL) falls back to when WAYLAND_DISPLAY is unset. A +/// stat, so it touches no GL state and is safe before GTK initialization. +fn wayland_socket_present() -> bool { + std::env::var_os("XDG_RUNTIME_DIR").is_some_and(|dir| { + std::path::Path::new(&dir) + .join(DEFAULT_WAYLAND_SOCKET) + .exists() + }) +} + fn nvidia_driver_loaded() -> bool { std::path::Path::new(NVIDIA_DRIVER_VERSION_PATH).exists() } @@ -257,6 +282,7 @@ pub fn configure_renderer() -> Option<(&'static [&'static str], &'static str)> { runtime_webkit_version(), gles_usable, nvidia_driver_loaded(), + wayland_socket_present(), ) { RenderingPlan::Apply(workaround, reason) => { let variables = workaround.variables(); @@ -289,6 +315,22 @@ mod tests { webkit_version: (u32, u32, u32), gles_usable: bool, nvidia_driver_loaded: bool, + ) -> RenderingPlan { + plan_on_host_with_socket( + vars, + webkit_version, + gles_usable, + nvidia_driver_loaded, + false, + ) + } + + fn plan_on_host_with_socket( + vars: &[(&str, &str)], + webkit_version: (u32, u32, u32), + gles_usable: bool, + nvidia_driver_loaded: bool, + wayland_socket: bool, ) -> RenderingPlan { rendering_plan( |name| { @@ -299,6 +341,7 @@ mod tests { webkit_version, gles_usable, nvidia_driver_loaded, + wayland_socket, ) } @@ -349,7 +392,10 @@ mod tests { // backing store release builds dereference (block/buzz#3654). assert_eq!( plan_on_nvidia(&[]), - RenderingPlan::Apply(RenderingWorkaround::ForceSharedMemory, NVIDIA_REASON) + RenderingPlan::Apply( + RenderingWorkaround::ForceSharedMemoryOnNvidia, + NVIDIA_REASON + ) ); } @@ -357,7 +403,10 @@ mod tests { fn nvidia_under_an_explicit_x11_backend_still_applies() { assert_eq!( plan_on_nvidia(&[(GDK_BACKEND, "x11")]), - RenderingPlan::Apply(RenderingWorkaround::ForceSharedMemory, NVIDIA_REASON) + RenderingPlan::Apply( + RenderingWorkaround::ForceSharedMemoryOnNvidia, + NVIDIA_REASON + ) ); } @@ -427,7 +476,10 @@ mod tests { // The patch tests the first byte against '0', so "0" is not a request. assert_eq!( plan_on_nvidia(&[(FORCE_DMABUF, "0")]), - RenderingPlan::Apply(RenderingWorkaround::ForceSharedMemory, NVIDIA_REASON) + RenderingPlan::Apply( + RenderingWorkaround::ForceSharedMemoryOnNvidia, + NVIDIA_REASON + ) ); } @@ -533,12 +585,62 @@ mod tests { std::env::remove_var(FORCE_DMABUF); } + #[test] + fn a_generic_shared_memory_plan_leaves_webkits_own_nvidia_detection_alone() { + // Nothing here confirmed NVIDIA: /proc/driver/nvidia can be hidden by a container + // or a confined launch while WebKit's GL probe still sees it. Standing the patch + // down there would force the wl_shm commit path bug 315436 disconnects on. + assert_eq!( + plan(&[(WAYLAND_DISPLAY, "wayland-0")]), + RenderingPlan::Apply(RenderingWorkaround::ForceSharedMemory, WAYLAND_REASON) + ); + assert_eq!( + RenderingWorkaround::ForceSharedMemory.variables(), + &[FORCE_SHARED_MEMORY] + ); + } + + #[test] + fn a_named_wayland_that_cannot_open_falls_through_to_the_next_backend() { + // GDK tries wayland first, its opener fails with no socket to connect to, and the + // loop continues to x11. Calling that a Wayland session hands an X11 host the + // empty transport set. + assert_eq!( + plan_on_nvidia(&[(GDK_BACKEND, "wayland,x11"), (X11_DISPLAY, ":0")]), + RenderingPlan::Apply( + RenderingWorkaround::ForceSharedMemoryOnNvidia, + NVIDIA_REASON + ) + ); + // Same list, but the default wayland-0 socket is there, so wayland does open. + assert_eq!( + plan_on_host_with_socket( + &[(GDK_BACKEND, "wayland,x11"), (X11_DISPLAY, ":0")], + MODERN_WEBKIT, + true, + true, + true + ), + RenderingPlan::Apply(RenderingWorkaround::DisableDmabuf, NVIDIA_WAYLAND_REASON) + ); + } + + #[test] + fn a_lone_named_wayland_still_takes_the_wayland_workaround() { + // No fallback entry, so GTK either finds a compositor or fails to start. Treating + // it as X11 would be a guess against the only backend the operator named. + assert_eq!( + plan_on_nvidia(&[(GDK_BACKEND, "wayland")]), + RenderingPlan::Apply(RenderingWorkaround::DisableDmabuf, NVIDIA_WAYLAND_REASON) + ); + } + #[test] fn forcing_shared_memory_also_opts_out_of_the_distro_patch() { // isNVIDIA() returns before mode.add(SharedMemory) on 2.50.4 and 2.52.6, so // FORCE_SHM on its own is never read there and the set is empty regardless. assert_eq!( - RenderingWorkaround::ForceSharedMemory.variables(), + RenderingWorkaround::ForceSharedMemoryOnNvidia.variables(), &[FORCE_SHARED_MEMORY, FORCE_DMABUF] ); assert_eq!( @@ -558,7 +660,10 @@ mod tests { (FORCE_DMABUF, "1"), (APPLIED_WORKAROUND, &claimed), ]), - RenderingPlan::Apply(RenderingWorkaround::ForceSharedMemory, NVIDIA_REASON) + RenderingPlan::Apply( + RenderingWorkaround::ForceSharedMemoryOnNvidia, + NVIDIA_REASON + ) ); } @@ -623,7 +728,10 @@ mod tests { (WAYLAND_DISPLAY, "wayland-0"), (X11_DISPLAY, ":0"), ]), - RenderingPlan::Apply(RenderingWorkaround::ForceSharedMemory, NVIDIA_REASON) + RenderingPlan::Apply( + RenderingWorkaround::ForceSharedMemoryOnNvidia, + NVIDIA_REASON + ) ); // Reverse the order and Wayland is what opens first. assert_eq!( From 21f9cbd2aa0dbbc2b4d9e1d6d7c46c4665036c04 Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Mon, 24 Aug 2026 06:45:10 +0000 Subject: [PATCH 09/11] Follow GDK's default backend order and reject a stale Wayland socket for PR #8884 --- studio/src-tauri/src/linux_webkit.rs | 85 +++++++++++++++++++++------- 1 file changed, 66 insertions(+), 19 deletions(-) diff --git a/studio/src-tauri/src/linux_webkit.rs b/studio/src-tauri/src/linux_webkit.rs index e13b4bc92da5..363b1b5dd68e 100644 --- a/studio/src-tauri/src/linux_webkit.rs +++ b/studio/src-tauri/src/linux_webkit.rs @@ -2,6 +2,7 @@ // Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0 use std::ffi::{OsStr, OsString}; +use std::os::unix::net::UnixStream; const APPIMAGE: &str = "APPIMAGE"; const GLES_V2: &[u8] = b"libGLESv2.so.2\0"; @@ -137,18 +138,15 @@ fn rendering_plan( let wayland_display = env(WAYLAND_DISPLAY) .map(|display| !display.is_empty()) .unwrap_or(false); - // WAYLAND_DISPLAY is inherited by XWayland clients, so it only decides the backend when - // GDK_BACKEND leaves the choice open. When GDK_BACKEND names backends, what matters is - // which one opens, and wl_display_connect(NULL) also falls back to the default - // wayland-0 socket, so an unset WAYLAND_DISPLAY alone does not mean wayland cannot open. - let wayland_session = match env(GDK_BACKEND) { - Some(backends) => selected_backend_is_wayland( - &backends, - wayland_display || wayland_socket, - env(X11_DISPLAY).is_some_and(|display| !display.is_empty()), - ), - None => wayland_display, - }; + // What matters is which backend opens, not WAYLAND_DISPLAY, which XWayland clients + // inherit too. wl_display_connect(NULL) falls back to the default wayland-0 socket, so + // an unset WAYLAND_DISPLAY alone does not mean wayland cannot open. An unset + // GDK_BACKEND is GDK's own "*", which tries wayland before x11 (gdk_backends[]). + let wayland_session = selected_backend_is_wayland( + env(GDK_BACKEND).as_deref().unwrap_or(OsStr::new("*")), + wayland_display || wayland_socket, + env(X11_DISPLAY).is_some_and(|display| !display.is_empty()), + ); // The DMA-BUF transport breaks on the proprietary driver on either display server, so // this cannot be gated on a Wayland session. Upstream declined the fix (bug 262607 @@ -221,16 +219,20 @@ fn rendering_plan( RenderingPlan::Apply(workaround, WAYLAND_REASON) } -/// The socket wl_display_connect(NULL) falls back to when WAYLAND_DISPLAY is unset. A -/// stat, so it touches no GL state and is safe before GTK initialization. -fn wayland_socket_present() -> bool { - std::env::var_os("XDG_RUNTIME_DIR").is_some_and(|dir| { - std::path::Path::new(&dir) - .join(DEFAULT_WAYLAND_SOCKET) - .exists() +/// Whether the socket wl_display_connect(NULL) falls back to accepts connections. A +/// crashed compositor leaves the file behind, and existence alone would then read as a +/// Wayland session while GDK's opener fails over to x11. Connecting instead fails the same +/// way GDK does (ECONNREFUSED), and touches no GL state, so it is safe before GTK init. +fn wayland_socket_connectable(runtime_dir: Option) -> bool { + runtime_dir.is_some_and(|dir| { + UnixStream::connect(std::path::Path::new(&dir).join(DEFAULT_WAYLAND_SOCKET)).is_ok() }) } +fn wayland_socket_present() -> bool { + wayland_socket_connectable(std::env::var_os("XDG_RUNTIME_DIR")) +} + fn nvidia_driver_loaded() -> bool { std::path::Path::new(NVIDIA_DRIVER_VERSION_PATH).exists() } @@ -824,4 +826,49 @@ mod tests { RenderingPlan::PreserveEnvironment ); } + + // GDK_BACKEND unset is "*", so a live default socket opens wayland even with no + // WAYLAND_DISPLAY, and this must not be read as the X11 session FORCE_SHM is for. + #[test] + fn an_unset_gdk_backend_follows_the_default_socket() { + assert_eq!( + plan_on_host_with_socket(&[(X11_DISPLAY, ":0")], MODERN_WEBKIT, true, true, true), + RenderingPlan::Apply(RenderingWorkaround::DisableDmabuf, NVIDIA_WAYLAND_REASON) + ); + assert_eq!( + plan_on_host_with_socket(&[(X11_DISPLAY, ":0")], MODERN_WEBKIT, true, true, false), + RenderingPlan::Apply( + RenderingWorkaround::ForceSharedMemoryOnNvidia, + NVIDIA_REASON + ) + ); + } + + fn socket_dir(name: &str) -> std::path::PathBuf { + let dir = std::env::temp_dir().join(format!("unsloth-wl-{}-{name}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + dir + } + + #[test] + fn a_listening_wayland_socket_is_a_wayland_session() { + let dir = socket_dir("live"); + let _listener = + std::os::unix::net::UnixListener::bind(dir.join(DEFAULT_WAYLAND_SOCKET)).unwrap(); + assert!(wayland_socket_connectable(Some(dir.clone().into()))); + let _ = std::fs::remove_dir_all(&dir); + } + + // A crashed compositor leaves the file behind; GDK's opener still fails over to x11. + #[test] + fn a_stale_wayland_socket_is_not_a_wayland_session() { + let dir = socket_dir("stale"); + let socket = dir.join(DEFAULT_WAYLAND_SOCKET); + std::os::unix::net::UnixListener::bind(&socket).unwrap(); + assert!(socket.exists()); + assert!(!wayland_socket_connectable(Some(dir.clone().into()))); + assert!(!wayland_socket_connectable(None)); + let _ = std::fs::remove_dir_all(&dir); + } } From 216442eed22920930f6a821e7e05692c88920423 Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Mon, 24 Aug 2026 07:07:45 +0000 Subject: [PATCH 10/11] Probe the socket WAYLAND_DISPLAY names, not its presence, for PR #8884 --- studio/src-tauri/src/linux_webkit.rs | 127 +++++++++++++++++++++------ 1 file changed, 99 insertions(+), 28 deletions(-) diff --git a/studio/src-tauri/src/linux_webkit.rs b/studio/src-tauri/src/linux_webkit.rs index 363b1b5dd68e..cc454a228e53 100644 --- a/studio/src-tauri/src/linux_webkit.rs +++ b/studio/src-tauri/src/linux_webkit.rs @@ -9,6 +9,7 @@ const GLES_V2: &[u8] = b"libGLESv2.so.2\0"; const WAYLAND_DISPLAY: &str = "WAYLAND_DISPLAY"; const X11_DISPLAY: &str = "DISPLAY"; +const WAYLAND_SOCKET: &str = "WAYLAND_SOCKET"; const DEFAULT_WAYLAND_SOCKET: &str = "wayland-0"; const GDK_BACKEND: &str = "GDK_BACKEND"; const FORCE_SHARED_MEMORY: &str = "WEBKIT_DMABUF_RENDERER_FORCE_SHM"; @@ -65,18 +66,17 @@ enum RenderingPlan { /// runs on X11 whenever an X display opens, and on NVIDIA that decides between the /// shared-memory switch and the empty transport set. /// -/// A display cannot be opened here, so "opens" is the same environment GDK's own openers -/// read: `wayland_display` covers WAYLAND_DISPLAY and the default wayland-0 socket -/// wl_display_connect(NULL) falls back to. A named backend that cannot open is not the -/// selection, it is skipped, so wayland,x11 lands on X11 when no compositor answers. -fn selected_backend_is_wayland(backends: &OsStr, wayland_display: bool, x11_display: bool) -> bool { +/// A display cannot be opened here, so `wayland_open` is the socket probe below standing in +/// for GDK's own opener. A named backend that cannot open is not the selection, it is +/// skipped, so wayland,x11 lands on X11 when no compositor answers. +fn selected_backend_is_wayland(backends: &OsStr, wayland_open: bool, x11_display: bool) -> bool { let mut named_wayland = false; for backend in backends.to_string_lossy().split(',') { match backend { - "wayland" if wayland_display => return true, + "wayland" if wayland_open => return true, "wayland" => named_wayland = true, "x11" if x11_display => return false, - "*" if wayland_display => return true, + "*" if wayland_open => return true, "*" if x11_display => return false, _ => continue, } @@ -135,16 +135,13 @@ fn rendering_plan( .is_some_and(force_dmabuf_requested); let is_appimage = env(APPIMAGE).is_some(); - let wayland_display = env(WAYLAND_DISPLAY) - .map(|display| !display.is_empty()) - .unwrap_or(false); // What matters is which backend opens, not WAYLAND_DISPLAY, which XWayland clients - // inherit too. wl_display_connect(NULL) falls back to the default wayland-0 socket, so - // an unset WAYLAND_DISPLAY alone does not mean wayland cannot open. An unset - // GDK_BACKEND is GDK's own "*", which tries wayland before x11 (gdk_backends[]). + // inherit and a dead compositor leaves behind. `wayland_socket` is the answer from the + // socket wl_display_connect would actually use. An unset GDK_BACKEND is GDK's own "*", + // which tries wayland before x11 (gdk_backends[]). let wayland_session = selected_backend_is_wayland( env(GDK_BACKEND).as_deref().unwrap_or(OsStr::new("*")), - wayland_display || wayland_socket, + wayland_socket, env(X11_DISPLAY).is_some_and(|display| !display.is_empty()), ); @@ -219,18 +216,40 @@ fn rendering_plan( RenderingPlan::Apply(workaround, WAYLAND_REASON) } -/// Whether the socket wl_display_connect(NULL) falls back to accepts connections. A -/// crashed compositor leaves the file behind, and existence alone would then read as a -/// Wayland session while GDK's opener fails over to x11. Connecting instead fails the same -/// way GDK does (ECONNREFUSED), and touches no GL state, so it is safe before GTK init. -fn wayland_socket_connectable(runtime_dir: Option) -> bool { - runtime_dir.is_some_and(|dir| { - UnixStream::connect(std::path::Path::new(&dir).join(DEFAULT_WAYLAND_SOCKET)).is_ok() - }) +/// Whether the socket wl_display_connect would use accepts connections. Existence is not +/// enough and a set WAYLAND_DISPLAY is not either: a dead compositor leaves both behind, +/// and GDK's opener would then fail over to x11 while this said Wayland. Connecting fails +/// the same way GDK does, and touches no GL state, so it is safe before GTK init. +/// +/// Same resolution as libwayland: WAYLAND_SOCKET is an inherited fd and settles it on its +/// own, an absolute WAYLAND_DISPLAY is the path (1.15+), otherwise it is a name under +/// XDG_RUNTIME_DIR, defaulting to wayland-0. +fn wayland_socket_connectable( + runtime_dir: Option, + display: Option, + inherited_fd: bool, +) -> bool { + if inherited_fd { + return true; + } + let name = display.filter(|display| !display.is_empty()); + let path = match name { + Some(name) if std::path::Path::new(&name).is_absolute() => std::path::PathBuf::from(name), + name => match runtime_dir { + Some(dir) => std::path::Path::new(&dir) + .join(name.unwrap_or_else(|| DEFAULT_WAYLAND_SOCKET.into())), + None => return false, + }, + }; + UnixStream::connect(path).is_ok() } fn wayland_socket_present() -> bool { - wayland_socket_connectable(std::env::var_os("XDG_RUNTIME_DIR")) + wayland_socket_connectable( + std::env::var_os("XDG_RUNTIME_DIR"), + std::env::var_os(WAYLAND_DISPLAY), + std::env::var_os(WAYLAND_SOCKET).is_some(), + ) } fn nvidia_driver_loaded() -> bool { @@ -318,12 +337,16 @@ mod tests { gles_usable: bool, nvidia_driver_loaded: bool, ) -> RenderingPlan { + // A fixture that names a display means a live one, which is what the probe answers. + let live = vars + .iter() + .any(|(key, value)| *key == WAYLAND_DISPLAY && !value.is_empty()); plan_on_host_with_socket( vars, webkit_version, gles_usable, nvidia_driver_loaded, - false, + live, ) } @@ -856,19 +879,67 @@ mod tests { let dir = socket_dir("live"); let _listener = std::os::unix::net::UnixListener::bind(dir.join(DEFAULT_WAYLAND_SOCKET)).unwrap(); - assert!(wayland_socket_connectable(Some(dir.clone().into()))); + assert!(wayland_socket_connectable( + Some(dir.clone().into()), + None, + false + )); + // The same socket named explicitly, and named by absolute path as libwayland allows. + assert!(wayland_socket_connectable( + Some(dir.clone().into()), + Some(DEFAULT_WAYLAND_SOCKET.into()), + false + )); + assert!(wayland_socket_connectable( + None, + Some(dir.join(DEFAULT_WAYLAND_SOCKET).into()), + false + )); let _ = std::fs::remove_dir_all(&dir); } - // A crashed compositor leaves the file behind; GDK's opener still fails over to x11. + // A crashed compositor leaves the file behind, and WAYLAND_DISPLAY behind with it; + // GDK's opener still fails over to x11, so neither may read as a Wayland session. #[test] fn a_stale_wayland_socket_is_not_a_wayland_session() { let dir = socket_dir("stale"); let socket = dir.join(DEFAULT_WAYLAND_SOCKET); std::os::unix::net::UnixListener::bind(&socket).unwrap(); assert!(socket.exists()); - assert!(!wayland_socket_connectable(Some(dir.clone().into()))); - assert!(!wayland_socket_connectable(None)); + for display in [None, Some(OsString::from(DEFAULT_WAYLAND_SOCKET))] { + assert!(!wayland_socket_connectable( + Some(dir.clone().into()), + display, + false + )); + } + assert!(!wayland_socket_connectable(None, None, false)); + // A name with no runtime dir to resolve it against cannot open either. + assert!(!wayland_socket_connectable( + None, + Some(DEFAULT_WAYLAND_SOCKET.into()), + false + )); + // WAYLAND_SOCKET is an inherited fd: the connection is already made. + assert!(wayland_socket_connectable(None, None, true)); let _ = std::fs::remove_dir_all(&dir); } + + // The X11 arm of the same failure: WAYLAND_DISPLAY outlives its compositor, DISPLAY + // works, and DISABLE_DMABUF on an X11 webview is the empty-transport-set crash. + #[test] + fn a_dead_compositor_named_by_wayland_display_falls_back_to_x11() { + let session = &[(WAYLAND_DISPLAY, "wayland-0"), (X11_DISPLAY, ":0")]; + assert_eq!( + plan_on_host_with_socket(session, MODERN_WEBKIT, true, true, false), + RenderingPlan::Apply( + RenderingWorkaround::ForceSharedMemoryOnNvidia, + NVIDIA_REASON + ) + ); + assert_eq!( + plan_on_host_with_socket(session, MODERN_WEBKIT, true, false, false), + RenderingPlan::PreserveEnvironment + ); + } } From 5a411741f28d490123a9b81529d7d52c8c1e8e79 Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Mon, 24 Aug 2026 07:35:16 +0000 Subject: [PATCH 11/11] Probe the X display too, and keep an empty WAYLAND_DISPLAY off the default socket for PR #8884 --- studio/src-tauri/src/linux_webkit.rs | 170 ++++++++++++++++++++++++--- 1 file changed, 153 insertions(+), 17 deletions(-) diff --git a/studio/src-tauri/src/linux_webkit.rs b/studio/src-tauri/src/linux_webkit.rs index cc454a228e53..0ddf249c1406 100644 --- a/studio/src-tauri/src/linux_webkit.rs +++ b/studio/src-tauri/src/linux_webkit.rs @@ -10,6 +10,7 @@ const GLES_V2: &[u8] = b"libGLESv2.so.2\0"; const WAYLAND_DISPLAY: &str = "WAYLAND_DISPLAY"; const X11_DISPLAY: &str = "DISPLAY"; const WAYLAND_SOCKET: &str = "WAYLAND_SOCKET"; +const X11_SOCKET_DIR: &str = "/tmp/.X11-unix"; const DEFAULT_WAYLAND_SOCKET: &str = "wayland-0"; const GDK_BACKEND: &str = "GDK_BACKEND"; const FORCE_SHARED_MEMORY: &str = "WEBKIT_DMABUF_RENDERER_FORCE_SHM"; @@ -103,6 +104,7 @@ fn rendering_plan( gles_usable: bool, nvidia_driver_loaded: bool, wayland_socket: bool, + x11_open: bool, ) -> RenderingPlan { // Either renderer variable is an operator override when present, `=0` and an empty // value included, unless the marker says we wrote it. Without that test a launch @@ -135,14 +137,14 @@ fn rendering_plan( .is_some_and(force_dmabuf_requested); let is_appimage = env(APPIMAGE).is_some(); - // What matters is which backend opens, not WAYLAND_DISPLAY, which XWayland clients - // inherit and a dead compositor leaves behind. `wayland_socket` is the answer from the - // socket wl_display_connect would actually use. An unset GDK_BACKEND is GDK's own "*", - // which tries wayland before x11 (gdk_backends[]). + // What matters is which backend opens, not what WAYLAND_DISPLAY and DISPLAY say: both + // are inherited by clients of the other server and both outlive the thing they name. + // Each flag is the answer from a probe of the socket that opener would use. An unset + // GDK_BACKEND is GDK's own "*", which tries wayland before x11 (gdk_backends[]). let wayland_session = selected_backend_is_wayland( env(GDK_BACKEND).as_deref().unwrap_or(OsStr::new("*")), wayland_socket, - env(X11_DISPLAY).is_some_and(|display| !display.is_empty()), + x11_open, ); // The DMA-BUF transport breaks on the proprietary driver on either display server, so @@ -223,7 +225,9 @@ fn rendering_plan( /// /// Same resolution as libwayland: WAYLAND_SOCKET is an inherited fd and settles it on its /// own, an absolute WAYLAND_DISPLAY is the path (1.15+), otherwise it is a name under -/// XDG_RUNTIME_DIR, defaulting to wayland-0. +/// XDG_RUNTIME_DIR, defaulting to wayland-0. The fallback is on unset, not on empty, so +/// `WAYLAND_DISPLAY=` resolves to the runtime directory itself and cannot connect. That is +/// how a session forces itself through XWayland, and it must not read as Wayland. fn wayland_socket_connectable( runtime_dir: Option, display: Option, @@ -232,14 +236,14 @@ fn wayland_socket_connectable( if inherited_fd { return true; } - let name = display.filter(|display| !display.is_empty()); - let path = match name { - Some(name) if std::path::Path::new(&name).is_absolute() => std::path::PathBuf::from(name), - name => match runtime_dir { - Some(dir) => std::path::Path::new(&dir) - .join(name.unwrap_or_else(|| DEFAULT_WAYLAND_SOCKET.into())), + let name = display.unwrap_or_else(|| DEFAULT_WAYLAND_SOCKET.into()); + let path = if std::path::Path::new(&name).is_absolute() { + std::path::PathBuf::from(name) + } else { + match runtime_dir { + Some(dir) => std::path::Path::new(&dir).join(name), None => return false, - }, + } }; UnixStream::connect(path).is_ok() } @@ -248,10 +252,42 @@ fn wayland_socket_present() -> bool { wayland_socket_connectable( std::env::var_os("XDG_RUNTIME_DIR"), std::env::var_os(WAYLAND_DISPLAY), - std::env::var_os(WAYLAND_SOCKET).is_some(), + // An empty value is not an fd; libwayland fails the parse rather than falling back. + std::env::var_os(WAYLAND_SOCKET).is_some_and(|fd| !fd.is_empty()), ) } +/// Whether the X display DISPLAY names accepts connections, so the x11 arm is decided the +/// same way as the wayland one rather than on the variable alone. Deliberately fail-safe: +/// only a local `:N` display is probed, and only both of Xorg's listeners refusing counts +/// as closed. Guessing "closed" for a live server would put DISABLE_DMABUF on an X11 +/// webview, which is the empty transport set, so anything unrecognized stays trusted. +fn x11_display_open(display: Option, socket_dir: &str) -> bool { + let Some(display) = display.filter(|display| !display.is_empty()) else { + return false; + }; + let text = display.to_string_lossy().into_owned(); + let Some((host, number)) = text.rsplit_once(':') else { + return true; + }; + let number = number.split('.').next().unwrap_or_default(); + if !matches!(host, "" | "unix") + || number.is_empty() + || !number.bytes().all(|b| b.is_ascii_digit()) + { + return true; // a TCP or otherwise unusual display; not ours to judge + } + let path = format!("{socket_dir}/X{number}"); + if UnixStream::connect(&path).is_ok() { + return true; + } + // Xorg binds the abstract name too, and on some hosts only that one is reachable. + use std::os::linux::net::SocketAddrExt; + std::os::unix::net::SocketAddr::from_abstract_name(path.as_bytes()) + .and_then(|address| UnixStream::connect_addr(&address)) + .is_ok() +} + fn nvidia_driver_loaded() -> bool { std::path::Path::new(NVIDIA_DRIVER_VERSION_PATH).exists() } @@ -304,6 +340,7 @@ pub fn configure_renderer() -> Option<(&'static [&'static str], &'static str)> { gles_usable, nvidia_driver_loaded(), wayland_socket_present(), + x11_display_open(std::env::var_os(X11_DISPLAY), X11_SOCKET_DIR), ) { RenderingPlan::Apply(workaround, reason) => { let variables = workaround.variables(); @@ -338,9 +375,7 @@ mod tests { nvidia_driver_loaded: bool, ) -> RenderingPlan { // A fixture that names a display means a live one, which is what the probe answers. - let live = vars - .iter() - .any(|(key, value)| *key == WAYLAND_DISPLAY && !value.is_empty()); + let live = named(vars, WAYLAND_DISPLAY); plan_on_host_with_socket( vars, webkit_version, @@ -356,6 +391,26 @@ mod tests { gles_usable: bool, nvidia_driver_loaded: bool, wayland_socket: bool, + ) -> RenderingPlan { + // As above for DISPLAY: a fixture that names one models a server that answers. + let x11_open = named(vars, X11_DISPLAY); + plan_on_displays( + vars, + webkit_version, + gles_usable, + nvidia_driver_loaded, + wayland_socket, + x11_open, + ) + } + + fn plan_on_displays( + vars: &[(&str, &str)], + webkit_version: (u32, u32, u32), + gles_usable: bool, + nvidia_driver_loaded: bool, + wayland_socket: bool, + x11_open: bool, ) -> RenderingPlan { rendering_plan( |name| { @@ -367,9 +422,15 @@ mod tests { gles_usable, nvidia_driver_loaded, wayland_socket, + x11_open, ) } + fn named(vars: &[(&str, &str)], variable: &str) -> bool { + vars.iter() + .any(|(key, value)| *key == variable && !value.is_empty()) + } + fn plan_with_version_and_gles( vars: &[(&str, &str)], webkit_version: (u32, u32, u32), @@ -925,6 +986,81 @@ mod tests { let _ = std::fs::remove_dir_all(&dir); } + // WAYLAND_DISPLAY= is how a session forces itself through XWayland. libwayland falls + // back to wayland-0 on unset, not on empty, so the empty value must not reach the + // still-live default socket and read as Wayland. + #[test] + fn an_explicitly_empty_wayland_display_is_not_the_default_socket() { + let dir = socket_dir("empty-display"); + let _listener = + std::os::unix::net::UnixListener::bind(dir.join(DEFAULT_WAYLAND_SOCKET)).unwrap(); + assert!(wayland_socket_connectable( + Some(dir.clone().into()), + None, + false + )); + assert!(!wayland_socket_connectable( + Some(dir.clone().into()), + Some(OsString::new()), + false + )); + // Nor is an empty WAYLAND_SOCKET an inherited fd. + assert!(!wayland_socket_connectable( + None, + Some(OsString::new()), + false + )); + let _ = std::fs::remove_dir_all(&dir); + } + + // DISPLAY names a server that may be gone; only a local display is judged, and only + // when both of Xorg's listeners refuse, since guessing closed for a live server would + // put DISABLE_DMABUF on an X11 webview. + #[test] + fn only_a_local_x_display_that_answers_nothing_reads_as_closed() { + let dir = socket_dir("x11"); + let sockets = dir.to_string_lossy().into_owned(); + let probe = |display: &str| x11_display_open(Some(display.into()), &sockets); + + assert!(!x11_display_open(None, &sockets)); + assert!(!x11_display_open(Some(OsString::new()), &sockets)); + // Nothing has bound :0 in this directory yet. + assert!(!probe(":0")); + let _listener = std::os::unix::net::UnixListener::bind(dir.join("X0")).unwrap(); + assert!(probe(":0")); + assert!(probe(":0.1"), "the screen suffix is not part of the socket"); + // Abstract only, which is all some hosts publish. + use std::os::linux::net::SocketAddrExt; + let abstract_name = + std::os::unix::net::SocketAddr::from_abstract_name(format!("{sockets}/X1")).unwrap(); + let _abstract = std::os::unix::net::UnixListener::bind_addr(&abstract_name).unwrap(); + assert!(probe(":1")); + assert!(!probe(":2")); + // Not ours to judge: a remote display, a screen number that is not one, a path. + assert!(probe("somehost:2")); + assert!(probe(":abc")); + assert!(probe("/tmp/.X11-unix/X2")); + let _ = std::fs::remove_dir_all(&dir); + } + + // A dead X server with an explicit x11-first order: GDK skips the opener that fails and + // lands on wayland, so the plan has to land there too. + #[test] + fn a_dead_x_server_lets_an_x11_first_order_fall_through_to_wayland() { + let session = &[(GDK_BACKEND, "x11,wayland"), (X11_DISPLAY, ":0")]; + assert_eq!( + plan_on_displays(session, MODERN_WEBKIT, true, true, true, false), + RenderingPlan::Apply(RenderingWorkaround::DisableDmabuf, NVIDIA_WAYLAND_REASON) + ); + assert_eq!( + plan_on_displays(session, MODERN_WEBKIT, true, true, true, true), + RenderingPlan::Apply( + RenderingWorkaround::ForceSharedMemoryOnNvidia, + NVIDIA_REASON + ) + ); + } + // The X11 arm of the same failure: WAYLAND_DISPLAY outlives its compositor, DISPLAY // works, and DISABLE_DMABUF on an X11 webview is the empty-transport-set crash. #[test]