ohos: fail fast when the Slint thread panics

Catch unwinding panics on the Slint thread, log the payload, and abort instead of leaving a frozen window backed by a dead event loop. Slint's thread-local platform cannot be safely reinitialized.
This commit is contained in:
springmin committed 2026-10-26 20:35:06 +08:00
1 parent 3435fa0237
commit 43a032aa02
1 file changed
+30 -13
+30 -13
View File
@@ -55,19 +55,36 @@ pub fn start(
.name("slint-ohos".into())
.stack_size(8 * 1024 * 1024)
.spawn(move || {
log::info("slint thread started");
let platform = platform::OhosPlatform::new(event_tx, event_rx, gate, quit, visible);
if let Err(err) = slint::platform::set_platform(Box::new(platform)) {
log::error(&format!("set_platform failed: {err}"));
return;
}
log::info("slint platform installed; building UI");
let han_families = fonts::register_system_fonts();
log::info(&format!(
"system fonts registered; Han fallback families: {han_families}"
));
if let Err(err) = app_factory() {
log::error(&format!("application terminated: {err}"));
// A panic in the Slint thread used to leave the last frame frozen
// and the event loop dead. Slint's thread-local platform cannot be
// safely reinitialized after a panic, so fail fast with a clear
// log instead of leaving an apparently alive but unusable window.
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
log::info("slint thread started");
let platform = platform::OhosPlatform::new(event_tx, event_rx, gate, quit, visible);
if let Err(err) = slint::platform::set_platform(Box::new(platform)) {
log::error(&format!("set_platform failed: {err}"));
return;
}
log::info("slint platform installed; building UI");
let han_families = fonts::register_system_fonts();
log::info(&format!(
"system fonts registered; Han fallback families: {han_families}"
));
if let Err(err) = app_factory() {
log::error(&format!("application terminated: {err}"));
}
}));
if let Err(payload) = result {
let message = payload
.downcast_ref::<&str>()
.map(|message| (*message).to_owned())
.or_else(|| payload.downcast_ref::<String>().cloned())
.unwrap_or_else(|| "non-string panic payload".to_owned());
log::error(&format!(
"slint thread panicked: {message}; aborting to avoid a frozen window"
));
std::process::abort();
}
})?;
Ok(())