@@ -656,9 +656,9 @@ async def start(self=None) -> Browser:
656656 await asyncio .create_subprocess_exec (
657657 exe ,
658658 * params ,
659- stdin = asyncio .subprocess .PIPE ,
660- stdout = asyncio .subprocess .PIPE ,
661- stderr = asyncio .subprocess .PIPE ,
659+ stdin = asyncio .subprocess .DEVNULL ,
660+ stdout = asyncio .subprocess .DEVNULL ,
661+ stderr = asyncio .subprocess .DEVNULL ,
662662 close_fds = is_posix ,
663663 )
664664 )
@@ -934,7 +934,22 @@ def stop(self, deconstruct=False):
934934 close_success = False
935935 try :
936936 if self .connection :
937- loop = asyncio .get_running_loop ()
937+ loop = None
938+ for obj in (self , self .connection , getattr (
939+ self .connection , "websocket" , None )
940+ ):
941+ if hasattr (obj , "loop" ):
942+ loop = obj .loop
943+ break
944+ if hasattr (obj , "_loop" ):
945+ loop = obj ._loop
946+ break
947+ if not loop :
948+ with suppress (Exception ):
949+ loop = asyncio .get_event_loop_policy ().get_event_loop ()
950+ if not loop :
951+ with suppress (Exception ):
952+ loop = asyncio .get_event_loop ()
938953 if loop .is_running ():
939954 loop .create_task (self .connection .aclose ())
940955 logger .debug ("Closed connection with create_task()" )
@@ -952,8 +967,9 @@ def stop(self, deconstruct=False):
952967 for _ in range (3 ):
953968 try :
954969 if connection_id not in sb_config ._closed_connection_ids :
970+ the_proc = psutil .Process (self ._process .pid )
955971 self ._process .terminate ()
956- procs .append (psutil . Process ( self . _process . pid ) )
972+ procs .append (the_proc )
957973 logger .debug (
958974 "Terminated browser with pid %d successfully."
959975 % self ._process .pid
@@ -964,8 +980,9 @@ def stop(self, deconstruct=False):
964980 break
965981 except (Exception ,):
966982 try :
983+ the_proc = psutil .Process (self ._process .pid )
967984 self ._process .kill ()
968- procs .append (psutil . Process ( self . _process . pid ) )
985+ procs .append (the_proc )
969986 logger .debug (
970987 "Killed browser with pid %d successfully."
971988 % self ._process .pid
@@ -1016,6 +1033,27 @@ def stop(self, deconstruct=False):
10161033 logger .debug ("Process has been terminated: %d." % p .pid )
10171034 for p in alive :
10181035 logger .debug ("Process is still alive: %d." % p .pid )
1036+ if "loop" in locals () and loop and not loop .is_closed ():
1037+ with suppress (Exception ):
1038+ if not loop .is_running ():
1039+ # Time to flush websocket closes and child watcher signals
1040+ loop .run_until_complete (asyncio .sleep (0.05 ))
1041+ # Cancel lingering tasks
1042+ pending = [
1043+ t for t in asyncio .all_tasks (loop ) if not t .done ()
1044+ ]
1045+ for task in pending :
1046+ task .cancel ()
1047+ if pending :
1048+ loop .run_until_complete (
1049+ asyncio .gather (* pending , return_exceptions = True )
1050+ )
1051+ # Shutdown and close
1052+ loop .run_until_complete (loop .shutdown_asyncgens ())
1053+ loop .close ()
1054+ # Clear the closed loop from the thread policy
1055+ # so future tests get a fresh loop
1056+ asyncio .set_event_loop (asyncio .new_event_loop ())
10191057 if self .config .user_data_dir and not self .config .uses_custom_data_dir :
10201058 for _ in range (3 ):
10211059 try :
0 commit comments