@@ -127,7 +127,7 @@ - (NSString *)installApplicationWithHost:(NSString *)testHost withError:(NSError
127127 }];
128128 return nil ;
129129 } else {
130- [simDevice shutdownWithError : errPtr];
130+ [self shutdownSimulatorWithError : errPtr];
131131 if (*errPtr) {
132132 [BPUtils printInfo: ERROR withString: @" Shutdown simulator failed with error: %@ " , [*errPtr localizedDescription ]];
133133 [deviceSet deleteDeviceAsync: simDevice completionHandler: ^(NSError *error) {
@@ -307,6 +307,19 @@ - (BOOL)useSimulatorWithDeviceUDID:(NSUUID *)deviceUDID {
307307 return YES ;
308308}
309309
310+ - (BOOL )shutdownSimulatorWithError : (id *)error {
311+ [BPUtils printInfo: INFO withString: @" Starting Safe Shutdown of %@ " , self .device.UDID.UUIDString];
312+
313+ // Calling shutdown when already shutdown should be avoided (if detected).
314+ if ([self .device.stateString isEqualToString: @" Shutdown" ]) {
315+ [BPUtils printInfo: INFO withString: @" Shutdown of %@ succeeded as it is already shutdown" , self .device];
316+ *error = nil ;
317+ return YES ;
318+ }
319+
320+ return [self .device shutdownWithError: error];
321+ }
322+
310323- (void )bootWithCompletion : (void (^)(NSError *error))completion {
311324 // Now boot it.
312325 [BPUtils printInfo: INFO withString: @" Booting a simulator without launching Simulator app" ];
@@ -320,7 +333,7 @@ - (void)openSimulatorHeadlessWithCompletion:(void (^)(NSError *))completion {
320333 [self .device bootAsyncWithOptions: options completionHandler: ^(NSError *bootError){
321334 NSError *error = [self waitForDeviceReady ];
322335 if (error) {
323- [self .device shutdownWithError : &error];
336+ [self shutdownSimulatorWithError : &error];
324337 if (error) {
325338 [BPUtils printInfo: ERROR withString: @" Shutting down Simulator failed: %@ " , [error localizedDescription ]];
326339 }
@@ -571,7 +584,7 @@ - (void)deleteSimulatorWithCompletion:(void (^)(NSError *error, BOOL success))co
571584 }
572585 if (self.device ) {
573586 [BPUtils printInfo: INFO withString: @" Shutting down Simulator" ];
574- [self .device shutdownWithError : &error];
587+ [self shutdownSimulatorWithError : &error];
575588 if (error) {
576589 [BPUtils printInfo: ERROR withString: @" Shutting down Simulator failed: %@ " , [error localizedDescription ]];
577590 completion (error, NO );
0 commit comments