Files
faumarayandClaude Sonnet 5.5 a4694e944b voelin-gpui: a window that cannot open ends the app with the reason, not a hang
When open_main_window failed (no usable Vulkan or GL driver), run() stored the
error and called cx.quit() inside the launch callback. GPUI's Linux platform
calls that callback before it starts its event loop, and calloop's
EventLoop::run clears its stop signal on entry, so the quit was lost: the
binary stayed alive with no window and no message (reproduced on a private
Xvfb with VK_DRIVER_FILES, __EGL_VENDOR_LIBRARY_DIRS and LIBGL_DRIVERS_PATH
pointing nowhere: exit 124 under `timeout`, nothing in the log after "Creating
colormap"), and the `failure` that main was to print was never returned. The
quit is a foreground task now, which runs inside the loop: the same run exits
with status 1 and "cannot open the main window / Failed to create surface".

With no display server at all GPUI starts its headless platform, whose
open_window succeeds with a window that draws nothing, so the same silent hang
came from there; open_main_window refuses a compositor named "headless" with
"no display: neither DISPLAY nor WAYLAND_DISPLAY names a display server".

tests/launch_failure.rs starts the built binary both ways (the driver case
under VOELIN_X11_TEST_DISPLAY, skipped where the machine's driver is found
anyway; the display case needs none) and fails when the process outlives two
minutes; with the old cx.quit() the driver test fails after 120 s, with the fix
both pass in a third of a second. docs/ui.md ("Running it") says what a missing
driver and a missing display look like; CHANGELOG.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-27 09:41:53 +03:00

155 lines
5.4 KiB
Rust

//! 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}"
),
}
}