Skip to content

fix: do not wait 60 seconds for debugger port when the app is crashed #4455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class EmulatorDiscoveryNames {

export const DEVICE_LOG_EVENT_NAME = "deviceLogData";
export const IOS_LOG_PREDICATE = 'senderImagePath contains "NativeScript" || eventMessage contains[c] "NativeScript"';
export const IOS_APP_CRASH_LOG_REG_EXP = /Fatal JavaScript exception \- application has been terminated/;

export const TARGET_FRAMEWORK_IDENTIFIERS = {
Cordova: "Cordova",
Expand Down
3 changes: 1 addition & 2 deletions lib/common/mobile/ios/device/ios-device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ export class IOSDevice extends IOSDeviceBase {
}
}

protected async getDebugSocketCore(appId: string, projectName: string): Promise<net.Socket> {
await super.attachToDebuggerFoundEvent(projectName);
protected async getDebugSocketCore(appId: string): Promise<net.Socket> {
await this.$iOSSocketRequestExecutor.executeAttachRequest(this, constants.AWAIT_NOTIFICATION_TIMEOUT_SECONDS, appId);
const port = await super.getDebuggerPort(appId);
const deviceId = this.deviceInfo.identifier;
Expand Down
10 changes: 6 additions & 4 deletions lib/common/mobile/ios/ios-device-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export abstract class IOSDeviceBase implements Mobile.IiOSDevice {
return this.cachedSockets[appId];
}

this.cachedSockets[appId] = await this.getDebugSocketCore(appId, projectName);
await this.attachToDebuggerFoundEvent(appId, projectName);
await this.applicationManager.startApplication({ appId, projectName });
this.cachedSockets[appId] = await this.getDebugSocketCore(appId);

if (this.cachedSockets[appId]) {
this.cachedSockets[appId].on("close", async () => {
Expand All @@ -38,11 +40,11 @@ export abstract class IOSDeviceBase implements Mobile.IiOSDevice {
);
}

protected abstract async getDebugSocketCore(appId: string, projectName: string): Promise<net.Socket>;
protected abstract async getDebugSocketCore(appId: string): Promise<net.Socket>;

protected async attachToDebuggerFoundEvent(projectName: string): Promise<void> {
protected async attachToDebuggerFoundEvent(appId: string, projectName: string): Promise<void> {
await this.startDeviceLogProcess(projectName);
await this.$iOSDebuggerPortService.attachToDebuggerPortFoundEvent();
await this.$iOSDebuggerPortService.attachToDebuggerPortFoundEvent(appId);
}

protected async getDebuggerPort(appId: string): Promise<number> {
Expand Down
3 changes: 1 addition & 2 deletions lib/common/mobile/ios/simulator/ios-simulator-device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ export class IOSSimulator extends IOSDeviceBase implements Mobile.IiOSDevice {
return this.$iOSSimulatorLogProvider.startLogProcess(this.simulator.id, options);
}

protected async getDebugSocketCore(appId: string, projectName: string): Promise<net.Socket> {
protected async getDebugSocketCore(appId: string): Promise<net.Socket> {
let socket: net.Socket;
await super.attachToDebuggerFoundEvent(projectName);
const attachRequestMessage = this.$iOSNotification.getAttachRequest(appId, this.deviceInfo.identifier);
await this.$iOSEmulatorServices.postDarwinNotification(attachRequestMessage, this.deviceInfo.identifier);
const port = await super.getDebuggerPort(appId);
Expand Down
1 change: 1 addition & 0 deletions lib/definitions/hmr-status-service.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
interface IHmrStatusService {
watchHmrStatus(deviceId: string, operationHash: string): void;
getHmrStatus(deviceId: string, operationHash: string): Promise<number>;
attachToHmrStatusEvent(): void;
}
3 changes: 2 additions & 1 deletion lib/definitions/ios-debugger-port-service.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface IIOSDebuggerPortData {
interface IIOSDebuggerPortStoredData {
port: number;
timer?: NodeJS.Timer;
error?: Error;
}

interface IIOSDebuggerPortService {
Expand All @@ -24,5 +25,5 @@ interface IIOSDebuggerPortService {
* Attaches on DEBUGGER_PORT_FOUND event and stores the port
* @returns {Promise<void>}
*/
attachToDebuggerPortFoundEvent(): Promise<void>;
attachToDebuggerPortFoundEvent(appId: string): Promise<void>;
}
30 changes: 25 additions & 5 deletions lib/services/hmr-status-service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cache } from "../common/decorators";
import { HmrConstants } from "../common/constants";
import { HmrConstants, IOS_APP_CRASH_LOG_REG_EXP } from "../common/constants";

export class HmrStatusService implements IHmrStatusService {
public static HMR_STATUS_LOG_REGEX = /([a-z A-Z]*) hmr hash ([a-z0-9]*)\./;
Expand All @@ -11,9 +11,10 @@ export class HmrStatusService implements IHmrStatusService {

constructor(private $logParserService: ILogParserService,
private $processService: IProcessService,
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
private $logger: ILogger) {
this.$processService.attachToProcessExitSignals(this, this.dispose);
}
this.$processService.attachToProcessExitSignals(this, this.dispose);
}

public getHmrStatus(deviceId: string, operationHash: string): Promise<number> {
return new Promise((resolve, reject) => {
Expand All @@ -33,13 +34,32 @@ export class HmrStatusService implements IHmrStatusService {
});
}

public watchHmrStatus(deviceId: string, operationHash: string): void {
this.setData(deviceId, operationHash);
}

@cache()
public attachToHmrStatusEvent(): void {
this.$logParserService.addParseRule({
regex: HmrStatusService.HMR_STATUS_LOG_REGEX,
handler: this.handleHmrStatusFound.bind(this),
name: "hmrStatus"
});
this.$logParserService.addParseRule({
regex: IOS_APP_CRASH_LOG_REG_EXP,
handler: this.handleAppCrash.bind(this),
name: "appCrashHmr",
platform: this.$devicePlatformsConstants.iOS.toLowerCase()
});
}

private handleAppCrash(matches: RegExpMatchArray, deviceId: string): void {
for (const operationId in this.hashOperationStatuses) {
const operation = this.hashOperationStatuses[operationId];
if (operationId.startsWith(deviceId) && !operation.status) {
operation.status = HmrConstants.HMR_ERROR_STATUS;
}
}
}

private handleHmrStatusFound(matches: RegExpMatchArray, deviceId: string): void {
Expand All @@ -65,7 +85,7 @@ export class HmrStatusService implements IHmrStatusService {
this.$logger.trace("Found hmr status.", { status, hash });

if (status) {
this.setData(status, hash, deviceId);
this.setData(deviceId, hash, status);
}
}

Expand All @@ -77,7 +97,7 @@ export class HmrStatusService implements IHmrStatusService {
return null;
}

private setData(status: Number, operationHash: string, deviceId: string): void {
private setData(deviceId: string, operationHash: string, status?: Number): void {
const key = `${deviceId}${operationHash}`;

if (!this.hashOperationStatuses[key]) {
Expand Down
41 changes: 33 additions & 8 deletions lib/services/ios-debugger-port-service.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
import { DEBUGGER_PORT_FOUND_EVENT_NAME, ATTACH_REQUEST_EVENT_NAME } from "../common/constants";
import { DEBUGGER_PORT_FOUND_EVENT_NAME, ATTACH_REQUEST_EVENT_NAME, IOS_APP_CRASH_LOG_REG_EXP } from "../common/constants";
import { cache } from "../common/decorators";
import { APPLICATION_RESPONSE_TIMEOUT_SECONDS } from "../constants";

export class IOSDebuggerPortService implements IIOSDebuggerPortService {
public static DEBUG_PORT_LOG_REGEX = /NativeScript debugger has opened inspector socket on port (\d+?) for (.*)[.]/;
private mapDebuggerPortData: IDictionary<IIOSDebuggerPortStoredData> = {};
private currentAppId: string;

constructor(private $logParserService: ILogParserService,
private $iOSNotification: IiOSNotification,
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
private $logger: ILogger) { }

public getPort(data: IIOSDebuggerPortInputData): Promise<number> {
return new Promise((resolve, reject) => {
public async getPort(data: IIOSDebuggerPortInputData): Promise<number> {
return new Promise<number>((resolve, reject) => {
const key = `${data.deviceId}${data.appId}`;
const retryInterval = 500;
let retryCount = Math.max(APPLICATION_RESPONSE_TIMEOUT_SECONDS * 1000 / retryInterval, 10);

const interval = setInterval(() => {
let port = this.getPortByKey(key);
const port = this.getPortByKey(key);
if (port || retryCount === 0) {
clearInterval(interval);
resolve(port);
} else {
port = this.getPortByKey(key);
retryCount--;
if (this.mapDebuggerPortData[key] && this.mapDebuggerPortData[key].error) {
clearInterval(interval);
reject(this.mapDebuggerPortData[key].error);
} else {
retryCount--;
}
}
}, retryInterval);
});
}

public async attachToDebuggerPortFoundEvent(): Promise<void> {
public async attachToDebuggerPortFoundEvent(appId: string): Promise<void> {
this.currentAppId = appId;
this.attachToDebuggerPortFoundEventCore();
this.attachToAttachRequestEvent();
}
Expand All @@ -43,6 +49,24 @@ export class IOSDebuggerPortService implements IIOSDebuggerPortService {
name: "debugPort",
platform: this.$devicePlatformsConstants.iOS.toLowerCase()
});
this.$logParserService.addParseRule({
regex: IOS_APP_CRASH_LOG_REG_EXP,
handler: this.handleAppCrash.bind(this),
name: "appCrash",
platform: this.$devicePlatformsConstants.iOS.toLowerCase()
});
}

private handleAppCrash(matches: RegExpMatchArray, deviceId: string): void {
const data = {
port: 0,
appId: this.currentAppId,
deviceId,
error: new Error("The application has been terminated.")
};

this.clearTimeout(data);
this.setData(data, { port: data.port, error: data.error });
}

private handlePortFound(matches: RegExpMatchArray, deviceId: string): void {
Expand Down Expand Up @@ -73,7 +97,7 @@ export class IOSDebuggerPortService implements IIOSDebuggerPortService {
}

private getPortByKey(key: string): number {
if (this.mapDebuggerPortData[key]) {
if (this.mapDebuggerPortData[key] && this.mapDebuggerPortData[key].port) {
return this.mapDebuggerPortData[key].port;
}

Expand All @@ -89,6 +113,7 @@ export class IOSDebuggerPortService implements IIOSDebuggerPortService {

this.mapDebuggerPortData[key].port = storedData.port;
this.mapDebuggerPortData[key].timer = storedData.timer;
this.mapDebuggerPortData[key].error = storedData.error;
}

private clearTimeout(data: IIOSDebuggerPortData): void {
Expand Down
9 changes: 7 additions & 2 deletions lib/services/livesync/livesync-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -664,12 +664,17 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi
const service = this.getLiveSyncService(device.deviceInfo.platform);

const watchAction = async (watchInfo: ILiveSyncWatchInfo): Promise<void> => {
const isInHMRMode = liveSyncData.useHotModuleReload && platformHmrData.hash;
if (isInHMRMode) {
this.$hmrStatusService.watchHmrStatus(device.deviceInfo.identifier, platformHmrData.hash);
}

let liveSyncResultInfo = await service.liveSyncWatchAction(device, watchInfo);

await this.refreshApplication(projectData, liveSyncResultInfo, deviceBuildInfoDescriptor.debugOptions, deviceBuildInfoDescriptor.outputPath);

// If didRecover is true, this means we were in ErrorActivity and fallback files were already transfered and app will be restarted.
if (!liveSyncResultInfo.didRecover && liveSyncData.useHotModuleReload && platformHmrData.hash) {
// If didRecover is true, this means we were in ErrorActivity and fallback files were already transferred and app will be restarted.
if (!liveSyncResultInfo.didRecover && isInHMRMode) {
const status = await this.$hmrStatusService.getHmrStatus(device.deviceInfo.identifier, platformHmrData.hash);
if (status === HmrConstants.HMR_ERROR_STATUS) {
watchInfo.filesToSync = platformHmrData.fallbackFiles;
Expand Down
Loading