@@ -375,31 +375,42 @@ async def start_bidi_session(self) -> None:
375
375
for _ in range (1 , 30 ):
376
376
try :
377
377
# we must keep this socket open throughout the lifetime of that session
378
- reader , self .writer_marionette = await asyncio .open_connection ("127.0.0.1" , marionette_port )
378
+ self . reader_marionette , self .writer_marionette = await asyncio .open_connection ("127.0.0.1" , marionette_port )
379
379
break
380
380
except ConnectionRefusedError as e :
381
381
log_proto .debug ("waiting for firefox marionette: %s" , e )
382
382
await asyncio .sleep (1 )
383
383
else :
384
384
raise WebdriverError ("could not connect to firefox marionette" )
385
385
386
- reply = await reader .read (1024 )
386
+ reply = await self . reader_marionette .read (1024 )
387
387
if b'"marionetteProtocol":3' not in reply :
388
388
raise WebdriverError (f"unexpected marionette reply: { reply .decode ()} " )
389
389
cmd = '[0,1,"WebDriver:NewSession",{"webSocketUrl":true,"acceptInsecureCerts":true}]'
390
390
self .writer_marionette .write (f"{ len (cmd )} :{ cmd } " .encode ())
391
391
await self .writer_marionette .drain ()
392
- reply = await reader .read (1024 )
392
+ reply = await self . reader_marionette .read (1024 )
393
393
# cut off length prefix
394
394
reply = json .loads (reply [reply .index (b":" ) + 1 :].decode ())
395
395
if not isinstance (reply , list ) or len (reply ) != 4 or not isinstance (reply [3 ], dict ):
396
396
raise WebdriverError (f"unexpected marionette session request reply: { reply !r} " )
397
397
log_proto .debug ("marionette session request reply: %s" , reply )
398
398
399
+ # continue to read from Marionette, in case there are any messages; we must drain the pipe
400
+ self .marionette_logger : asyncio .Task = asyncio .create_task (self .log_marionette (), name = "marionette_logger" )
401
+
399
402
url = reply [3 ]["capabilities" ]["webSocketUrl" ]
400
403
self .bidi_session = BidiSession (session_url = url , ws_url = url , process = driver )
401
404
log_proto .debug ("Established firefox session %r" , self .bidi_session )
402
405
406
+ async def log_marionette (self ) -> None :
407
+ # we don't expect messages here, so log them as warnings
408
+ while True :
409
+ msg = await self .reader_marionette .read ()
410
+ log_proto .warning ("marionette: %s" , msg )
411
+
403
412
async def close_bidi_session (self ) -> None :
413
+ self .marionette_logger .cancel ()
414
+ del self .marionette_logger
404
415
self .writer_marionette .close ()
405
416
await self .writer_marionette .wait_closed ()
0 commit comments