Skip to content

Commit e7039a0

Browse files
fix(debug): debug-brk option does not work for iOS Simulator (#3340)
The `debug-brk` option was broken in a previous commit as we were trying to connect to the backend port (18181). The problem is that connecting to it makes the runtime thinks debugger is attached and it continues to next statements. Instead of connecting to the port, check if it is in LISTEN state.
1 parent 0126afc commit e7039a0

File tree

3 files changed

+31
-29
lines changed

3 files changed

+31
-29
lines changed

lib/services/ios-debug-service.ts

+25-25
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
3232
private $iOSNotification: IiOSNotification,
3333
private $iOSSocketRequestExecutor: IiOSSocketRequestExecutor,
3434
private $processService: IProcessService,
35-
private $socketProxyFactory: ISocketProxyFactory) {
35+
private $socketProxyFactory: ISocketProxyFactory,
36+
private $net: INet) {
3637
super(device, $devicesService);
3738
this.$processService.attachToProcessExitSignals(this, this.debugStop);
3839
this.$socketProxyFactory.on(CONNECTION_ERROR_EVENT_NAME, (e: Error) => this.emit(CONNECTION_ERROR_EVENT_NAME, e));
@@ -125,29 +126,28 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
125126
const lineStream = byline(child_process.stdout);
126127
this._childProcess = child_process;
127128

128-
await new Promise((resolve: () => void, reject) => {
129-
lineStream.on('data', (line: NodeBuffer) => {
130-
const lineText = line.toString();
131-
if (lineText && _.startsWith(lineText, debugData.applicationIdentifier)) {
132-
const pid = getPidFromiOSSimulatorLogs(debugData.applicationIdentifier, lineText);
133-
if (!pid) {
134-
this.$logger.trace(`Line ${lineText} does not contain PID of the application ${debugData.applicationIdentifier}.`);
135-
return;
136-
}
137-
138-
this._lldbProcess = this.$childProcess.spawn("lldb", ["-p", pid]);
139-
if (log4js.levels.TRACE.isGreaterThanOrEqualTo(this.$logger.getLevel())) {
140-
this._lldbProcess.stdout.pipe(process.stdout);
141-
}
142-
this._lldbProcess.stderr.pipe(process.stderr);
143-
this._lldbProcess.stdin.write("process continue\n");
144-
this.connectToApplicationOnEmulator(debugData.deviceIdentifier).then(resolve, reject);
145-
} else {
146-
process.stdout.write(line + "\n");
129+
lineStream.on('data', (line: NodeBuffer) => {
130+
const lineText = line.toString();
131+
if (lineText && _.startsWith(lineText, debugData.applicationIdentifier)) {
132+
const pid = getPidFromiOSSimulatorLogs(debugData.applicationIdentifier, lineText);
133+
if (!pid) {
134+
this.$logger.trace(`Line ${lineText} does not contain PID of the application ${debugData.applicationIdentifier}.`);
135+
return;
147136
}
148-
});
137+
138+
this._lldbProcess = this.$childProcess.spawn("lldb", ["-p", pid]);
139+
if (log4js.levels.TRACE.isGreaterThanOrEqualTo(this.$logger.getLevel())) {
140+
this._lldbProcess.stdout.pipe(process.stdout);
141+
}
142+
this._lldbProcess.stderr.pipe(process.stderr);
143+
this._lldbProcess.stdin.write("process continue\n");
144+
} else {
145+
process.stdout.write(line + "\n");
146+
}
149147
});
150148

149+
await this.waitForBackendPortToBeOpened(debugData.deviceIdentifier);
150+
151151
return this.wireDebuggerClient(debugData, debugOptions);
152152
}
153153

@@ -158,13 +158,13 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
158158

159159
const iOSEmulatorService = <Mobile.IiOSSimulatorService>this.$iOSEmulatorServices;
160160
await iOSEmulatorService.postDarwinNotification(attachRequestMessage);
161-
await this.connectToApplicationOnEmulator(debugData.deviceIdentifier);
161+
await this.waitForBackendPortToBeOpened(debugData.deviceIdentifier);
162162
return result;
163163
}
164164

165-
private async connectToApplicationOnEmulator(deviceIdentifier: string): Promise<void> {
166-
const socket = await this.$iOSEmulatorServices.connectToPort({ port: inspectorBackendPort });
167-
if (!socket) {
165+
private async waitForBackendPortToBeOpened(deviceIdentifier: string): Promise<void> {
166+
const portListens = await this.$net.waitForPortToListen({ port: inspectorBackendPort, timeout: 10000, interval: 200 });
167+
if (!portListens) {
168168
const error = <Mobile.IDeviceError>new Error("Unable to connect to application. Ensure application is running on simulator.");
169169
error.deviceIdentifier = deviceIdentifier;
170170
throw error;

test/services/ios-debug-service.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ class IOSDebugServiceInheritor extends IOSDebugService {
1717
$iOSNotification: IiOSNotification,
1818
$iOSSocketRequestExecutor: IiOSSocketRequestExecutor,
1919
$processService: IProcessService,
20-
$socketProxyFactory: ISocketProxyFactory) {
20+
$socketProxyFactory: ISocketProxyFactory,
21+
$net: INet) {
2122
super(<any>{}, $devicesService, $platformService, $iOSEmulatorServices, $childProcess, $hostInfo, $logger, $errors,
22-
$npmInstallationManager, $iOSNotification, $iOSSocketRequestExecutor, $processService, $socketProxyFactory);
23+
$npmInstallationManager, $iOSNotification, $iOSSocketRequestExecutor, $processService, $socketProxyFactory, $net);
2324
}
2425

2526
public getChromeDebugUrl(debugOptions: IDebugOptions, port: number): string {
@@ -49,7 +50,8 @@ const createTestInjector = (): IInjector => {
4950
});
5051

5152
testInjector.register("net", {
52-
getAvailablePortInRange: async (startPort: number, endPort?: number): Promise<number> => 41000
53+
getAvailablePortInRange: async (startPort: number, endPort?: number): Promise<number> => 41000,
54+
waitForPortToListen: async (opts: { port: number, timeout: number, interval?: number }): Promise<boolean> => true
5355
});
5456

5557
return testInjector;

0 commit comments

Comments
 (0)