1
0
mirror of https://github.com/ARM-software/devlib.git synced 2025-01-31 02:00:45 +00:00

android: Add a retry logic for background command PID detection

PID detection can sometimes fail for unknown reason. Maybe there is a
latency between the process being started and "ps" being able to see it
that can sometimes be high enough that we look for the process before
it's exposed.

In order to remedy that, add a retry logic to avoid plain failures.
This commit is contained in:
Douglas Raillard 2023-07-05 11:07:17 +01:00 committed by Marc Bonnici
parent 900531b417
commit 0579a814f1

View File

@ -657,6 +657,7 @@ def adb_background_shell(conn, command,
busybox = conn.busybox busybox = conn.busybox
_check_env() _check_env()
orig_command = command
stdout, stderr, command = redirect_streams(stdout, stderr, command) stdout, stderr, command = redirect_streams(stdout, stderr, command)
if as_root: if as_root:
command = f'{busybox} printf "%s" {quote(command)} | su' command = f'{busybox} printf "%s" {quote(command)} | su'
@ -693,10 +694,25 @@ def adb_background_shell(conn, command,
# avoids having to rely on PID ordering (which could be misleading if PIDs # avoids having to rely on PID ordering (which could be misleading if PIDs
# got recycled). # got recycled).
find_pid = f'''pids=$({busybox} ps -A -o pid,args | {grep_cmd} | {busybox} grep -v {quote(grep_cmd)} | {busybox} awk '{{print $1}}') && {busybox} printf "%s" "$pids" && {busybox} kill -CONT $pids''' find_pid = f'''pids=$({busybox} ps -A -o pid,args | {grep_cmd} | {busybox} grep -v {quote(grep_cmd)} | {busybox} awk '{{print $1}}') && {busybox} printf "%s" "$pids" && {busybox} kill -CONT $pids'''
excep = None
for _ in range(5):
try:
pids = conn.execute(find_pid, as_root=as_root) pids = conn.execute(find_pid, as_root=as_root)
# We choose the highest PID as the "control" PID. It actually does not # We choose the highest PID as the "control" PID. It actually does not
# really matter which one we pick, as they are all equivalent sh -c layers. # really matter which one we pick, as they are all equivalent sh -c layers.
pid = max(map(int, pids.split())) pid = max(map(int, pids.split()))
except TargetStableError:
raise
except Exception as e:
excep = e
time.sleep(10e-3)
continue
else:
break
else:
raise TargetTransientError(f'Could not detect PID of background command: {orig_command}') from excep
return (p, pid) return (p, pid)
def adb_kill_server(timeout=30, adb_server=None): def adb_kill_server(timeout=30, adb_server=None):