Skip to content

Commit c464b28

Browse files
committed
Stabilize simulator lifecycle teardown
1 parent eaf5808 commit c464b28

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

server/src/api/routes.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ async fn boot_simulator(
546546
State(state): State<AppState>,
547547
Path(udid): Path<String>,
548548
) -> Result<Json<Value>, AppError> {
549-
state.registry.remove(&udid);
549+
forget_lifecycle_session(&state, &udid);
550550
let action_udid = udid.clone();
551551
run_bridge_action(state.clone(), move |bridge| {
552552
bridge.boot_simulator(&action_udid)
@@ -559,25 +559,33 @@ async fn shutdown_simulator(
559559
State(state): State<AppState>,
560560
Path(udid): Path<String>,
561561
) -> Result<Json<Value>, AppError> {
562+
forget_lifecycle_session(&state, &udid);
562563
let action_udid = udid.clone();
563564
run_bridge_action(state.clone(), move |bridge| {
564565
bridge.shutdown_simulator(&action_udid)
565566
})
566567
.await?;
567-
state.registry.remove(&udid);
568568
simulator_payload(state, udid).await
569569
}
570570

571571
async fn erase_simulator(
572572
State(state): State<AppState>,
573573
Path(udid): Path<String>,
574574
) -> Result<Json<Value>, AppError> {
575-
state.registry.remove(&udid);
575+
forget_lifecycle_session(&state, &udid);
576576
let action_udid = udid.clone();
577577
run_bridge_action(state, move |bridge| bridge.erase_simulator(&action_udid)).await?;
578578
Ok(json(json_value!({ "ok": true })))
579579
}
580580

581+
fn forget_lifecycle_session(state: &AppState, udid: &str) {
582+
// SimulatorKit can reset the server-side connection if a cached private
583+
// display session is destructed while CoreSimulator is booting, shutting
584+
// down, or erasing the same device. Detach it from the registry without
585+
// running Objective-C teardown on the lifecycle response path.
586+
state.registry.forget(udid);
587+
}
588+
581589
async fn install_app(
582590
State(state): State<AppState>,
583591
Path(udid): Path<String>,

server/src/simulators/registry.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ impl<T: Clone> SessionStore<T> {
4444
fn remove(&self, key: &str) {
4545
self.sessions.lock().unwrap().remove(key);
4646
}
47+
48+
fn forget(&self, key: &str) {
49+
if let Some(value) = self.sessions.lock().unwrap().remove(key) {
50+
std::mem::forget(value);
51+
}
52+
}
4753
}
4854

4955
#[derive(Clone)]
@@ -106,6 +112,10 @@ impl<T: Clone + Send + 'static> SessionRegistry<T> {
106112
pub fn remove(&self, udid: &str) {
107113
self.store.remove(udid);
108114
}
115+
116+
pub fn forget(&self, udid: &str) {
117+
self.store.forget(udid);
118+
}
109119
}
110120

111121
impl SessionRegistry<SimulatorSession> {

0 commit comments

Comments
 (0)