From 7aa67a2f0e5741a37773095b74cbfceca0f259b4 Mon Sep 17 00:00:00 2001 From: fatme Date: Fri, 19 Oct 2018 16:40:37 +0300 Subject: [PATCH] fix: doesn't await for AppLaunching notification when CLI retries to attach to debugger --- lib/declarations.d.ts | 2 +- lib/definitions/debug.d.ts | 4 +++ .../ios/socket-request-executor.ts | 28 +++++++++++-------- lib/services/ios-debug-service.ts | 2 +- lib/services/livesync/livesync-service.ts | 5 +++- 5 files changed, 26 insertions(+), 15 deletions(-) diff --git a/lib/declarations.d.ts b/lib/declarations.d.ts index ef54b193b8..67ecb3381f 100644 --- a/lib/declarations.d.ts +++ b/lib/declarations.d.ts @@ -709,7 +709,7 @@ interface IiOSNotification extends NodeJS.EventEmitter { } interface IiOSSocketRequestExecutor { - executeLaunchRequest(deviceIdentifier: string, timeout: number, readyForAttachTimeout: number, projectId: string, shouldBreak?: boolean): Promise; + executeLaunchRequest(deviceIdentifier: string, timeout: number, readyForAttachTimeout: number, projectId: string, debugOptions: IDebugOptions): Promise; executeAttachRequest(device: Mobile.IiOSDevice, timeout: number, projectId: string): Promise; } diff --git a/lib/definitions/debug.d.ts b/lib/definitions/debug.d.ts index 034f3403b6..350094fa27 100644 --- a/lib/definitions/debug.d.ts +++ b/lib/definitions/debug.d.ts @@ -95,6 +95,10 @@ interface IDebugOptions { * The sdk version of the emulator. */ sdk?: string; + /** + * Defines if the handshake(AppLaunching notification) between CLI and runtime should be executed. The handshake is not needed when CLI retries to attach to the debugger. + */ + skipHandshake?: boolean; } /** diff --git a/lib/device-sockets/ios/socket-request-executor.ts b/lib/device-sockets/ios/socket-request-executor.ts index 88bb8e5bc4..76db790060 100644 --- a/lib/device-sockets/ios/socket-request-executor.ts +++ b/lib/device-sockets/ios/socket-request-executor.ts @@ -48,24 +48,19 @@ export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor { } } - public async executeLaunchRequest(deviceIdentifier: string, timeout: number, readyForAttachTimeout: number, projectId: string, shouldBreak?: boolean): Promise { + public async executeLaunchRequest(deviceIdentifier: string, timeout: number, readyForAttachTimeout: number, projectId: string, debugOptions: IDebugOptions): Promise { try { - const appLaunchingSocket = await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getAppLaunching(projectId), constants.IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE); - await this.$iOSNotificationService.awaitNotification(deviceIdentifier, +appLaunchingSocket, timeout); + if (!debugOptions.skipHandshake) { + await this.executeHandshake(deviceIdentifier, projectId, timeout); + } - if (shouldBreak) { + if (debugOptions.debugBrk) { await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getWaitForDebug(projectId)); } - // We need to send the ObserveNotification ReadyForAttach before we post the AttachRequest. - const readyForAttachSocket = await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getReadyForAttach(projectId), constants.IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE); - const readyForAttachPromise = this.$iOSNotificationService.awaitNotification(deviceIdentifier, +readyForAttachSocket, readyForAttachTimeout); - - await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getAttachRequest(projectId, deviceIdentifier)); - await readyForAttachPromise; + await this.executeAttachAvailable(deviceIdentifier, projectId, readyForAttachTimeout); } catch (e) { - this.$logger.trace("Launch request error:"); - this.$logger.trace(e); + this.$logger.trace("Launch request error: ", e); this.$errors.failWithoutHelp("Error while waiting for response from NativeScript runtime."); } } @@ -79,9 +74,18 @@ export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor { await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getAttachRequest(projectId, deviceIdentifier)); await readyForAttachPromise; } catch (e) { + this.$logger.trace("Attach available error: ", e); this.$errors.failWithoutHelp(`The application ${projectId} timed out when performing the socket handshake.`); } } + + private async executeHandshake(deviceIdentifier: string, projectId: string, timeout: number): Promise { + // This notification will be send only once by the runtime during application start. + // In case app is already running, we'll fail here as we'll not receive it. + const appLaunchingNotification = this.$iOSNotification.getAppLaunching(projectId); + const appLaunchingSocket = await this.$iOSNotificationService.postNotification(deviceIdentifier, appLaunchingNotification, constants.IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE); + await this.$iOSNotificationService.awaitNotification(deviceIdentifier, +appLaunchingSocket, timeout); + } } $injector.register("iOSSocketRequestExecutor", IOSSocketRequestExecutor); diff --git a/lib/services/ios-debug-service.ts b/lib/services/ios-debug-service.ts index 94648ecc54..2fa6749ad6 100644 --- a/lib/services/ios-debug-service.ts +++ b/lib/services/ios-debug-service.ts @@ -182,7 +182,7 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS } private async debugBrkCore(device: Mobile.IiOSDevice, debugData: IDebugData, debugOptions: IDebugOptions): Promise { - await this.$iOSSocketRequestExecutor.executeLaunchRequest(device.deviceInfo.identifier, AWAIT_NOTIFICATION_TIMEOUT_SECONDS, AWAIT_NOTIFICATION_TIMEOUT_SECONDS, debugData.applicationIdentifier, debugOptions.debugBrk); + await this.$iOSSocketRequestExecutor.executeLaunchRequest(device.deviceInfo.identifier, AWAIT_NOTIFICATION_TIMEOUT_SECONDS, AWAIT_NOTIFICATION_TIMEOUT_SECONDS, debugData.applicationIdentifier, debugOptions); return this.wireDebuggerClient(debugData, debugOptions, device); } diff --git a/lib/services/livesync/livesync-service.ts b/lib/services/livesync/livesync-service.ts index ce73c52f33..6d903f397f 100644 --- a/lib/services/livesync/livesync-service.ts +++ b/lib/services/livesync/livesync-service.ts @@ -224,7 +224,9 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi }; debugData.pathToAppPackage = this.$platformService.lastOutputPath(settings.platform, buildConfig, projectData, settings.outputPath); - return this.printDebugInformation(await this.$debugService.debug(debugData, settings.debugOptions)); + const debugInfo = await this.$debugService.debug(debugData, settings.debugOptions); + const result = this.printDebugInformation(debugInfo); + return result; } public printDebugInformation(debugInformation: IDebugInformation): IDebugInformation { @@ -270,6 +272,7 @@ export class LiveSyncService extends EventEmitter implements IDebugLiveSyncServi } catch (err) { this.$logger.trace("Couldn't attach debugger, will modify options and try again.", err); attachDebuggerOptions.debugOptions.start = false; + attachDebuggerOptions.debugOptions.skipHandshake = true; try { debugInformation = await this.attachDebugger(attachDebuggerOptions); } catch (innerErr) {