Skip to content

Control a real USB-connected iPhone from Linux (no Mac, no paid developer account) and expose it to AI agents as MCP tools

fix(daemon): restart a WDA that answers /status but no longer XCTest (#60)

Qinghe Li9815f1119c07
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 98f715f..5531cbb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,7 @@ All notable changes to this project are documented here. The format is based on
### Fixed
- Locking no longer takes about 10 s every time on some iPhones (e.g. iPhone 15 Pro Max, iOS 27.0.1): pressing the power key via IOHID does nothing there, so the daemon remembers it per device and uses WDA's lock first (about 2 s).
+- A WDA that still answers `/status` but no longer answers anything that goes through XCTest (foreground app, screenshots, elements, lock state all time out) is now noticed and restarted within about 30 s. Before, the daemon kept showing it as ready, and in background mode (`ICTL_WDA_DETACHED`) even a daemon restart attached to the stuck WDA again.
## [0.2.3] - 2026-11-13
diff --git a/CHANGELOG.zh-CN.md b/CHANGELOG.zh-CN.md
index 8802ed0..4f91af7 100644
--- a/CHANGELOG.zh-CN.md
+++ b/CHANGELOG.zh-CN.md
@@ -9,6 +9,7 @@
### 修复
- 部分 iPhone(如 iPhone 15 Pro Max,iOS 27.0.1)上锁屏不再每次要 10 秒左右:经 IOHID 短按电源键在这些设备上不起作用,守护进程会按设备记住,之后先用 WDA 的锁屏(约 2 秒)。
+- WDA 还应答 `/status`、经 XCTest 的请求却全都不应答(前台 App、截图、读元素、锁屏状态全部超时)时,现在约 30 秒内就会发现并重启它。以前守护进程一直显示「就绪」,后台模式(`ICTL_WDA_DETACHED`)下连重启守护进程都会把卡死的 WDA 接回来。
## [0.2.3] - 2026-11-13
diff --git a/crates/ictl-daemon/src/core.rs b/crates/ictl-daemon/src/core.rs
index 906ec97..4a1f34e 100644
--- a/crates/ictl-daemon/src/core.rs
+++ b/crates/ictl-daemon/src/core.rs
@@ -307,6 +307,8 @@ pub(crate) struct Inner {
pub starting: Option<JoinHandle<StartOutcome>>,
pub resigning: bool,
pub wda_misses: Misses,
+ /// 连续几轮 `/status` 正常、经 XCTest 的探测却没有应答(见 `WdaClient::xctest_responsive`)
+ pub wda_stalls: Misses,
pub backoff: Backoff,
/// 连续列不出设备的次数(usbmuxd 抖一下不立刻当成离线)
pub list_errors: u32,
@@ -422,6 +424,7 @@ impl Daemon {
starting: None,
resigning: false,
wda_misses: Misses::default(),
+ wda_stalls: Misses::default(),
backoff: Backoff::default(),
list_errors: 0,
offline_since: Some(now),
@@ -573,6 +576,7 @@ impl Daemon {
g.swiped_tree = None;
drop_tunnel(g).await;
g.wda_misses.reset();
+ g.wda_stalls.reset();
}
pub(crate) async fn ensure_tunnel<'a>(
diff --git a/crates/ictl-daemon/src/machine.rs b/crates/ictl-daemon/src/machine.rs
index 8f11aaa..3a573dd 100644
--- a/crates/ictl-daemon/src/machine.rs
+++ b/crates/ictl-daemon/src/machine.rs
@@ -115,6 +115,11 @@ impl Misses {
pub fn reset(&mut self) {
self.0 = 0;
}
+
+ /// 有没应答的、但还没到 [`Self::LIMIT`]
+ pub fn pending(&self) -> bool {
+ self.0 > 0
+ }
}
#[cfg(test)]
@@ -181,9 +186,12 @@ mod tests {
#[test]
fn misses_need_three_in_a_row() {
let mut m = Misses::default();
+ assert!(!m.pending());
assert!(!m.hit());
+ assert!(m.pending());
assert!(!m.hit());
m.reset();
+ assert!(!m.pending());
assert!(!m.hit());
assert!(!m.hit());
assert!(m.hit());
diff --git a/crates/ictl-daemon/src/supervise.rs b/crates/ictl-daemon/src/supervise.rs
index b81ff90..b9096ce 100644
--- a/crates/ictl-daemon/src/supervise.rs
+++ b/crates/ictl-daemon/src/supervise.rs
@@ -7,7 +7,7 @@ use ictl_device::{Device, DeviceError, LogStream, Rotation, ScreenSize, Tunnel,
use ictl_wda::{WdaClient, WdaError, WdaRunner, http::DeviceTransport};
use crate::core::{Daemon, DetachedWda, DeviceProfile, Inner, close_hid};
-use crate::machine::DeviceState;
+use crate::machine::{DeviceState, Misses};
use crate::notify::Event;
/// Three 10-second supervisor intervals. A missing background sample within this
@@ -305,7 +305,25 @@ impl Daemon {
(Some(w), true) => w.ready().await.unwrap_or(false),
_ => false,
};
- if ready {
+ // /status 不经 XCTest:WDA 半死时它照样 ready。再问一次经 XCTest 的,连续几轮没应答就当它卡死、重新启动
+ let stalled = ready && self.xctest_stalled(g).await;
+ if stalled {
+ self.log.log(&ictl_l10n::log_trf!(
+ "WDA 卡死:/status 正常,但经 XCTest 的请求连续 {} 轮没有应答,重新启动 WDA",
+ "WDA is stuck: /status answers, but requests that go through XCTest went unanswered {} checks in a row; restarting WDA",
+ Misses::LIMIT
+ ));
+ // 后台启动的那个不能再接上:忘掉它,下面重新后台启动时会先结束它
+ if g.persisted.detached_wda.take().is_some() {
+ g.persisted.save(&self.cfg.state_file());
+ }
+ self.update_display(g, None).await;
+ self.drop_session(g).await;
+ } else if ready && g.wda_stalls.pending() {
+ // 这一轮没应答、还没到判定卡死:先不采样屏幕(同样经 XCTest,每次还要白等 10 秒)
+ return;
+ }
+ if ready && !stalled {
g.wda_misses.reset();
self.sample_display(g, false).await;
if g.state != DeviceState::Ready {
@@ -318,7 +336,7 @@ impl Daemon {
}
return;
}
- if !self.confirm_wda_loss(g, runner_alive).await {
+ if !stalled && !self.confirm_wda_loss(g, runner_alive).await {
return;
}
if g.runner.is_some() {
@@ -334,6 +352,28 @@ impl Daemon {
self.spawn_start(g, device, bundle);
}
+ /// 经 XCTest 的探测([`WdaClient::xctest_responsive`])连续 [`Misses::LIMIT`] 轮没有应答时返回 true。
+ /// WDA 回了错误、或者应答了,都算活着
+ async fn xctest_stalled(&self, g: &mut Inner) -> bool {
+ let Some(w) = g.wda.as_ref() else { return false };
+ match w.xctest_responsive().await {
+ Err(e) if e.no_answer() => {
+ tracing::debug!(
+ "{}",
+ ictl_l10n::log_trf!(
+ "WDA 经 XCTest 的探测没有应答:{e}",
+ "WDA did not answer a probe that goes through XCTest: {e}"
+ )
+ );
+ g.wda_stalls.hit()
+ }
+ _ => {
+ g.wda_stalls.reset();
+ false
+ }
+ }
+ }
+
/// Keep an active view and finger through the same transient status misses as the runner.
pub(crate) async fn confirm_wda_loss(&self, g: &mut Inner, runner_alive: bool) -> bool {
if runner_alive && !g.wda_misses.hit() {
diff --git a/crates/ictl-wda/src/client.rs b/crates/ictl-wda/src/client.rs
index fa14729..6e2b7fb 100644
--- a/crates/ictl-wda/src/client.rs
+++ b/crates/ictl-wda/src/client.rs
@@ -10,6 +10,8 @@ use crate::http::Transport;
use crate::{Result, WdaError, elements};
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);
+/// [`WdaClient::xctest_responsive`] 的时限。正常几十到几百毫秒;守护进程每轮巡检问一次,等太久会拖住巡检
+pub const XCTEST_PROBE_TIMEOUT: Duration = Duration::from_secs(5);
/// How long a measured screen size is trusted at the start of a gesture. `/wda/screen` takes about 100 ms (measured on
/// iOS 27) and only Display Zoom changes the size, so a touch-down doesn't pay for it every time
const SCREEN_RECHECK: Duration = Duration::from_secs(60);
@@ -244,6 +246,13 @@ impl<T: Transport> WdaClient<T> {
Ok(self.call("GET", "/status", None, Duration::from_secs(5)).await?["value"]["ready"] == true)
}
+ /// WDA 还能不能经 XCTest 办事:问一次前台 App,限时 [`XCTEST_PROBE_TIMEOUT`]。
+ /// `/status` 不经 XCTest,WDA 半死(iPad 真机:`/status` 照常 200,前台 App、截图、读元素、`/wda/locked` 全部超时)时
+ /// 它照样报 ready;这一问才看得出来
+ pub async fn xctest_responsive(&self) -> Result<()> {
+ self.call("GET", "/wda/activeAppInfo", None, XCTEST_PROBE_TIMEOUT).await.map(drop)
+ }
+
/// Read the WDA screen size again, including changes to Display Zoom.
pub async fn measure_screen(&mut self) -> Result<ScreenSize> {
let v = self.get("/wda/screen").await?;
@@ -973,6 +982,30 @@ mod tests {
assert!(matches!(WdaClient::new(&m).active_app().await, Err(WdaError::Interrupted(_))));
}
+ /// 经 XCTest 的探测:只有「送到了却没有应答」算卡死的迹象;WDA 回了错误说明它还在办事,连不上归 `/status` 管
+ #[tokio::test]
+ async fn xctest_probe_counts_only_missing_answers() {
+ struct Fails(std::io::ErrorKind);
+ impl Transport for Fails {
+ async fn send(&self, _: &str, _: &str, _: Option<Vec<u8>>, t: Duration) -> std::io::Result<(u16, Vec<u8>)> {
+ assert_eq!(t, XCTEST_PROBE_TIMEOUT);
+ Err(std::io::Error::new(self.0, "x"))
+ }
+ }
+ let probe = |k| async move { WdaClient::new(Fails(k)).xctest_responsive().await };
+ assert!(probe(std::io::ErrorKind::TimedOut).await.unwrap_err().no_answer());
+ assert!(probe(crate::http::AFTER_SENT).await.unwrap_err().no_answer());
+ assert!(!probe(std::io::ErrorKind::NotConnected).await.unwrap_err().no_answer());
+ let m = Mock::with(&[
+ (200, r#"{"value":{"bundleId":"com.apple.springboard","pid":60}}"#),
+ (500, r#"{"value":{"error":"unknown error","message":"busy"}}"#),
+ ]);
+ let c = WdaClient::new(&m);
+ assert!(c.xctest_responsive().await.is_ok());
+ assert!(!c.xctest_responsive().await.unwrap_err().no_answer());
+ assert_eq!(m.seen.lock().unwrap()[0].1, "/wda/activeAppInfo");
+ }
+
#[tokio::test]
async fn screen_size_is_cached_and_status_ready() {
let unknown = r#"{"value":{"error":"unknown command","message":"no"}}"#;
diff --git a/crates/ictl-wda/src/lib.rs b/crates/ictl-wda/src/lib.rs
index cb5ecfa..eec6f89 100644
--- a/crates/ictl-wda/src/lib.rs
+++ b/crates/ictl-wda/src/lib.rs
@@ -36,6 +36,13 @@ pub enum WdaError {
pub type Result<T> = std::result::Result<T, WdaError>;
+impl WdaError {
+ /// 请求送到了 WDA,却没有完整的应答(超时,或应答到一半断了)。WDA 回了一个错误不算:它还在办事
+ pub fn no_answer(&self) -> bool {
+ matches!(self, Self::Timeout(_) | Self::Interrupted(_))
+ }
+}
+
/// 识别 XCTest 启动失败是否是「等待自动化密码」:密码框 60 秒没输(toolkit 和 iOS 27 真机同一文字),
/// 或手机锁屏时密码框弹不出来、被系统直接取消(LocalAuthentication 错误,如 Code=-4「认证已取消」)
pub fn is_automation_pin_error(message: &str) -> bool {
diff --git a/docs/dev/regression-cases.md b/docs/dev/regression-cases.md
index ce8df33..eb2caf8 100644
--- a/docs/dev/regression-cases.md
+++ b/docs/dev/regression-cases.md
@@ -106,3 +106,4 @@
| 100 | 0.2.2 起控制台每次按下都先请求 `/wda/screen`(约 0.1~0.2 秒),HID 会话空闲 30 秒关掉后,鼠标一直停在画面里时第一下还要等约 0.5 秒重开 | 按下只问方向,尺寸在转屏时和距上次测量满 60 秒后才重量;鼠标在画面上移动时每 20 秒最多预热一次 HID 会话 | 单测 display_poll_remeasures_size_only_when_needed;页面测试 hovering keeps the HID session warm at most every 20 seconds;真机:控制台连按,按下往返在几十毫秒内 |
| 101 | 控制台开着、页面走 WDA 截图的画面(浏览器解不了 HEVC,或低于 iOS 27)时,WDA 每 1.5~2.5 分钟被 iOS 杀掉(`cpu_resource_fatal`:60 秒内平均 86%~93% CPU,上限 80%)。MJPEG 缩到 75% 让 WDA 逐帧解码、缩放(CoreGraphics)、重新编码;横屏时 `mjpegFixOrientation` 让它逐帧旋转、重新编码,同样超限 | MJPEG 原尺寸、不转正(`MJPEG_SCALE` = 100,`mjpegFixOrientation` = false):WDA 直接转发 testmanagerd 硬件编好的 JPEG,帧率不变(约 34 fps)。横屏的竖帧由页面转正(H.264 按 `display.rotation`,MJPEG 的 img 由浏览器按 EXIF) | 真机(iPhone 15 Pro Max,iOS 27.0.1):拉本机编码的 `/video`,竖屏 25 分钟、横屏 5 分钟不再被杀(开着转正时横屏 2 分钟被杀);无头 Chrome:横屏下 H.264 和 MJPEG 画面方向正确、坐标换算正确 |
| 102 | iPhone 15 Pro Max(iOS 27.0.1)每次锁屏约 10 秒:经 IOHID 短按电源键(0.05~0.1 秒)调用卡约 5.3 秒且不锁,再等 3 秒才退回 `/wda/lock`(约 1.9 秒)。iPhone 18 Pro Max(iOS 27.2.1)正相反 | 按设备记住哪种锁屏有效(`PerDevice::lock_via_wda`),之后先用它;先用的不灵仍会试另一种 | 单测 lock_order_follows_what_worked_on_this_device;真机:同一台连续锁屏,第一次约 10 秒、之后约 2 秒 |
+| 103 | WDA 半死:`/status` 照常 200,前台 App、截图、读元素、`/wda/locked` 全部超时(iPad mini 5,iPadOS 26.7.3,后台启动的 WDA 跑了几个小时后)。守护进程只看 `/status`,一直显示「就绪」,不重启;后台模式下守护进程重启还会把它接回来 | 巡检时 `/status` 正常再问一次经 XCTest 的 `/wda/activeAppInfo`(限时 5 秒),连续 3 轮没有应答(超时或应答到一半断开)就判定卡死:忘掉记下的后台 WDA,丢掉会话重新启动。WDA 回了错误算活着;守护进程自己的请求占着设备时巡检跳过,长手势不会被误判 | 单测 xctest_probe_counts_only_missing_answers、misses_need_three_in_a_row;真机:绕过守护进程直接让 WDA 长按 90 秒(期间 `/status` 5 毫秒 200,`/wda/activeAppInfo` 超时),0.2.3 一直「就绪」;修复后后台模式和普通模式都在约 28 秒判定卡死、约 34 秒恢复;经守护进程连续长按 6×10 秒不误判 |