Skip to content

Commit 98769d4

Browse files
Fix killing simctl process and caller
In case when ios-sim-portable is used as a library, in a CLI for example, sending Ctrl + C should kill the CLI process and its related resources. However, in case the code is currently executing `xcrun simctl` command, sending Ctrl + C does not kill the CLI process. There are two reasons for this: * we use Node.js's `child_process.spawnSync` method to call `xcrun simctl` commands * simctl handles the killing signals in its own manner When the executable passed to spawnSync method has its own logic for handling Signals for termination, Node.js process does not receive the Signals. That's why, the spawned process just exits and CLI's code continues. However, the returned result has a signal property showing the signal used to kill the process. In order to send the same signal to CLI, just check if the `signal` property is set and send it to the current Node.js process as well.
1 parent 0b52a9c commit 98769d4

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

lib/simctl.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ export class Simctl implements ISimctl {
3232
}
3333

3434
public install(deviceId: string, applicationPath: string): void {
35-
return this.simctlExec("install", [deviceId, applicationPath]);
35+
this.simctlExec("install", [deviceId, applicationPath]);
3636
}
3737

3838
public uninstall(deviceId: string, appIdentifier: string, opts?: any): void {
39-
return this.simctlExec("uninstall", [deviceId, appIdentifier], opts);
39+
this.simctlExec("uninstall", [deviceId, appIdentifier], opts);
4040
}
4141

4242
public notifyPost(deviceId: string, notification: string): void {
43-
return this.simctlExec("notify_post", [deviceId, notification]);
43+
this.simctlExec("notify_post", [deviceId, notification]);
4444
}
4545

4646
public getAppContainer(deviceId: string, appIdentifier: string): string {
@@ -120,11 +120,20 @@ export class Simctl implements ISimctl {
120120
return devices;
121121
}
122122

123-
private simctlExec(command: string, args: string[], opts?: any): any {
124-
let result = childProcess.spawnSync("xcrun", ["simctl", command].concat(args), opts);
125-
if(result && result.stdout) {
126-
return result.stdout.toString().trim();
123+
private simctlExec(command: string, args: string[], opts?: any): string {
124+
const result = childProcess.spawnSync("xcrun", ["simctl", command].concat(args), opts);
125+
if (result) {
126+
if (result.signal) {
127+
// In some cases, sending Ctrl + C (SIGINT) is handled by the simctl itself and spawnSync finishes, but the SIGINT does not stop current process.
128+
// In order to ensure the same signal is sent to the caller (CLI for example), send the signal manually:
129+
process.send(result.signal);
130+
}
131+
132+
if (result.stdout) {
133+
return result.stdout.toString().trim();
134+
}
127135
}
136+
128137
return '';
129138
}
130139
}

0 commit comments

Comments
 (0)