Skip to content

Commit 030f3d9

Browse files
committed
Handle transient CoreSimulator hierarchy errors
1 parent 1f3843f commit 030f3d9

2 files changed

Lines changed: 36 additions & 12 deletions

File tree

server/src/api/routes.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -495,13 +495,12 @@ async fn accessibility_tree(
495495
fallback_reason,
496496
)))
497497
}
498-
Err(inspector_error) => {
498+
Err(_inspector_error) => {
499499
let available_sources = available_sources_with_native_ax(Some(&session));
500500
match state.registry.bridge().accessibility_snapshot(&udid, None) {
501-
Ok(native_snapshot) => Ok(json(attach_tree_metadata(
501+
Ok(native_snapshot) => Ok(json(attach_available_sources(
502502
native_snapshot,
503503
&available_sources,
504-
Some(inspector_error),
505504
))),
506505
Err(native_ax_error) => Ok(json(empty_accessibility_tree(
507506
SOURCE_NATIVE_AX,
@@ -512,13 +511,12 @@ async fn accessibility_tree(
512511
}
513512
}
514513
}
515-
Err(inspector_error) => {
514+
Err(_inspector_error) => {
516515
let available_sources = available_sources_with_native_ax(None);
517516
match state.registry.bridge().accessibility_snapshot(&udid, None) {
518-
Ok(native_snapshot) => Ok(json(attach_tree_metadata(
517+
Ok(native_snapshot) => Ok(json(attach_available_sources(
519518
native_snapshot,
520519
&available_sources,
521-
Some(inspector_error),
522520
))),
523521
Err(native_ax_error) => Ok(json(empty_accessibility_tree(
524522
SOURCE_NATIVE_AX,
@@ -990,12 +988,20 @@ fn empty_accessibility_tree(
990988
}
991989

992990
fn suppress_native_ax_translation_error(message: &str) -> Option<String> {
993-
if message.contains("No translation object returned for simulator") {
991+
if message.contains("No translation object returned for simulator")
992+
|| is_core_simulator_service_mismatch(message)
993+
{
994994
return None;
995995
}
996996
Some(message.to_owned())
997997
}
998998

999+
fn is_core_simulator_service_mismatch(message: &str) -> bool {
1000+
message.contains("CoreSimulator.framework was changed while the process was running")
1001+
|| message.contains("Service version")
1002+
&& message.contains("does not match expected service version")
1003+
}
1004+
9991005
fn attach_tree_metadata(
10001006
mut snapshot: Value,
10011007
available_sources: &[String],

server/src/native/bridge.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use serde::de::Error as DeError;
44
use serde::{Deserialize, Serialize};
55
use std::ffi::{c_void, CStr, CString};
66
use std::ptr;
7+
use std::time::Duration;
78

89
#[derive(Debug, Clone, Serialize, Deserialize)]
910
pub struct Simulator {
@@ -254,11 +255,13 @@ impl NativeBridge {
254255
point: Option<(f64, f64)>,
255256
) -> Result<serde_json::Value, AppError> {
256257
let udid = CString::new(udid).map_err(|e| AppError::bad_request(e.to_string()))?;
257-
let json = unsafe {
258-
let mut error = ptr::null_mut();
259-
let raw =
260-
ffi::xcw_native_accessibility_snapshot(udid.as_ptr(), false, 0.0, 0.0, &mut error);
261-
string_from_raw(raw, error)?
258+
let json = match native_accessibility_snapshot_json(&udid) {
259+
Ok(json) => json,
260+
Err(error) if is_core_simulator_service_mismatch(&error.to_string()) => {
261+
std::thread::sleep(Duration::from_millis(250));
262+
native_accessibility_snapshot_json(&udid)?
263+
}
264+
Err(error) => return Err(error),
262265
};
263266
let snapshot: serde_json::Value =
264267
serde_json::from_str(&json).map_err(|e| AppError::internal(e.to_string()))?;
@@ -613,6 +616,21 @@ impl Drop for NativeSession {
613616
}
614617
}
615618

619+
fn native_accessibility_snapshot_json(udid: &CString) -> Result<String, AppError> {
620+
unsafe {
621+
let mut error = ptr::null_mut();
622+
let raw =
623+
ffi::xcw_native_accessibility_snapshot(udid.as_ptr(), false, 0.0, 0.0, &mut error);
624+
string_from_raw(raw, error)
625+
}
626+
}
627+
628+
fn is_core_simulator_service_mismatch(message: &str) -> bool {
629+
message.contains("CoreSimulator.framework was changed while the process was running")
630+
|| message.contains("Service version")
631+
&& message.contains("does not match expected service version")
632+
}
633+
616634
fn accessibility_snapshot_at_point(
617635
snapshot: serde_json::Value,
618636
x: f64,

0 commit comments

Comments
 (0)