diff --git a/CHANGELOG.md b/CHANGELOG.md index 56579db..6715b9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,7 +20,9 @@ User-visible changes of the apps and the `tsgw` gateway. The format follows every token, a few widgets and the icons (`VOELIN_DEV_THEME`, `VOELIN_FONT_SCALE`, `VOELIN_WINDOW_SIZE`; screenshots with `scripts/gpui-shot.sh`, docs/ui/). `VOELIN_METER=1` logs what it costs once a - second: the CPU by group of threads, the memory and its own counters. The Slint app (`voelin`) is unchanged and + second: the CPU by group of threads, the memory and its own counters. A + window that cannot open (no driver, no display server) ends the app with + the reason and exit status 1. The Slint app (`voelin`) is unchanged and stays the app; both run on one engine until the GPUI one reaches parity (docs/ui.md). It needs Vulkan or OpenGL at run time (Mesa's lavapipe renders in software); no package ships it yet. diff --git a/crates/voelin-gpui/src/lib.rs b/crates/voelin-gpui/src/lib.rs index 76743b0..ecbc673 100644 --- a/crates/voelin-gpui/src/lib.rs +++ b/crates/voelin-gpui/src/lib.rs @@ -77,6 +77,14 @@ fn open_main_window( engine: HostedEngine, counters: Counters, ) -> Result<()> { + // Without a display server GPUI falls back to a headless platform, which + // opens windows that draw nothing and runs until it is stopped. + if cx.compositor_name() == "headless" { + return Err(anyhow::anyhow!( + "no display: neither DISPLAY nor WAYLAND_DISPLAY names a display server" + )) + .context("cannot open the main window"); + } let HostedEngine { engine, events, .. } = engine; let app = cx.new({ let counters = counters.clone(); @@ -140,7 +148,12 @@ pub fn run() -> Result<()> { ); if let Err(e) = open_main_window(cx, &switches, hosted, counters) { *failure.borrow_mut() = Some(e); - cx.quit(); + // Not `cx.quit()` here: GPUI's Linux platform runs this callback + // before it starts its event loop, and the loop forgets a stop + // asked for earlier (calloop clears its stop signal when it + // starts), so the process would stay with no window and no + // message. A task runs inside the loop. + cx.spawn(async move |cx| cx.update(|cx| cx.quit())).detach(); } } }); diff --git a/crates/voelin-gpui/tests/launch_failure.rs b/crates/voelin-gpui/tests/launch_failure.rs new file mode 100644 index 0000000..2b860b9 --- /dev/null +++ b/crates/voelin-gpui/tests/launch_failure.rs @@ -0,0 +1,154 @@ +//! The binary's failure path, end to end: when the main window cannot be +//! opened (no display server, no Vulkan or GL driver), `voelin-gpui` prints +//! why and exits with status 1 instead of staying alive with no window and no +//! message. +//! +//! It has to start the real binary. GPUI's Linux platform runs the launch +//! callback before it starts its event loop, and the loop forgets a quit asked +//! for before it started, so a failure handled by quitting inside the +//! callback hung the process; the test platform of the unit tests has no such +//! loop and cannot show it. +//! +//! The no-driver test needs a test X server in `VOELIN_X11_TEST_DISPLAY` (an +//! Xvfb, as CI has; never the desktop's display) and is skipped without one. + +#![cfg(target_os = "linux")] + +use std::fs; +use std::os::unix::fs::PermissionsExt as _; +use std::path::{Path, PathBuf}; +use std::process::{Command, ExitStatus, Stdio}; +use std::time::{Duration, Instant}; + +/// Where the drivers are not: the Vulkan loader, EGL's vendor library +/// directory and Mesa's driver directory all point here. +const NOWHERE: &str = "/nonexistent-voelin-gpui-test"; + +/// Loaded machines are slow, so the wait is generous; a run that works takes a +/// second or two. +const WAIT: Duration = Duration::from_secs(120); + +/// The scratch directory of one run, removed at the end. +struct Scratch(PathBuf); + +impl Drop for Scratch { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.0); + } +} + +/// How a run of the binary went. +enum Outcome { + /// It ended, with this status and this much of the log. + Ended(ExitStatus, String), + /// It picked a GPU: the window can open, so nothing failed. + Opened, + /// It was still running after [`WAIT`] with no GPU picked. + Hung(String), +} + +/// Run the binary with the drivers hidden (`hide_drivers`) on `display` +/// (none: no display server at all) and wait for it to end, to pick a GPU or +/// to run out of time. Nothing of the user's state, display or session bus is +/// in its environment. +fn run_binary(name: &str, display: Option<&str>, hide_drivers: bool) -> Outcome { + let scratch = Scratch( + Path::new(env!("CARGO_TARGET_TMPDIR")).join(format!("{name}-{}", std::process::id())), + ); + let home = scratch.0.join("home"); + let runtime = scratch.0.join("runtime"); + fs::create_dir_all(&runtime).unwrap(); + fs::set_permissions(&runtime, fs::Permissions::from_mode(0o700)).unwrap(); + let log = scratch.0.join("stderr.log"); + + let mut command = Command::new(env!("CARGO_BIN_EXE_voelin-gpui")); + command + .env_remove("DISPLAY") + .env_remove("WAYLAND_DISPLAY") + .env_remove("WAYLAND_SOCKET") + .env_remove("XDG_SESSION_TYPE") + .env_remove("DBUS_SESSION_BUS_ADDRESS") + .env_remove("RUST_LOG") + .env("HOME", &home) + .env("XDG_RUNTIME_DIR", &runtime) + .env("XDG_CONFIG_HOME", home.join("config")) + .env("XDG_DATA_HOME", home.join("data")) + .env("XDG_STATE_HOME", home.join("state")) + .env("XDG_CACHE_HOME", home.join("cache")) + .env("VOELIN_DATA_DIR", home.join("voelin")) + // The log line that tells a GPU was picked. + .env("VOELIN_LOG", "warn,voelin_gpui=info,gpui_wgpu=info") + .stdin(Stdio::null()) + .stdout(Stdio::null()) + .stderr(fs::File::create(&log).unwrap()); + if let Some(display) = display { + command.env("DISPLAY", display); + } + if hide_drivers { + command + .env("VK_DRIVER_FILES", format!("{NOWHERE}/vulkan.json")) + .env("VK_ICD_FILENAMES", format!("{NOWHERE}/vulkan.json")) + .env("__EGL_VENDOR_LIBRARY_DIRS", NOWHERE) + .env("__EGL_VENDOR_LIBRARY_FILENAMES", format!("{NOWHERE}/egl.json")) + .env("LIBGL_DRIVERS_PATH", NOWHERE); + } + let mut child = command.spawn().unwrap(); + + let deadline = Instant::now() + WAIT; + loop { + if let Some(status) = child.try_wait().unwrap() { + return Outcome::Ended(status, fs::read_to_string(&log).unwrap_or_default()); + } + let seen = fs::read_to_string(&log).unwrap_or_default(); + let outcome = if seen.contains("Selected GPU adapter") { + Outcome::Opened + } else if Instant::now() > deadline { + Outcome::Hung(seen) + } else { + std::thread::sleep(Duration::from_millis(50)); + continue; + }; + let _ = child.kill(); + let _ = child.wait(); + return outcome; + } +} + +#[test] +fn a_window_that_cannot_open_for_want_of_a_driver_ends_the_binary_with_an_error() { + let Some(display) = std::env::var("VOELIN_X11_TEST_DISPLAY").ok().filter(|d| !d.is_empty()) + else { + eprintln!("skipped: VOELIN_X11_TEST_DISPLAY is not set"); + return; + }; + match run_binary("no-driver", Some(&display), true) { + Outcome::Ended(status, seen) => { + assert_eq!(status.code(), Some(1), "{seen}"); + assert!(seen.contains("cannot open the main window"), "{seen}"); + } + Outcome::Opened => eprintln!( + "skipped: this machine's driver was found in spite of the environment, \ + so the window can open" + ), + Outcome::Hung(seen) => panic!( + "still running after {WAIT:?} with no window and no GPU: a quit asked for in the \ + launch callback is lost\n{seen}" + ), + } +} + +#[test] +fn without_a_display_server_the_binary_ends_with_an_error() { + match run_binary("no-display", None, false) { + Outcome::Ended(status, seen) => { + assert_eq!(status.code(), Some(1), "{seen}"); + assert!(seen.contains("cannot open the main window"), "{seen}"); + assert!(seen.contains("no display"), "{seen}"); + } + Outcome::Opened => panic!("a GPU was picked without a display server"), + Outcome::Hung(seen) => panic!( + "still running after {WAIT:?} with no window: GPUI's headless platform keeps \ + the app alive\n{seen}" + ), + } +} diff --git a/docs/ui.md b/docs/ui.md index e19b509..b5b2c6c 100644 --- a/docs/ui.md +++ b/docs/ui.md @@ -1005,7 +1005,8 @@ crates/voelin-gpui/ and a job from another thread reach the entity; the theme reaches gpui-component, follows the window's appearance and the text size; the Slint file's tokens equal the Rust ones (below); the icons and fonts load. - `cargo test -p voelin-gpui`. + `tests/launch_failure.rs` starts the built binary where its window cannot + open (below). `cargo test -p voelin-gpui`. ### Running it @@ -1024,6 +1025,29 @@ run took is in the log ("Selected GPU adapter: ..."; the default filter shows GPUI's adapter lines: `VOELIN_LOG` or `RUST_LOG` change it). On Wayland and on X11 (a window manager is needed there for fullscreen and close requests). +A window that cannot open ends the app with the reason and exit status 1; it +does not stay up without a window. With no usable driver (none installed, or +`VK_DRIVER_FILES` pointing nowhere) the message is + +``` +Error: cannot open the main window + +Caused by: + Failed to create surface: Failed to create surface for any enabled backend: {} +``` + +and above it the log has the Vulkan loader's own lines (`vkCreateInstance: +Found no drivers!`); install a driver or lavapipe. With no display server +(neither `DISPLAY` nor `WAYLAND_DISPLAY` set, over ssh for one) it is `cannot +open the main window`, caused by `no display: neither DISPLAY nor +WAYLAND_DISPLAY names a display server`; GPUI alone would start its headless +platform there, which opens windows that draw nothing and runs until it is +stopped. `tests/launch_failure.rs` starts the binary both ways (the driver one +needs the test display of `VOELIN_X11_TEST_DISPLAY`; it hides the drivers +through `VK_DRIVER_FILES`, `__EGL_VENDOR_LIBRARY_DIRS` and +`LIBGL_DRIVERS_PATH`, and is skipped where the machine's driver is found +anyway). + | Switch | Does | |---|---| | `VOELIN_OPEN=swatch` | the swatch page in place of the shell; the Slint front end's other pages are not here yet and are named in a warning |