Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ What changed in each release, one line per change. Versions follow [Semantic Ver

### Fixes

- iOS 27+: an action that comes while the control page checks the phone in the background no longer leaves the accessibility connection half-read, and those checks no longer restart the phone's accessibility daemon; only actions decide that (#197).
- A task paused for a long time no longer keeps the control page's background checks from running (#197).
- Switching back to a phone made ready in the background takes effect at once even when the daemon was busy at that moment; it could wait 10 seconds. The daemon serves at most 256 clients at once; more wait their turn (#197).
- AI tasks limited to some apps: an action is checked again once it has the phone, in case another app came to the front meanwhile, and `iphone_web_eval` is checked against the app that shows the page (#197).
- `iphone_tap_element`'s scroll search and `iphone_wait_for` stop as soon as the task is paused or taken over or the caller goes away, instead of holding the phone until they time out; the daemon also limits a scroll search to 15 swipes (#197).
- iOS 17–18: the developer disk image is mounted the way these versions need, so WebDriverAgent starts on them again (#186).
- Times in `status`, `doctor` and the event log are shown in local time with the time zone; they were UTC without saying so (#184).
- Errors from `iphone-control update`, `service` and the update panel on the control page follow the chosen language; they were always in English (#184).
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

### 修复

- iOS 27 起:控制台在后台检查手机时来了操作,检查中途让路后无障碍连接不再停在半条消息上;这类检查也不再重启手机的无障碍服务,要不要重启只由操作决定(#197)。
- 任务暂停很久时,控制台的后台检查不再一直跑不了(#197)。
- 切回一台在后台准备好的手机时,即使守护进程当时正忙也马上生效,之前可能要等 10 秒;守护进程同时最多服务 256 个客户端,更多的排队等候(#197)。
- 限定了 App 范围的 AI 任务:操作拿到手机后再查一次前台 App,以防期间换了别的 App;`iphone_web_eval` 按显示页面的 App 检查(#197)。
- `iphone_tap_element` 的边滑边找和 `iphone_wait_for` 在任务暂停、被接管或调用方离开时立刻停下,不再占着手机等到超时;守护进程也把边滑边找限制在 15 次以内(#197)。
- iOS 17–18:开发者镜像按这些版本需要的方式挂载,WebDriverAgent 又能在上面启动了(#186)。
- `status`、`doctor` 和事件日志里的时间改为本地时间并标明时区,之前显示的是 UTC 却没有说明(#184)。
- `iphone-control update`、`service` 和控制台更新面板里的报错跟随界面语言,之前总是英文(#184)。
Expand Down
206 changes: 183 additions & 23 deletions crates/ictl-ax/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! `deviceAPIVersion` before `clientNeedsAccessibility:` keeps it answering without WDA (round 3).

use std::collections::{HashMap, HashSet, VecDeque};
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

Expand Down Expand Up @@ -84,6 +84,9 @@ pub struct Session<S> {
/// When the phone last said the screen is changing (a notification before the next tree)
changes: watch::Receiver<Option<Instant>>,
closed: watch::Receiver<bool>,
/// A request stopped halfway (cancelled, or its write failed): what is on the stream is no longer known (half a
/// message written, an answer still to come for nobody), so the session counts as closed and is opened again
abandoned: AtomicBool,
requests: tokio::sync::Mutex<()>,
next_id: AtomicU32,
reader: tokio::task::JoinHandle<()>,
Expand All @@ -95,6 +98,41 @@ impl<S> Drop for Session<S> {
}
}

/// One request on the stream: unless [`InFlight::done`] is called, dropping it (the request was cancelled, or failed
/// writing) abandons the session ([`Session::is_closed`]) and stops its reader, which would otherwise go on answering
/// the phone on a stream that may hold half a message; the answer channel is let go either way
struct InFlight<'a> {
abandoned: &'a AtomicBool,
waiter: &'a Waiter,
reader: &'a tokio::task::JoinHandle<()>,
done: bool,
}

impl InFlight<'_> {
fn done(mut self) {
self.done = true;
}
}

impl Drop for InFlight<'_> {
fn drop(&mut self) {
if !self.done {
self.abandoned.store(true, Ordering::SeqCst);
self.reader.abort();
}
self.waiter.lock().unwrap_or_else(|p| p.into_inner()).take();
}
}

/// Writing one request, at most this long: a stream that takes no more means the phone or the link stopped
const WRITE_TIMEOUT: Duration = Duration::from_secs(5);

/// A write that didn't finish in time: the link, not the service (an unanswered service is [`AxError::Timeout`],
/// which the daemon restarts it for)
fn write_stalled() -> AxError {
AxError::Io(std::io::Error::new(std::io::ErrorKind::TimedOut, "writing a request took too long"))
}

async fn send<W: AsyncWrite + Unpin>(w: &mut W, bytes: &[u8]) -> Result<()> {
w.write_all(bytes).await?;
w.flush().await?;
Expand Down Expand Up @@ -151,6 +189,7 @@ impl<S: AsyncRead + AsyncWrite + Unpin + Send + 'static> Session<S> {
pushes,
changes,
closed,
abandoned: AtomicBool::new(false),
requests: tokio::sync::Mutex::new(()),
next_id: AtomicU32::new(4),
reader,
Expand All @@ -174,8 +213,12 @@ async fn read_loop<S: AsyncRead + AsyncWrite + Unpin + Send>(
break;
}
};
// a reply that can't be written in time: the link stopped, the session is done
if let Some(b) = answer(&f)
&& send(&mut *writer.lock().await, &b).await.is_err()
&& !matches!(
tokio::time::timeout(WRITE_TIMEOUT, async { send(&mut *writer.lock().await, &b).await }).await,
Ok(Ok(()))
)
{
break;
}
Expand Down Expand Up @@ -225,8 +268,21 @@ fn describe(o: &Obj) -> String {
}

impl<S: AsyncRead + AsyncWrite + Unpin + Send + 'static> Session<S> {
/// Registers for the next answers, once the stream is known to be open (checked after registering: a stream
/// that closed in between would otherwise leave the request waiting out its timeout)
fn start(&self) -> Result<(mpsc::UnboundedReceiver<Response>, InFlight<'_>)> {
let (tx, rx) = mpsc::unbounded_channel();
*self.waiter.lock().unwrap_or_else(|p| p.into_inner()) = Some(tx);
let flight = InFlight { abandoned: &self.abandoned, waiter: &self.waiter, reader: &self.reader, done: false };
if self.is_closed() {
flight.done();
return Err(AxError::Closed);
}
Ok((rx, flight))
}

pub fn is_closed(&self) -> bool {
*self.closed.borrow()
*self.closed.borrow() || self.abandoned.load(Ordering::SeqCst)
}

/// The last tree the phone pushed
Expand Down Expand Up @@ -271,29 +327,43 @@ impl<S: AsyncRead + AsyncWrite + Unpin + Send + 'static> Session<S> {
return Err(AxError::Closed);
}
let _one = self.requests.lock().await;
let (tx, mut rx) = mpsc::unbounded_channel();
*self.waiter.lock().unwrap_or_else(|p| p.into_inner()) = Some(tx);
let (mut rx, flight) = self.start()?;
let mut closed = self.closed.clone();
let r = async {
let body = archive::encode_data(&archive::encode_request(request_type, 0, object, params, CLIENT_TYPE));
let id = self.next_id.fetch_add(1, Ordering::Relaxed);
send(&mut *self.writer.lock().await, &dtx::invocation(id, "processDataFromHost:", vec![body], false))
.await?;
let msg = dtx::invocation(id, "processDataFromHost:", vec![body], false);
match tokio::time::timeout(WRITE_TIMEOUT, async { send(&mut *self.writer.lock().await, &msg).await }).await
{
Ok(r) => r?,
Err(_) => return Err(write_stalled()),
}
let deadline = tokio::time::Instant::now() + REQUEST_TIMEOUT;
loop {
let r = match tokio::time::timeout_at(deadline, rx.recv()).await {
Ok(Some(r)) => r,
Ok(None) => return Err(AxError::Closed),
Err(_) => return Err(AxError::Timeout("request")),
let r = tokio::select! {
r = tokio::time::timeout_at(deadline, rx.recv()) => match r {
Ok(Some(r)) => r,
Ok(None) => return Err(AxError::Closed),
Err(_) => return Ok(Err(AxError::Timeout("request"))),
},
_ = closed.wait_for(|c| *c) => return Err(AxError::Closed),
};
let same_object = object.is_none_or(|o| r.object.is_none_or(|a| (a.pid, a.id) == (o.pid, o.id)));
if r.request_type == request_type && same_object {
return Ok(r);
return Ok(Ok(r));
}
tracing::debug!("remoteAXService: passed over a late answer to request type {}", r.request_type);
}
}
.await;
self.waiter.lock().unwrap_or_else(|p| p.into_inner()).take();
// written whole: an unanswered request (a timeout) leaves the stream as it was; a late answer is passed over
let r = match r {
Ok(r) => {
flight.done();
r
}
Err(e) => return Err(e),
};
let r = r?;
if r.error != 0 {
return Err(AxError::Protocol(format!("request type {request_type} failed: error {}", r.error)));
Expand All @@ -312,11 +382,11 @@ impl<S: AsyncRead + AsyncWrite + Unpin + Send + 'static> Session<S> {
return Ok(out);
}
let _one = self.requests.lock().await;
let (tx, mut rx) = mpsc::unbounded_channel();
*self.waiter.lock().unwrap_or_else(|p| p.into_inner()) = Some(tx);
let (mut rx, flight) = self.start()?;
let mut closed = self.closed.clone();
let r = async {
let list = Param::Ints(attr::WALK.to_vec());
{
let write = async {
let mut w = self.writer.lock().await;
for o in objects {
let req = archive::encode_request(
Expand All @@ -331,15 +401,24 @@ impl<S: AsyncRead + AsyncWrite + Unpin + Send + 'static> Session<S> {
w.write_all(&msg).await?;
}
w.flush().await?;
Ok::<_, AxError>(())
};
let many = WRITE_TIMEOUT + Duration::from_millis(5) * objects.len() as u32;
match tokio::time::timeout(many, write).await {
Ok(r) => r?,
Err(_) => return Err(write_stalled()),
}
let mut pending: Vec<ObjectRef> = objects.to_vec();
let deadline =
tokio::time::Instant::now() + REQUEST_TIMEOUT + Duration::from_millis(20) * objects.len() as u32;
while !pending.is_empty() {
let r = match tokio::time::timeout_at(deadline, rx.recv()).await {
Ok(Some(r)) => r,
Ok(None) => return Err(AxError::Closed),
Err(_) => return Err(AxError::Timeout("attributes")),
let r = tokio::select! {
r = tokio::time::timeout_at(deadline, rx.recv()) => match r {
Ok(Some(r)) => r,
Ok(None) => return Err(AxError::Closed),
Err(_) => return Ok(Err(AxError::Timeout("attributes"))),
},
_ = closed.wait_for(|c| *c) => return Err(AxError::Closed),
};
if r.request_type != request::ATTRIBUTES {
continue;
Expand All @@ -355,11 +434,16 @@ impl<S: AsyncRead + AsyncWrite + Unpin + Send + 'static> Session<S> {
out.insert(o, r.result);
}
}
Ok(())
Ok(Ok(()))
}
.await;
self.waiter.lock().unwrap_or_else(|p| p.into_inner()).take();
r.map(|()| out)
match r {
Ok(r) => {
flight.done();
r.map(|()| out)
}
Err(e) => Err(e),
}
}

/// The frontmost application's element
Expand Down Expand Up @@ -474,6 +558,11 @@ impl<S: AsyncRead + AsyncWrite + Unpin + Send + 'static> Session<S> {

/// Tells the phone this client no longer needs accessibility (it then closes the connection)
pub async fn close(&self) {
// half a request may be on the stream: nothing more is written, the connection just goes
if self.abandoned.load(Ordering::SeqCst) {
self.reader.abort();
return;
}
let no = archive::encode_scalar(Value::Boolean(false));
let id = self.next_id.fetch_add(1, Ordering::Relaxed);
let msg = dtx::invocation(id, "clientNeedsAccessibility:", vec![no], false);
Expand All @@ -483,3 +572,74 @@ impl<S: AsyncRead + AsyncWrite + Unpin + Send + 'static> Session<S> {
self.reader.abort();
}
}

#[cfg(test)]
mod tests {
use super::*;

/// A phone that answers the handshake and then reads everything without answering, until `hang_up` fires
async fn phone(hang_up: tokio::sync::oneshot::Receiver<()>) -> Session<tokio::io::DuplexStream> {
let (ours, theirs) = tokio::io::duplex(1 << 20);
tokio::spawn(async move {
let (mut r, mut w) = tokio::io::split(theirs);
let serve = async {
while let Ok(f) = dtx::read_frame(&mut r).await {
if f.selector().as_deref() == Some("deviceAPIVersion") {
let _ = send(&mut w, &dtx::reply(&f, Some(Value::Integer(7u64.into())))).await;
}
}
};
tokio::select! {
() = serve => {}
_ = hang_up => {}
}
});
Session::connect(ours, Duration::from_secs(2)).await.unwrap()
}

/// A request dropped halfway (a background check that gave way to an action) leaves the stream in an unknown
/// state: the session counts as closed, so the next request opens a new one instead of reading a stale answer
#[tokio::test]
async fn a_cancelled_request_abandons_the_session() {
let (_keep, rx) = tokio::sync::oneshot::channel();
let s = phone(rx).await;
assert!(!s.is_closed());
let cancelled = tokio::time::timeout(Duration::from_millis(50), s.request(request::FRONTMOST_APP, None, &[]));
assert!(cancelled.await.is_err());
assert!(s.is_closed(), "abandoned");
assert!(matches!(s.request(request::FRONTMOST_APP, None, &[]).await, Err(AxError::Closed)));
// nothing reads or answers on that stream any more
tokio::time::timeout(Duration::from_secs(1), async {
while !s.reader.is_finished() {
tokio::time::sleep(Duration::from_millis(5)).await;
}
})
.await
.expect("the reader stopped");
}

/// Not answered in time (the service is stuck, which the daemon may restart it for): the request was written
/// whole, so the session stays usable
#[tokio::test]
async fn an_unanswered_request_keeps_the_session() {
let (_keep, rx) = tokio::sync::oneshot::channel();
let s = phone(rx).await;
assert!(matches!(s.request(request::FRONTMOST_APP, None, &[]).await, Err(AxError::Timeout(_))));
assert!(!s.is_closed());
}

/// The phone hangs up while a request waits: it ends at once, not when its time is up
#[tokio::test]
async fn a_closed_connection_ends_the_wait() {
let (hang_up, rx) = tokio::sync::oneshot::channel();
let s = phone(rx).await;
let t0 = Instant::now();
let asked = s.request(request::FRONTMOST_APP, None, &[]);
let (r, ()) = tokio::join!(asked, async {
tokio::time::sleep(Duration::from_millis(100)).await;
let _ = hang_up.send(());
});
assert!(matches!(r, Err(AxError::Closed)), "{r:?}");
assert!(t0.elapsed() < REQUEST_TIMEOUT, "{:?}", t0.elapsed());
}
}
Loading
Loading