From d21e37244d06054acd106a97c69244c0eb4885fa Mon Sep 17 00:00:00 2001 From: mmmharius Date: Mon, 23 Nov 2026 15:25:33 +0000 Subject: [PATCH 1/2] Report: a crash reported while the report is being written is kept (#106) write_final returned when WRITE_LOCK was taken before it stored the crash, so a crash reported by the exception filter or a removed device while another thread wrote the report (WM_CLOSE, XCL_RUN_SECONDS, DLL_PROCESS_DETACH) never reached report.json. The crash is now stored first, and a crashing thread waits up to 2 s for the lock to write it itself; a wait never goes on forever, so a crash inside build() on the writing thread still ends. Calls without a crash still give way at once. Unit test: another thread holds the lock while a crash is reported. --- crates/xcl/src/report.rs | 44 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/crates/xcl/src/report.rs b/crates/xcl/src/report.rs index 49620b2..7168bd6 100644 --- a/crates/xcl/src/report.rs +++ b/crates/xcl/src/report.rs @@ -4,7 +4,8 @@ use std::collections::BTreeMap; use std::sync::atomic::Ordering; -use std::sync::Mutex; +use std::sync::{Mutex, MutexGuard, TryLockError}; +use std::time::{Duration, Instant}; use serde::{Deserialize, Serialize}; @@ -143,16 +144,32 @@ pub fn build() -> Report { } } +/// `m.try_lock()`, retried for `wait`: never an endless wait – the holder may be this very +/// thread, crashed inside `build()`. +fn lock_within(m: &Mutex, wait: Duration) -> Option> { + let end = Instant::now() + wait; + loop { + match m.try_lock() { + Ok(g) => return Some(g), + Err(TryLockError::WouldBlock) if Instant::now() < end => std::thread::sleep(Duration::from_millis(10)), + Err(_) => return None, + } + } +} + /// Write `report.json` (host side). `crash` is remembered for later writes. pub fn write_final(crash: Option) { // the process ends: a cursor held in the XCL window is let go crate::hle::kbm::release(); - let Ok(_g) = WRITE_LOCK.try_lock() else { return }; + // a crash is kept even while another thread writes the report, and is written once that + // thread is done – the process ends right after (#106); other calls give way at once + let wait = if crash.is_some() { Duration::from_secs(2) } else { Duration::ZERO }; if let Some(c) = crash { - if let Ok(mut g) = CRASH.lock() { + if let Some(mut g) = lock_within(&CRASH, wait) { g.get_or_insert(c); } } + let Some(_g) = lock_within(&WRITE_LOCK, wait) else { return }; let Some(boot) = crate::state::boot() else { return }; let r = build(); if let Ok(s) = serde_json::to_string_pretty(&r) { @@ -163,3 +180,24 @@ pub fn write_final(crash: Option) { pub fn load(path: &std::path::Path) -> Option { serde_json::from_str(&std::fs::read_to_string(path).ok()?).ok() } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_crash_is_kept_while_another_thread_writes_the_report() { + let (held, wait_held) = std::sync::mpsc::channel(); + let writer = std::thread::spawn(move || { + let _g = WRITE_LOCK.lock().unwrap(); + held.send(()).unwrap(); + std::thread::sleep(Duration::from_millis(100)); + }); + wait_held.recv().unwrap(); + // no boot record in unit tests: nothing is written to disk + write_final(Some("boom".into())); + writer.join().unwrap(); + assert_eq!(CRASH.lock().unwrap().as_deref(), Some("boom")); + *CRASH.lock().unwrap() = None; + } +} From 6cb71db0166964726bc073dab09b48d2bd71424d Mon Sep 17 00:00:00 2001 From: mmmharius Date: Mon, 23 Nov 2026 15:25:33 +0000 Subject: [PATCH 2/2] Config: title_memory is read in any case (#106) resolve compared title_memory with "auto" and "advanced" as written, while the manifest's value is lowercased and device_type is compared in any case: title_memory = "Auto" ignored the manifest, "Advanced" got 2048 MiB of unified memory instead of 4095. The value is trimmed and lowercased once. Unit test: "Auto" with an Advanced manifest and "ADVANCED" without one. --- crates/xcl/src/config.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/crates/xcl/src/config.rs b/crates/xcl/src/config.rs index 0f5f4e8..a49fa9e 100644 --- a/crates/xcl/src/config.rs +++ b/crates/xcl/src/config.rs @@ -76,6 +76,8 @@ impl Config { pub fn resolve(&mut self, manifest: Option<&crate::manifest::Manifest>) -> Vec { let mut from = Vec::new(); let series_s = self.is_series_s(); + // "Auto" / "Advanced" in xcl.toml: read in any case, as device_type and the manifest (#106) + self.title_memory = self.title_memory.trim().to_ascii_lowercase(); if self.title_memory == "auto" { let m = manifest.and_then(|m| if series_s { m.lockhart_memory.clone() } else { m.anaconda_memory.clone() }); if let Some(m) = &m { @@ -338,6 +340,16 @@ mod tests { assert_eq!((c.title_memory.as_str(), c.title_memory_mb), ("standard", 13_824)); } + #[test] + fn title_memory_is_read_in_any_case() { + let manifest = crate::manifest::Manifest { anaconda_memory: Some("Advanced".into()), ..Default::default() }; + for (setting, m) in [("Auto", Some(&manifest)), ("ADVANCED", None)] { + let mut c = Config { title_memory: setting.into(), ..Config::default() }; + c.resolve(m); + assert_eq!((c.title_memory.as_str(), c.uma_mb), ("advanced", 4095), "{setting}"); + } + } + #[test] fn raytracing_tier_parsing() { assert_eq!(parse_raytracing_tier("auto"), None);