@@ -656,19 +656,46 @@ def _exercise_gadget(
656656
657657
658658def _cleanup (config : AndroidSmokeConfig , serial : str ) -> None :
659- remote_server = f"{ REMOTE_DIR } /{ config .name } -server"
660- remote_loader = f"{ REMOTE_DIR } /gadget-loader"
661- remote_processes = (remote_server , remote_loader )
659+ processes = (
660+ (f"{ config .name } -server" , f"{ config .name } -server" [:15 ]),
661+ ("gadget-loader" , "gadget-loader" ),
662+ )
662663 failures : list [str ] = []
664+ signaled = False
663665
664- for remote_process in remote_processes :
666+ for label , process_comm in processes :
665667 result = root_shell (
666668 serial ,
667- f"pkill -9 -f '^ { remote_process } ([[:space:]]|$)' " ,
669+ f"pkill -TERM -x { process_comm } " ,
668670 check = False ,
669671 )
672+ signaled = signaled or result .returncode == 0
670673 if result .returncode not in (0 , 1 ):
671- failures .append (f"stop { Path (remote_process ).name } (exit { result .returncode } )" )
674+ failures .append (f"stop { label } (exit { result .returncode } )" )
675+
676+ if signaled :
677+ time .sleep (0.5 )
678+
679+ forced : list [tuple [str , str ]] = []
680+ for label , process_comm in processes :
681+ result = root_shell (serial , f"pgrep -x { process_comm } " , check = False )
682+ if result .returncode == 0 :
683+ forced .append ((label , process_comm ))
684+ kill_result = root_shell (serial , f"pkill -KILL -x { process_comm } " , check = False )
685+ if kill_result .returncode not in (0 , 1 ):
686+ failures .append (f"force-stop { label } (exit { kill_result .returncode } )" )
687+ elif result .returncode != 1 :
688+ failures .append (f"verify { label } (exit { result .returncode } )" )
689+
690+ if forced :
691+ failures .append ("processes did not stop gracefully: " + ", " .join (x [0 ] for x in forced ))
692+ time .sleep (0.2 )
693+ for label , process_comm in forced :
694+ result = root_shell (serial , f"pgrep -x { process_comm } " , check = False )
695+ if result .returncode == 0 :
696+ failures .append (f"process remains: { label } " )
697+ elif result .returncode != 1 :
698+ failures .append (f"verify { label } after SIGKILL (exit { result .returncode } )" )
672699
673700 remove_result = root_shell (serial , f"rm -rf -- { REMOTE_DIR } " , check = False )
674701 if remove_result .returncode != 0 :
@@ -677,21 +704,23 @@ def _cleanup(config: AndroidSmokeConfig, serial: str) -> None:
677704 for port in (config .port , choose_gadget_port (config .port )):
678705 adb (serial , "forward" , "--remove" , f"tcp:{ port } " , check = False )
679706
680- for remote_process in remote_processes :
681- result = root_shell (
682- serial ,
683- f"pgrep -f '^{ remote_process } ([[:space:]]|$)'" ,
684- check = False ,
685- )
686- if result .returncode == 0 :
687- failures .append (f"process remains: { Path (remote_process ).name } " )
688- elif result .returncode != 1 :
689- failures .append (f"verify { Path (remote_process ).name } (exit { result .returncode } )" )
690-
691707 directory_result = root_shell (serial , f"test ! -e { REMOTE_DIR } " , check = False )
692708 if directory_result .returncode != 0 :
693709 failures .append ("remote test directory remains" )
694710
711+ socket_result = root_shell (serial , "cat /proc/net/unix" , check = False )
712+ if socket_result .returncode != 0 :
713+ failures .append (f"list unix sockets (exit { socket_result .returncode } )" )
714+ else :
715+ socket_markers = (
716+ f"@{ config .name } -server-" ,
717+ f"@{ config .name } -gadget-" ,
718+ f"@/{ config .name } -zymbiote-" ,
719+ )
720+ for marker in socket_markers :
721+ if marker in socket_result .stdout :
722+ failures .append (f"unix socket remains: { marker } " )
723+
695724 forward_result = adb (serial , "forward" , "--list" , check = False )
696725 if forward_result .returncode != 0 :
697726 failures .append (f"list adb forwards (exit { forward_result .returncode } )" )
0 commit comments