@@ -276,14 +276,17 @@ async function main() {
276276 ) ,
277277 { phase : phaseSimulatorLifecycle } ,
278278 ) ;
279+ await measuredStep ( "close JS API session" , ( ) => closeSession ( ) , {
280+ phase : phaseSimulatorLifecycle ,
281+ } ) ;
279282 await measuredStep (
280283 "shutdown simulator" ,
281284 ( ) => shutdownSimulatorIfNeeded ( simulatorUDID ) ,
282285 { phase : phaseSimulatorLifecycle } ,
283286 ) ;
284287 await measuredStep (
285288 "erase simulator" ,
286- ( ) => runJson ( simdeck , [ "erase" , simulatorUDID ] ) ,
289+ ( ) => eraseSimulatorReliably ( simulatorUDID ) ,
287290 {
288291 phase : phaseSimulatorLifecycle ,
289292 } ,
@@ -542,7 +545,10 @@ function preapproveFixtureUrlScheme() {
542545
543546function shutdownSimulatorIfNeeded ( udid ) {
544547 try {
545- return runJson ( simdeck , [ "shutdown" , udid ] ) ;
548+ runText ( "xcrun" , [ "simctl" , "shutdown" , udid ] , {
549+ timeoutMs : 180_000 ,
550+ } ) ;
551+ return { ok : true , udid, action : "shutdown" } ;
546552 } catch ( error ) {
547553 if ( String ( error ?. message ?? error ) . includes ( "current state: Shutdown" ) ) {
548554 return { ok : true , udid, alreadyShutdown : true } ;
@@ -551,6 +557,27 @@ function shutdownSimulatorIfNeeded(udid) {
551557 }
552558}
553559
560+ function eraseSimulatorReliably ( udid ) {
561+ return retrySync (
562+ ( ) => {
563+ shutdownSimulatorIfNeeded ( udid ) ;
564+ runText ( "xcrun" , [ "simctl" , "erase" , udid ] , {
565+ timeoutMs : 180_000 ,
566+ } ) ;
567+ return { ok : true , udid, action : "erase" } ;
568+ } ,
569+ "erase simulator" ,
570+ 3 ,
571+ 3_000 ,
572+ ) ;
573+ }
574+
575+ function closeSession ( ) {
576+ session ?. close ( ) ;
577+ session = null ;
578+ return { ok : true } ;
579+ }
580+
554581function assertRoots ( payload , label ) {
555582 assertJson ( payload , label ) ;
556583 if ( ! Array . isArray ( payload . roots ) || payload . roots . length === 0 ) {
@@ -720,7 +747,7 @@ function openSimulatorApp(udid) {
720747
721748function cleanup ( ) {
722749 try {
723- session ?. close ( ) ;
750+ closeSession ( ) ;
724751 } catch { }
725752 if ( simulatorUDID && ! keepSimulator ) {
726753 spawnSync ( "xcrun" , [ "simctl" , "shutdown" , simulatorUDID ] , {
0 commit comments