@@ -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 ;
0 commit comments