Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Startup: preserve the GPU backend after windowing errors (#1228)

lionel69c01c1bff37
diff --git a/apps/photocraft/src/gpu_startup.rs b/apps/photocraft/src/gpu_startup.rs
index a04cce797..3b8598436 100644
--- a/apps/photocraft/src/gpu_startup.rs
+++ b/apps/photocraft/src/gpu_startup.rs
@@ -341,6 +341,12 @@ pub struct Sentinel {
/// The sentinel, shared with eframe's adapter hook and the app's started hook.
pub type SharedSentinel = Arc<Mutex<Option<Sentinel>>>;
+/// Only renderer errors should affect the next launch's graphics backend. Windowing errors
+/// (such as a missing Linux display) and app setup errors never indicate a driver failure.
+pub fn keep_marker_after_run(result: &eframe::Result) -> bool {
+ matches!(result, Err(eframe::Error::Wgpu(_)))
+}
+
impl Sentinel {
/// Open (creating) the marker in `dir` and lock it. Returns what a previous start left and,
/// unless another instance holds the lock or the directory isn't writable, this start's
@@ -599,6 +605,33 @@ mod tests {
let _ = std::fs::remove_dir_all(&dir);
}
+ #[test]
+ fn returned_non_graphics_errors_do_not_trigger_backend_recovery() {
+ let cases = [
+ ("success", Ok(()), false),
+ ("app-error", Err(eframe::Error::AppCreation(Box::new(std::io::Error::other("app setup failed")))), false),
+ ("gpu-error", Err(eframe::Error::Wgpu(egui_wgpu::WgpuError::CustomNativeAdapterSelectionError("no adapter".into()))), true),
+ ];
+ for (name, result, keep) in cases {
+ let dir = temp_dir(name);
+ let (_, s) = Sentinel::begin(&dir);
+ let mut s = s.expect("sentinel");
+ s.write(Marker { backend: "vulkan".into(), ..Default::default() }).unwrap();
+ if keep_marker_after_run(&result) {
+ drop(s);
+ } else {
+ s.finish();
+ }
+ let (previous, s) = Sentinel::begin(&dir);
+ assert_eq!(previous.crashed().is_some(), keep, "{name}: {result:?}");
+ if !keep {
+ assert_eq!(plan(Vulkan, previous.crashed(), None, false, Os::Other).backend, Vulkan);
+ }
+ s.expect("sentinel").finish();
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+ }
+
#[test]
fn unwritable_config_dir_is_not_fatal() {
let dir = temp_dir("file");
diff --git a/apps/photocraft/src/main.rs b/apps/photocraft/src/main.rs
index acdb76ac8..08f52b08a 100644
--- a/apps/photocraft/src/main.rs
+++ b/apps/photocraft/src/main.rs
@@ -390,12 +390,22 @@ fn main() -> eframe::Result {
Ok(Box::new(app))
}),
);
- // Closed before the first frames rendered: not a driver crash. (A start that failed to
- // create its device keeps the marker, so the next one tries a safer backend.)
- if result.is_ok()
- && let Some(s) = sentinel.lock().unwrap_or_else(std::sync::PoisonError::into_inner).take()
+ // Closed or failed outside graphics initialization: not a driver crash. A renderer
+ // error keeps the marker, so the next start tries a safer backend.
+ if !gpu_startup::keep_marker_after_run(&result)
+ && let Some(mut s) = sentinel.lock().unwrap_or_else(std::sync::PoisonError::into_inner).take()
{
- s.finish();
+ if result.is_err()
+ && let Some(marker) = previous.crashed()
+ {
+ // This failure supplies no new graphics-crash evidence: retain the previous
+ // marker, rather than recording this attempt's fallback backend.
+ if let Err(error) = s.write(marker.clone()) {
+ log::warn!("couldn't restore previous GPU startup marker: {error}");
+ }
+ } else {
+ s.finish();
+ }
}
// Retry in a fresh process: winit event loops cannot be recreated reliably in-process.
// Only renderer initialization failures qualify; never restart after editing has begun.
diff --git a/apps/photocraft/tests/startup_without_display.rs b/apps/photocraft/tests/startup_without_display.rs
new file mode 100644
index 000000000..5ffb5783a
--- /dev/null
+++ b/apps/photocraft/tests/startup_without_display.rs
@@ -0,0 +1,47 @@
+//! A failed Linux desktop launch must not poison the next launch's graphics policy.
+#![cfg(target_os = "linux")]
+
+#[test]
+fn missing_display_does_not_leave_a_gpu_crash_marker() {
+ let dir = std::env::temp_dir().join(format!("photocraft-no-display-{}", std::process::id()));
+ std::fs::create_dir_all(&dir).unwrap();
+ let prefs = br#"{"performance":{"gpuBackend":"vulkan","useGpu":true}}"#;
+ std::fs::write(dir.join("preferences.json"), prefs).unwrap();
+
+ let error = launch_without_display(&dir);
+ assert!(!dir.join("gpu-starting.json").exists(), "a display error is not a GPU crash: {error}");
+ assert_eq!(std::fs::read(dir.join("preferences.json")).unwrap(), prefs);
+ std::fs::remove_dir_all(&dir).unwrap();
+}
+
+#[test]
+fn missing_display_preserves_existing_gpu_crash_evidence() {
+ let dir = std::env::temp_dir().join(format!("photocraft-no-display-prior-crash-{}", std::process::id()));
+ std::fs::create_dir_all(&dir).unwrap();
+ let marker = serde_json::json!({
+ "backend": "vulkan", "adapter": "Test GPU", "adapterBackend": "vulkan",
+ "driver": "test", "version": "test"
+ });
+ std::fs::write(dir.join("gpu-starting.json"), serde_json::to_vec(&marker).unwrap()).unwrap();
+ launch_without_display(&dir);
+ let restored: serde_json::Value = serde_json::from_slice(&std::fs::read(dir.join("gpu-starting.json")).unwrap()).unwrap();
+ assert_eq!(restored, marker, "a windowing error must not erase or advance prior GPU crash evidence");
+ std::fs::remove_dir_all(&dir).unwrap();
+}
+
+fn launch_without_display(dir: &std::path::Path) -> String {
+ let output = std::process::Command::new(env!("CARGO_BIN_EXE_photocraft"))
+ .env("PHOTOCRAFT_CONFIG_DIR", dir)
+ .env_remove("DISPLAY")
+ .env_remove("WAYLAND_DISPLAY")
+ .env_remove("WAYLAND_SOCKET")
+ .env_remove("WGPU_BACKEND")
+ .env_remove("PHOTOCRAFT_GPU_STARTUP_FAILURE")
+ .env_remove("PHOTOCRAFT_CONTROL_PORT")
+ .output()
+ .unwrap();
+ let error = String::from_utf8_lossy(&output.stderr);
+ assert!(!output.status.success(), "a desktop cannot open without a display");
+ assert!(error.contains("WAYLAND_DISPLAY") && error.contains("DISPLAY"), "expected a display error, got: {error}");
+ error.into_owned()
+}
diff --git a/docs/development.md b/docs/development.md
index d95593e45..c17153037 100644
--- a/docs/development.md
+++ b/docs/development.md
@@ -44,6 +44,11 @@ CRAFT_FONTS_DIR="$PWD/../craft-fonts" cargo test --workspace # runs the Japa
On DX12 the shader compiler is FXC (`d3dcompiler_47.dll`, part of Windows), or a `dxcompiler.dll` placed beside `photocraft.exe`, loaded by its full path. wgpu's default looks `dxcompiler.dll` up by name, which reaches the current directory and `PATH` and loaded other programs' incompatible builds (#712).
+Handled windowing and app initialization errors clear this launch's startup marker, restoring any
+prior crash evidence. For example, a Linux launch without `DISPLAY` or `WAYLAND_DISPLAY` does not
+change the next launch's graphics backend. Renderer initialization errors and driver crashes still
+keep the marker for recovery.
+
If the device is lost while running (#243), every GPU entry point checks the device's health flag first, the canvas switches to the CPU compositor for the rest of the session and a notice says "GPU device was lost; using the CPU renderer." `ui.gpu.simulateLoss` triggers this path from the control channel.
## Environment variables