@@ -144,23 +144,14 @@ async def _inner_send(
144144 self ._object , method , augmented_params , timeout
145145 )
146146 try :
147- done , _ = await asyncio .wait (
148- {
149- self ._connection ._transport .on_error_future ,
150- callback .future ,
151- },
152- return_when = asyncio .FIRST_COMPLETED ,
153- )
147+ result = await callback .future
154148 except asyncio .CancelledError as exc :
155149 await self ._connection ._abort (
156150 self ._object ,
157151 callback ,
158152 str (exc ) or "Task was cancelled" ,
159153 )
160154 raise
161- if not callback .future .done ():
162- callback .future .cancel ()
163- result = next (iter (done )).result ()
164155 # Protocol now has named return values, assume result is one level deeper unless
165156 # there is explicit ambiguity.
166157 if not result :
@@ -351,9 +342,20 @@ async def init() -> None:
351342 if not self .playwright_future .done ():
352343 self .playwright_future .set_exception (exc )
353344
354- await self ._transport .connect ()
355- self ._init_task = self ._loop .create_task (init ())
356- await self ._transport .run ()
345+ try :
346+ await self ._transport .connect ()
347+ self ._init_task = self ._loop .create_task (init ())
348+ await self ._transport .run ()
349+ finally :
350+ cause = None
351+ if (
352+ self ._transport .on_error_future .done ()
353+ and not self ._transport .on_error_future .cancelled ()
354+ ):
355+ transport_exc = self ._transport .on_error_future .exception ()
356+ if transport_exc is not None :
357+ cause = str (transport_exc )
358+ self .cleanup (cause )
357359
358360 def stop_sync (self ) -> None :
359361 self ._transport .request_stop ()
@@ -367,6 +369,8 @@ async def stop_async(self) -> None:
367369 self .cleanup ()
368370
369371 def cleanup (self , cause : str = None ) -> None :
372+ if self ._closed_error :
373+ return
370374 self ._closed_error = TargetClosedError (cause ) if cause else TargetClosedError ()
371375 if self ._init_task and not self ._init_task .done ():
372376 self ._init_task .cancel ()
@@ -463,19 +467,12 @@ async def _abort(
463467 except (Error , OSError ):
464468 pass
465469 try :
466- done , _ = await asyncio .wait (
467- {
468- self ._transport .on_error_future ,
469- callback .future ,
470- },
471- return_when = asyncio .FIRST_COMPLETED ,
472- )
470+ await callback .future
471+ except (Exception , asyncio .CancelledError ):
472+ pass
473473 finally :
474474 if not callback .future .done ():
475475 callback .future .cancel ()
476- for future in done :
477- if not future .cancelled ():
478- future .exception ()
479476
480477 def dispatch (self , msg : ParsedMessagePayload ) -> None :
481478 if self ._closed_error :
0 commit comments