From e0333d47ac80eff2b5645827026eea09b89afa95 Mon Sep 17 00:00:00 2001 From: abdelrahman fahmy <104788132+Apdelrahman1911@users.noreply.github.com> Date: Sun, 6 Dec 2026 09:00:49 +0000 Subject: [PATCH] fix(appstate): pass explicit 0.9s simax timeout on info query Previously ui_state() sent {"op": "info"} without a "timeout" field while asking with a 1200ms host socket timeout. Because simax defaults "timeout" to 5.0s (simax.m:2413), any hung main thread timed out on the host socket at 1.2s before simax could return {"error": "busy: ..."}, causing appstate.rs to misclassify main-thread hangs as "suspended". Pass "timeout": 0.9 (strictly below 1200ms) so simax replies "busy" in time and app-state accurately distinguishes busy main threads from RunningBoard suspension. Add unit test covering info_query() and classify_info_reply(). --- tools/launcher/core/src/appstate.rs | 57 ++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 10 deletions(-) diff --git a/tools/launcher/core/src/appstate.rs b/tools/launcher/core/src/appstate.rs index 3836ed63..d17121e2 100644 --- a/tools/launcher/core/src/appstate.rs +++ b/tools/launcher/core/src/appstate.rs @@ -174,16 +174,17 @@ fn app_pids(app: &Value, pids: &[u32]) -> Vec { .collect() } -/// The app's state from its own simax reader (foreground / background), or what its process says (suspended) -fn ui_state(inst: &Instance, pid: u32, host_pid: u32, procs: &HashMap) -> String { - if procs.get(&host_pid).is_some_and(|r| r.state == 'T' || r.state == 't') { - return "suspended".into(); - } - let has_reader = crate::ax::endpoints(inst).iter().any(|e| crate::ax::pid_of_sock(&e.sock) == pid); - if !has_reader { - return "running".into(); - } - match crate::ax::ask_pid(inst, pid, &json!({"op": "info"}), Duration::from_millis(1200)) { +const INFO_QUERY_TIMEOUT_MS: u64 = 1200; +const INFO_SIMAX_TIMEOUT_SECS: f64 = 0.9; + +fn info_query() -> Value { + // Keep simax's main-thread wait strictly below INFO_QUERY_TIMEOUT_MS so a hung main thread + // returns {"error": "busy: ..."} instead of timing out the socket and being misclassified as "suspended". + json!({"op": "info", "timeout": INFO_SIMAX_TIMEOUT_SECS}) +} + +fn classify_info_reply(reply: Result) -> String { + match reply { Ok(v) if v["error"].as_str().is_some_and(|e| e.starts_with("busy")) => "busy (main thread not answering)".into(), Ok(v) => match v["state"].as_str().unwrap_or("") { "active" => "foreground".into(), @@ -196,6 +197,23 @@ fn ui_state(inst: &Instance, pid: u32, host_pid: u32, procs: &HashMap) } } +/// The app's state from its own simax reader (foreground / background), or what its process says (suspended) +fn ui_state(inst: &Instance, pid: u32, host_pid: u32, procs: &HashMap) -> String { + if procs.get(&host_pid).is_some_and(|r| r.state == 'T' || r.state == 't') { + return "suspended".into(); + } + let has_reader = crate::ax::endpoints(inst).iter().any(|e| crate::ax::pid_of_sock(&e.sock) == pid); + if !has_reader { + return "running".into(); + } + classify_info_reply(crate::ax::ask_pid( + inst, + pid, + &info_query(), + Duration::from_millis(INFO_QUERY_TIMEOUT_MS), + )) +} + /// The state dir and Darling prefix this boot of the phone uses fn dirs(inst: &Instance) -> (PathBuf, PathBuf) { match crate::instance::Monitor::default().poll(std::slice::from_ref(inst)).into_iter().next() { @@ -516,4 +534,23 @@ mod tests { Some("translator: dynarmic assertion failed: !conf.shared_translations: dynarmic: shared code cache full (67108864 bytes)") ); } + + #[test] + fn info_query_distinguishes_busy_from_suspended() { + let q = info_query(); + assert_eq!(q["op"].as_str(), Some("info")); + let simax_timeout = q["timeout"].as_f64().expect("timeout must be present"); + assert!( + simax_timeout > 0.05 && (simax_timeout * 1000.0) < INFO_QUERY_TIMEOUT_MS as f64, + "simax timeout {simax_timeout}s must be strictly below socket timeout {INFO_QUERY_TIMEOUT_MS}ms" + ); + assert_eq!( + classify_info_reply(Ok(json!({"error": "busy: the main thread did not answer in time"}))), + "busy (main thread not answering)" + ); + assert_eq!(classify_info_reply(Ok(json!({"state": "active"}))), "foreground"); + assert_eq!(classify_info_reply(Ok(json!({"state": "inactive"}))), "foreground (inactive)"); + assert_eq!(classify_info_reply(Ok(json!({"state": "background"}))), "background"); + assert_eq!(classify_info_reply(Err(anyhow::anyhow!("timeout"))), "suspended"); + } }