From 65fce7bcf03a41602a99cf17f14942c6665f9de3 Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Wed, 18 Nov 2026 15:03:58 +0800 Subject: [PATCH] feat(gpapi): recover from WebKit GBM buffer failures --- crates/gpapi/Cargo.toml | 4 +- crates/gpapi/src/gui.rs | 46 +++++++-- crates/gpapi/src/process/auth_launcher.rs | 77 +++++++++++++-- crates/gpapi/src/process/desktop_child.rs | 22 ++++- crates/gpapi/src/process/desktop_launcher.rs | 6 +- .../gpapi/src/process/gui_helper_launcher.rs | 1 + crates/gpapi/src/process/gui_launcher.rs | 72 +++++++++----- .../gpapi/src/process/rendering_recovery.rs | 96 +++++++++++++++++-- 8 files changed, 271 insertions(+), 53 deletions(-) diff --git a/crates/gpapi/Cargo.toml b/crates/gpapi/Cargo.toml index 8e0af06..e3d402f 100644 --- a/crates/gpapi/Cargo.toml +++ b/crates/gpapi/Cargo.toml @@ -64,8 +64,10 @@ oslog = { version = "0.2", default-features = false, optional = true } [target.'cfg(not(any(target_os="macos", target_os="windows")))'.dependencies] gtk = { version = "0.18", optional = true } -[target.'cfg(target_os="linux")'.dependencies] +[target.'cfg(any(target_os="linux", target_os="freebsd", target_os="openbsd"))'.dependencies] directories = { workspace = true, optional = true } + +[target.'cfg(target_os="linux")'.dependencies] libloading = { version = "0.9", optional = true } [target.'cfg(target_os="macos")'.dependencies] diff --git a/crates/gpapi/src/gui.rs b/crates/gpapi/src/gui.rs index 4be36bb..44a475f 100644 --- a/crates/gpapi/src/gui.rs +++ b/crates/gpapi/src/gui.rs @@ -70,17 +70,17 @@ impl GuiVariant { } } -#[cfg(target_os = "linux")] -fn preference_path() -> anyhow::Result { +#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))] +fn preference_path(name: &str) -> anyhow::Result { let dirs = directories::ProjectDirs::from("com.yuezk", "GlobalProtect-openconnect", "gpclient") .ok_or_else(|| anyhow::anyhow!("Failed to locate GUI preference directory"))?; - Ok(dirs.config_dir().join("gui")) + Ok(dirs.config_dir().join(name)) } pub fn preferred() -> anyhow::Result { #[cfg(target_os = "linux")] { - match std::fs::read_to_string(preference_path()?) { + match std::fs::read_to_string(preference_path("gui")?) { Ok(value) => match value.trim() { "gpgui" => Ok(GuiVariant::Tauri), "gpgui-nsh" => Ok(GuiVariant::Nsh), @@ -96,12 +96,46 @@ pub fn preferred() -> anyhow::Result { #[cfg(target_os = "linux")] pub fn save_preference(variant: GuiVariant) -> anyhow::Result<()> { + save_launch_preference("gui", variant.binary_name()) +} + +pub const COMPATIBILITY_GRAPHICS_SUPPORTED: bool = + cfg!(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd")); + +pub const FORCE_SHARED_MEMORY: &str = "WEBKIT_DMABUF_RENDERER_FORCE_SHM"; + +const COMPATIBILITY_GRAPHICS: &str = "compatibility-graphics"; + +#[cfg(not(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd")))] +fn preference_path(_: &str) -> anyhow::Result { + anyhow::bail!("Compatibility graphics is unavailable") +} + +pub fn compatibility_graphics_enabled() -> bool { + COMPATIBILITY_GRAPHICS_SUPPORTED && preference_path(COMPATIBILITY_GRAPHICS).is_ok_and(|path| path.is_file()) +} + +pub fn set_compatibility_graphics(enabled: bool) -> anyhow::Result<()> { + anyhow::ensure!( + COMPATIBILITY_GRAPHICS_SUPPORTED, + "Compatibility graphics is unavailable" + ); + if enabled { + return save_launch_preference(COMPATIBILITY_GRAPHICS, "enabled"); + } + match std::fs::remove_file(preference_path(COMPATIBILITY_GRAPHICS)?) { + Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()), + result => result.map_err(Into::into), + } +} + +fn save_launch_preference(name: &str, value: &str) -> anyhow::Result<()> { use std::io::Write; - let path = preference_path()?; + let path = preference_path(name)?; let directory = path.parent().unwrap(); std::fs::create_dir_all(directory)?; let mut file = tempfile::NamedTempFile::new_in(directory)?; - writeln!(file, "{}", variant.binary_name())?; + writeln!(file, "{value}")?; file.persist(path)?; Ok(()) } diff --git a/crates/gpapi/src/process/auth_launcher.rs b/crates/gpapi/src/process/auth_launcher.rs index fdd51c5..02aa81a 100644 --- a/crates/gpapi/src/process/auth_launcher.rs +++ b/crates/gpapi/src/process/auth_launcher.rs @@ -17,7 +17,10 @@ use super::{ command_traits::CommandExt, }; -use super::rendering_recovery::{RenderingCrash, StderrDiagnostic, eligible, is_crash_exit}; +use super::rendering_recovery::{ + AUTH_STDERR_PREFIX, GbmBufferFailure, RenderingCrash, StderrDiagnostic, allows_shared_memory_override, eligible, + is_crash_exit, is_gbm_diagnostic, +}; const MAX_AUTH_RESULT_BYTES: usize = 256 * 1024; @@ -53,6 +56,7 @@ pub struct SamlAuthLauncher<'a> { verbose: Option<&'a str>, log_format: LogFormat, dmabuf_disabled: bool, + desktop_compatibility_graphics: bool, } impl<'a> SamlAuthLauncher<'a> { @@ -85,9 +89,15 @@ impl<'a> SamlAuthLauncher<'a> { verbose: None, log_format: LogFormat::Text, dmabuf_disabled: false, + desktop_compatibility_graphics: false, } } + pub fn desktop_compatibility_graphics(mut self) -> Self { + self.desktop_compatibility_graphics = true; + self + } + pub fn dmabuf_disabled(mut self, disabled: bool) -> Self { self.dmabuf_disabled = disabled; self @@ -298,41 +308,68 @@ impl<'a> SamlAuthLauncher<'a> { if embedded && bundled && self.dmabuf_disabled { command.env("WEBKIT_DISABLE_DMABUF_RENDERER", "1"); } + let desktop_webview = embedded && bundled && self.desktop_compatibility_graphics; + let can_use_shared_memory = desktop_webview && allows_shared_memory_override(&command, true); + let use_shared_memory = can_use_shared_memory && crate::gui::compatibility_graphics_enabled(); + if use_shared_memory { + command.env(crate::gui::FORCE_SHARED_MEMORY, "1"); + } + let detect_gbm_failure = can_use_shared_memory && !use_shared_memory; let stdin = auth_stdin(self.browser); let cancellation = self.cancellation.clone(); let output = tokio::task::spawn_blocking(move || { + #[cfg(feature = "logger")] + let stderr = crate::logger::diagnostic_writer(); + #[cfg(not(feature = "logger"))] + let stderr: Box = Box::new(std::io::stderr()); + let mut stderr = if desktop_webview { + Box::new(std::io::LineWriter::new(AuthStderr(stderr))) + } else { + stderr + }; + let gbm_failed = std::cell::Cell::new(false); let check = || { if cancellation.is_cancelled() { Err(std::io::Error::new( std::io::ErrorKind::Interrupted, "Authentication cancelled", )) + } else if gbm_failed.get() { + Err(std::io::Error::other(GbmBufferFailure)) } else { Ok(()) } }; let mut diagnostic = StderrDiagnostic::default(); - #[cfg(feature = "logger")] - let mut stderr = crate::logger::diagnostic_writer(); - #[cfg(not(feature = "logger"))] - let mut stderr = std::io::stderr(); let output = run_observed( command, &check, Some(MAX_AUTH_RESULT_BYTES), - &mut |bytes| diagnostic.push(bytes), + &mut |bytes| { + diagnostic.push(bytes); + gbm_failed.set(detect_gbm_failure && diagnostic.should_retry_gbm(can_recover)); + }, &mut stderr, stdin, - )?; - Ok::<_, std::io::Error>((output.output, diagnostic.matched())) + ); + std::io::Write::flush(&mut stderr)?; + if gbm_failed.get() && !cancellation.is_cancelled() { + return Err(GbmBufferFailure.into()); + } + let output = output?; + Ok::<_, anyhow::Error>((output.output, diagnostic.matched())) }) .await?; let (output, rendering_diagnostic) = match output { Ok(output) => output, - Err(error) if error.kind() == std::io::ErrorKind::Interrupted => { + Err(error) + if error + .downcast_ref::() + .is_some_and(|error| error.kind() == std::io::ErrorKind::Interrupted) => + { return Err(crate::auth::AuthenticationCancelled.into()); } - Err(error) => return Err(error.into()), + Err(error) => return Err(error), }; if self.cancellation.is_cancelled() { @@ -342,6 +379,26 @@ impl<'a> SamlAuthLauncher<'a> { } } +// Attribute GBM failures to gpauth before forwarding them to the desktop supervisor. +struct AuthStderr(Box); + +impl std::io::Write for AuthStderr { + fn write(&mut self, bytes: &[u8]) -> std::io::Result { + for line in bytes.split_inclusive(|&byte| byte == b'\n') { + if is_gbm_diagnostic(line) { + self.0.write_all(&[AUTH_STDERR_PREFIX, line].concat())?; + } else { + self.0.write_all(line)?; + } + } + Ok(bytes.len()) + } + + fn flush(&mut self) -> std::io::Result<()> { + self.0.flush() + } +} + /// `gpauth --browser manual` reads the pasted `globalprotectcallback:` data from the user's terminal. fn auth_stdin(browser: Option<&str>) -> ChildInput<'static> { match browser { diff --git a/crates/gpapi/src/process/desktop_child.rs b/crates/gpapi/src/process/desktop_child.rs index a5beb9b..88ba362 100644 --- a/crates/gpapi/src/process/desktop_child.rs +++ b/crates/gpapi/src/process/desktop_child.rs @@ -1,10 +1,14 @@ use super::{ command_runner::{ChildInput, run_observed}, - rendering_recovery::{RenderingCrash, StderrDiagnostic, eligible_with_inheritance, is_crash_exit}, + rendering_recovery::{ + GbmBufferFailure, RenderingCrash, StderrDiagnostic, allows_shared_memory_override, eligible_with_inheritance, + is_crash_exit, + }, }; use crate::service::transport::SessionCredential; use anyhow::Context; use std::{ + cell::Cell, collections::HashMap, fs::OpenOptions, path::PathBuf, @@ -19,8 +23,10 @@ pub(super) async fn launch_desktop_child( envs: Option<&HashMap>, credential: &SessionCredential, cancellation: &CancellationToken, + detect_gbm_failure: bool, ) -> anyhow::Result { let retry_eligible = eligible_with_inheritance(&command, envs.is_none()); + let detect_gbm_failure = detect_gbm_failure && allows_shared_memory_override(&command, envs.is_none()); let log_file = match envs { Some(envs) => envs.get("GP_LOG_FILE").map(PathBuf::from), None => std::env::var_os("GP_LOG_FILE").map(PathBuf::from), @@ -38,12 +44,15 @@ pub(super) async fn launch_desktop_child( let cancellation = cancellation.clone(); tokio::task::spawn_blocking(move || { + let gbm_failed = Cell::new(false); let check = || { if cancellation.is_cancelled() { Err(std::io::Error::new( std::io::ErrorKind::Interrupted, "Desktop child launch cancelled", )) + } else if gbm_failed.get() { + Err(std::io::Error::other(GbmBufferFailure)) } else { Ok(()) } @@ -53,10 +62,17 @@ pub(super) async fn launch_desktop_child( command, &check, None, - &mut |bytes| diagnostic.push(bytes), + &mut |bytes| { + diagnostic.push(bytes); + gbm_failed.set(detect_gbm_failure && diagnostic.should_retry_gbm(retry_eligible)); + }, &mut sink, ChildInput::Frame(&frame), - )?; + ); + if gbm_failed.get() && !cancellation.is_cancelled() { + return Err(GbmBufferFailure.into()); + } + let observed = observed?; let output = observed.output; if is_crash_exit(output.status) && retry_eligible && diagnostic.matched() { return Err(RenderingCrash { status: output.status }.into()); diff --git a/crates/gpapi/src/process/desktop_launcher.rs b/crates/gpapi/src/process/desktop_launcher.rs index b7c6bc1..0668e05 100644 --- a/crates/gpapi/src/process/desktop_launcher.rs +++ b/crates/gpapi/src/process/desktop_launcher.rs @@ -170,7 +170,11 @@ impl<'a> DesktopLauncher<'a> { } match outcome { Err(error) if recovery.recover(&error) => { - warn!("{error}; restarting the desktop launch with DMABUF disabled and opaque windows"); + if recovery.shared_memory() { + warn!("{error}; restarting the desktop launch with compatibility graphics"); + } else { + warn!("{error}; restarting the desktop launch with DMABUF disabled and opaque windows"); + } expecting_renewal = true; input.write_all(&[DESKTOP_RENDERING_RETRY]).await?; } diff --git a/crates/gpapi/src/process/gui_helper_launcher.rs b/crates/gpapi/src/process/gui_helper_launcher.rs index 68a1361..0c969ba 100644 --- a/crates/gpapi/src/process/gui_helper_launcher.rs +++ b/crates/gpapi/src/process/gui_helper_launcher.rs @@ -79,6 +79,7 @@ impl<'a> GuiHelperLauncher<'a> { self.envs, self.credential, cancellation, + false, ) .await?; Ok(()) diff --git a/crates/gpapi/src/process/gui_launcher.rs b/crates/gpapi/src/process/gui_launcher.rs index 9baaf0e..287bcc7 100644 --- a/crates/gpapi/src/process/gui_launcher.rs +++ b/crates/gpapi/src/process/gui_launcher.rs @@ -101,6 +101,12 @@ impl<'a> GuiLauncher<'a> { if let Some(envs) = &self.envs { cmd.env_clear().envs(envs); } + if self.variant == crate::gui::GuiVariant::Tauri + && crate::gui::compatibility_graphics_enabled() + && super::rendering_recovery::allows_shared_memory_override(cmd.as_std(), self.envs.is_none()) + { + cmd.env(crate::gui::FORCE_SHARED_MEMORY, "1"); + } cmd.arg("--service-credential-on-stdin"); if self.minimized { cmd.arg("--minimized"); @@ -112,6 +118,7 @@ impl<'a> GuiLauncher<'a> { self.envs.as_ref(), &self.credential, cancellation, + self.variant == crate::gui::GuiVariant::Tauri, ) .await } @@ -207,32 +214,45 @@ impl<'a> GuiLauncher<'a> { gui_switch::reply(&mut socket, Ok(GuiSwitchOutcome::DownloadRequired)).await?; return Ok(None); } - service_input.write_all(&[DESKTOP_SWITCH_BEGIN]).await?; - pending = true; - let credential = super::desktop_launcher::read_credential(service_output).await? - .ok_or_else(|| anyhow::anyhow!("Service closed before issuing the switch credential"))?; - anyhow::ensure!(credential.product_version() == self.version, "Service product version mismatch"); - let ready = gui_switch::SwitchListener::new()?; - let mut envs = self.envs.clone().unwrap_or_else(|| std::env::vars().collect()); - envs.insert(gui_switch::READY_ENV.into(), ready.path().to_string_lossy().into_owned()); - let launcher = Self::new(self.version, Arc::new(credential), variant) - .envs(envs) - .minimized(false); - let program = match program { - Some(program) => program, - None => launcher.resolve_program(cancellation, command.theme).await?, - }; - let mut child = launcher.start_owned(program, cancellation.child_token()); - let start_result = tokio::select! { - result = tokio::time::timeout(std::time::Duration::from_secs(30), ready.ready()) => { - result.map_err(|_| anyhow::anyhow!("The selected GUI did not finish starting" )).and_then(|result| result) - }, - outcome = &mut child.task => { - return match outcome? { - Ok(status) => Err(anyhow::anyhow!("The selected GUI exited during startup: {status}")), - Err(error) => Err(error), - }; - } + use super::rendering_recovery::{GbmBufferFailure, RecoveryState}; + let mut recovery = RecoveryState::default(); + let mut program = program; + let (mut child, start_result) = loop { + service_input.write_all(&[DESKTOP_SWITCH_BEGIN]).await?; + pending = true; + let credential = super::desktop_launcher::read_credential(service_output).await? + .ok_or_else(|| anyhow::anyhow!("Service closed before issuing the switch credential"))?; + anyhow::ensure!(credential.product_version() == self.version, "Service product version mismatch"); + let ready = gui_switch::SwitchListener::new()?; + let mut envs = self.envs.clone().unwrap_or_else(|| std::env::vars().collect()); + envs.insert(gui_switch::READY_ENV.into(), ready.path().to_string_lossy().into_owned()); + let launcher = Self::new(self.version, Arc::new(credential), variant) + .envs(envs) + .minimized(false); + let executable = match &program { + Some(program) => program.clone(), + None => launcher.resolve_program(cancellation, command.theme).await?, + }; + let mut child = launcher.start_owned(executable.clone(), cancellation.child_token()); + let start_result = tokio::select! { + result = tokio::time::timeout(std::time::Duration::from_secs(30), ready.ready()) => { + result.map_err(|_| anyhow::anyhow!("The selected GUI did not finish starting" )).and_then(|result| result) + }, + outcome = &mut child.task => { + match outcome? { + Ok(status) => return Err(anyhow::anyhow!("The selected GUI exited during startup: {status}")), + Err(error) if error.is::() && recovery.recover(&error) => { + log::warn!("{error}; restarting the selected GUI with compatibility graphics"); + service_input.write_all(&[DESKTOP_SWITCH_CANCEL]).await?; + pending = false; + program = Some(executable); + continue; + } + Err(error) => return Err(recovery.terminal(error)), + } + } + }; + break (child, start_result); }; if let Err(error) = start_result.and_then(|()| crate::gui::save_preference(variant)) { child.cancel.cancel(); diff --git a/crates/gpapi/src/process/rendering_recovery.rs b/crates/gpapi/src/process/rendering_recovery.rs index 615668f..e523be9 100644 --- a/crates/gpapi/src/process/rendering_recovery.rs +++ b/crates/gpapi/src/process/rendering_recovery.rs @@ -1,6 +1,9 @@ use std::process::{Command, ExitStatus}; const DMABUF: &str = "WEBKIT_DISABLE_DMABUF_RENDERER"; +pub const MAIN_WEBVIEW_INITIALIZED: &str = "GP Connect main webview initialized"; +const GBM_DIAGNOSTIC: &[u8] = b"Failed to create GBM buffer"; +pub(crate) const AUTH_STDERR_PREFIX: &[u8] = b"gpauth stderr: "; const DIAGNOSTIC: &[u8] = b"Error 71 (Protocol error) dispatching to Wayland display"; #[derive(Debug, thiserror::Error)] @@ -16,20 +19,33 @@ pub struct RecoveryFailed(#[source] anyhow::Error); #[derive(Default)] pub struct RecoveryState { first_failure: Option, + shared_memory: bool, } impl RecoveryState { pub fn dmabuf_disabled(&self) -> bool { - self.first_failure.is_some() + self.first_failure.is_some() && !self.shared_memory + } + + pub fn shared_memory(&self) -> bool { + self.shared_memory } pub fn recover(&mut self, error: &anyhow::Error) -> bool { - if self.first_failure.is_none() && error.downcast_ref::().is_some() { - self.first_failure = Some(error.to_string()); - true - } else { - false + if self.first_failure.is_some() { + return false; } + let gbm_failure = error.downcast_ref::().is_some(); + if !gbm_failure && error.downcast_ref::().is_none() { + return false; + } + if gbm_failure && let Err(error) = crate::gui::set_compatibility_graphics(true) { + log::warn!("Could not save compatibility graphics setting: {error:#}"); + return false; + } + self.first_failure = Some(error.to_string()); + self.shared_memory = gbm_failure; + true } pub fn terminal(&self, error: anyhow::Error) -> anyhow::Error { @@ -70,6 +86,16 @@ pub(crate) fn eligible_with_inheritance(command: &Command, inherits: bool) -> bo && effective_env(command, DMABUF, inherits).is_none() } +pub(crate) fn allows_shared_memory_override(command: &Command, inherits: bool) -> bool { + crate::gui::COMPATIBILITY_GRAPHICS_SUPPORTED + && effective_env(command, DMABUF, inherits).is_none() + && effective_env(command, crate::gui::FORCE_SHARED_MEMORY, inherits).is_none() +} + +#[derive(Debug, thiserror::Error)] +#[error("WebKit failed to create a GBM buffer")] +pub(crate) struct GbmBufferFailure; + fn effective_env(command: &Command, key: &str, inherits: bool) -> Option { for (name, value) in command.get_envs() { if name == key { @@ -79,12 +105,21 @@ fn effective_env(command: &Command, key: &str, inherits: bool) -> Option bool { + !line.starts_with(AUTH_STDERR_PREFIX) + && line + .windows(GBM_DIAGNOSTIC.len()) + .any(|message| message == GBM_DIAGNOSTIC) +} + /// Observes diagnostic lines without retaining arbitrary application output. #[derive(Default)] pub(crate) struct StderrDiagnostic { line: Vec, overflow: bool, matched: bool, + gbm_failed: bool, + main_webview_initialized: bool, } impl StderrDiagnostic { @@ -104,6 +139,14 @@ impl StderrDiagnostic { fn check_line(&mut self) { let line = self.line.trim_ascii_end(); + if !self.overflow { + if line == MAIN_WEBVIEW_INITIALIZED.as_bytes() { + self.main_webview_initialized = true; + } + if !self.main_webview_initialized && is_gbm_diagnostic(line) { + self.gbm_failed = true; + } + } let message = line.strip_suffix(b".").unwrap_or(line); if !self.overflow && (line.starts_with(b"Gdk-Message:") @@ -117,6 +160,10 @@ impl StderrDiagnostic { } } + pub(crate) fn should_retry_gbm(&self, can_retry_wayland: bool) -> bool { + self.gbm_failed && !(can_retry_wayland && self.matched) + } + pub(crate) fn matched(&mut self) -> bool { self.check_line(); self.matched @@ -127,6 +174,43 @@ impl StderrDiagnostic { mod tests { use super::*; + #[test] + fn detects_gbm_failure_before_main_webview_initializes() { + for line in [ + b"Failed to create GBM buffer of size 280x404: Invalid argument\n".as_slice(), + b"(WebKitWebProcess:42): Warning: Failed to create GBM buffer\n", + b"gpauth: Failed to create GBM buffer\n", + ] { + let mut observer = StderrDiagnostic::default(); + for bytes in line.chunks(7) { + observer.push(bytes); + } + assert!(observer.gbm_failed); + assert!(!observer.matched()); + } + } + + #[test] + fn ignores_gbm_failure_after_startup_and_from_auth_stderr() { + let mut observer = StderrDiagnostic::default(); + observer + .push(b"gpauth stderr: (WebKitWebProcess:42): Failed to create GBM buffer of size 900x648: Invalid argument\n"); + assert!(!observer.gbm_failed); + observer.push(format!("{MAIN_WEBVIEW_INITIALIZED}\n").as_bytes()); + observer.push(b"Failed to create GBM buffer of size 840x640: Invalid argument\n"); + assert!(!observer.gbm_failed); + } + + #[test] + fn preserves_wayland_retry_when_both_diagnostics_are_observed() { + let mut observer = StderrDiagnostic::default(); + observer.push(b"Failed to create GBM buffer of size 280x404: Invalid argument\n"); + observer.push(b"Gdk-Message: Error 71 (Protocol error) dispatching to Wayland display.\n"); + assert!(observer.matched()); + assert!(!observer.should_retry_gbm(true)); + assert!(observer.should_retry_gbm(false)); + } + #[test] fn recognizes_split_diagnostic_without_matching_quoted_warning() { let mut observer = StderrDiagnostic::default();