Skip to content

Craft Anything: understand how any software works, and craft your own.

Rust

Test both start-panic decisions, and don't blame the graphics for every early panic (#811)

smittie2000a067ac6981f8
diff --git a/apps/craft-anything/src/main.rs b/apps/craft-anything/src/main.rs
index 903e9e76..b5bca9b6 100644
--- a/apps/craft-anything/src/main.rs
+++ b/apps/craft-anything/src/main.rs
@@ -89,6 +89,8 @@ fn main() -> eframe::Result {
// starts the app that way and rejected the panic, exit 101), instead of returning an error.
let shown = Arc::new(std::sync::atomic::AtomicBool::new(false));
let shown_by_app = Arc::clone(&shown);
+ // AssertUnwindSafe: after a caught panic nothing from this closure is used again. The event
+ // loop isn't retried; the app only reports the failure (below) and exits.
let run = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
eframe::run_native(
"Craft Anything",
@@ -112,13 +114,7 @@ fn main() -> eframe::Result {
}),
)
}));
- let run = match run {
- Ok(run) => run,
- // A window that never showed: report it like any other start failure, below.
- Err(payload) if !shown.load(std::sync::atomic::Ordering::Relaxed) => Err(eframe::Error::AppCreation(panic_text(payload.as_ref()).into())),
- // A panic after the window was up is a bug, not a start failure: it ends the app as before.
- Err(payload) => std::panic::resume_unwind(payload),
- };
+ let run = start_result(run, shown.load(std::sync::atomic::Ordering::Relaxed));
match &run {
Ok(()) => log::info!("closed"),
Err(e) => {
@@ -155,12 +151,27 @@ fn wants_software(setting: Option<&str>) -> bool {
setting.is_some_and(|s| s.trim().eq_ignore_ascii_case("software"))
}
+/// The result of a run that may have panicked. A panic before the window's first frames were shown
+/// becomes an error, which the start-failure dialog then reports: on eframe 0.36 that is how wgpu
+/// reports a surface it can't configure (no usable GPU, or no desktop). It can also be any other bug
+/// in the app's first moments, so the text says "internal error" and doesn't blame the graphics. A
+/// panic after the window was up is resumed, ending the app as a panic always did.
+fn start_result(run: std::thread::Result<eframe::Result>, shown: bool) -> eframe::Result {
+ match run {
+ Ok(run) => run,
+ Err(payload) if !shown => {
+ Err(eframe::Error::AppCreation(format!("internal error before the window was shown: {}", panic_text(payload.as_ref())).into()))
+ }
+ Err(payload) => std::panic::resume_unwind(payload),
+ }
+}
+
/// The message a panic carried (`panic!` with a literal or a formatted string), or a generic one.
fn panic_text(payload: &(dyn std::any::Any + Send)) -> String {
match (payload.downcast_ref::<&str>(), payload.downcast_ref::<String>()) {
(Some(text), _) => (*text).to_string(),
(None, Some(text)) => text.clone(),
- (None, None) => "the graphics start failed".to_string(),
+ (None, None) => "an unknown panic".to_string(),
}
}
@@ -235,18 +246,6 @@ mod tests {
use super::*;
use eframe::wgpu::{Backend as B, DeviceType as T};
- /// A panic before the window showed becomes the error the start-failure dialog shows: wgpu's
- /// own message when there is one (`panic!` with a literal or a formatted string).
- #[test]
- fn a_start_panic_keeps_its_message() {
- let literal: Box<dyn std::any::Any + Send> = Box::new("wgpu error: Validation Error");
- let formatted: Box<dyn std::any::Any + Send> = Box::new(String::from("In Surface::configure: Invalid surface"));
- let other: Box<dyn std::any::Any + Send> = Box::new(7_u8);
- assert_eq!(panic_text(literal.as_ref()), "wgpu error: Validation Error");
- assert_eq!(panic_text(formatted.as_ref()), "In Surface::configure: Invalid surface");
- assert_eq!(panic_text(other.as_ref()), "the graphics start failed");
- }
-
/// #27: a VMware VM offers its virtual GPU over GL and WARP over DX12; GL drew a black window.
/// #33: a start that shows nothing is looked at through `desktop.log`; between same-class
/// devices DX12 comes before Vulkan, and GL stays the last resort.
@@ -320,6 +319,49 @@ mod graphics_tests {
use super::*;
use eframe::wgpu::Dx12Compiler;
+ /// A panic before the window showed becomes the error the start-failure dialog shows: wgpu's
+ /// own message when there is one (`panic!` with a literal or a formatted string).
+ #[test]
+ fn a_start_panic_keeps_its_message() {
+ let literal: Box<dyn std::any::Any + Send> = Box::new("wgpu error: Validation Error");
+ let formatted: Box<dyn std::any::Any + Send> = Box::new(String::from("In Surface::configure: Invalid surface"));
+ let other: Box<dyn std::any::Any + Send> = Box::new(7_u8);
+ assert_eq!(panic_text(literal.as_ref()), "wgpu error: Validation Error");
+ assert_eq!(panic_text(formatted.as_ref()), "In Surface::configure: Invalid surface");
+ assert_eq!(panic_text(other.as_ref()), "an unknown panic");
+ }
+
+ /// A run that ended normally, or with an error, comes back as it was.
+ #[test]
+ fn a_run_without_a_panic_is_passed_through() {
+ assert!(start_result(Ok(Ok(())), false).is_ok());
+ let error = start_result(Ok(Err(eframe::Error::AppCreation("no adapter".into()))), true);
+ assert!(matches!(error, Err(eframe::Error::AppCreation(e)) if e.to_string() == "no adapter"));
+ }
+
+ /// A panic before the window showed becomes the start error the dialog reports, with the
+ /// panic's message and without blaming the graphics.
+ #[test]
+ fn a_panic_before_the_window_showed_becomes_a_start_error() {
+ let run: std::thread::Result<eframe::Result> = Err(Box::new("In Surface::configure: Invalid surface"));
+ let Err(eframe::Error::AppCreation(e)) = start_result(run, false) else {
+ panic!("expected a start error");
+ };
+ assert_eq!(e.to_string(), "internal error before the window was shown: In Surface::configure: Invalid surface");
+ }
+
+ /// A panic after the window was up isn't turned into a start error: it is resumed, with its
+ /// own payload, as if nothing had caught it.
+ #[test]
+ fn a_panic_after_the_window_showed_is_resumed() {
+ let resumed = std::panic::catch_unwind(|| {
+ let run: std::thread::Result<eframe::Result> = Err(Box::new("a bug in a frame"));
+ let _ = start_result(run, true);
+ });
+ let payload = resumed.expect_err("the panic must be resumed");
+ assert_eq!(panic_text(payload.as_ref()), "a bug in a frame");
+ }
+
/// #522: wgpu's `Auto` found a `dxcompiler.dll` without `dxil.dll`, and the device was lost on
/// an RTX 5060 Ti. FXC is the default; the environment variable still decides when set.
#[test]