Skip to content

Commit 3a490bf

Browse files
committed
fix: terminate Android smoke daemons cleanly
1 parent 05a618d commit 3a490bf

2 files changed

Lines changed: 95 additions & 17 deletions

File tree

scripts/android_smoke.py

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -656,19 +656,46 @@ def _exercise_gadget(
656656

657657

658658
def _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})")

tests/test_android_smoke.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,8 @@ def fake_adb(_serial: str, *arguments: str, **_kwargs: object) -> SimpleNamespac
561561

562562
android_smoke._cleanup(config, "SERIAL-1")
563563

564+
assert "pkill -TERM -x oemcodec-server" in root_commands
565+
assert "pkill -TERM -x gadget-loader" in root_commands
564566
assert "rm -rf -- /data/local/tmp/phantom-frida-test" in root_commands
565567
assert ("forward", "--list") in adb_commands
566568

@@ -625,3 +627,50 @@ def fake_adb(_serial: str, *arguments: str, **_kwargs: object) -> SimpleNamespac
625627

626628
with pytest.raises(android_smoke.SmokeFailure, match="forward remains"):
627629
android_smoke._cleanup(config, "SERIAL-1")
630+
631+
632+
def test_cleanup_rejects_lingering_runtime_socket(monkeypatch: pytest.MonkeyPatch) -> None:
633+
def fake_root_shell(_serial: str, command: str, **_kwargs: object) -> SimpleNamespace:
634+
stdout = "@/oemcodec-zymbiote-deadbeef\n" if command == "cat /proc/net/unix" else ""
635+
return SimpleNamespace(
636+
returncode=1 if command.startswith(("pkill", "pgrep")) else 0,
637+
stdout=stdout,
638+
stderr="",
639+
)
640+
641+
monkeypatch.setattr(android_smoke, "root_shell", fake_root_shell)
642+
monkeypatch.setattr(
643+
android_smoke,
644+
"adb",
645+
lambda *_args, **_kwargs: SimpleNamespace(returncode=0, stdout="", stderr=""),
646+
)
647+
config = SimpleNamespace(name="oemcodec", port=27142)
648+
649+
with pytest.raises(android_smoke.SmokeFailure, match="unix socket remains"):
650+
android_smoke._cleanup(config, "SERIAL-1")
651+
652+
653+
def test_cleanup_uses_kernel_comm_limit_for_long_server_name(
654+
monkeypatch: pytest.MonkeyPatch,
655+
) -> None:
656+
root_commands: list[str] = []
657+
658+
def fake_root_shell(_serial: str, command: str, **_kwargs: object) -> SimpleNamespace:
659+
root_commands.append(command)
660+
return SimpleNamespace(
661+
returncode=1 if command.startswith(("pkill", "pgrep")) else 0,
662+
stdout="",
663+
stderr="",
664+
)
665+
666+
monkeypatch.setattr(android_smoke, "root_shell", fake_root_shell)
667+
monkeypatch.setattr(
668+
android_smoke,
669+
"adb",
670+
lambda *_args, **_kwargs: SimpleNamespace(returncode=0, stdout="", stderr=""),
671+
)
672+
config = SimpleNamespace(name="abcdefghijklmnopqrst", port=27142)
673+
674+
android_smoke._cleanup(config, "SERIAL-1")
675+
676+
assert "pkill -TERM -x abcdefghijklmno" in root_commands

0 commit comments

Comments
 (0)