Skip to content

The agent that grows with you

test(termux): reap the full TUI process tree before cleanup

ethernet3b9c76b88a04
diff --git a/scripts/termux/validate_installed.py b/scripts/termux/validate_installed.py
index f2069e4115d..adeb0cacc92 100644
--- a/scripts/termux/validate_installed.py
+++ b/scripts/termux/validate_installed.py
@@ -2,17 +2,14 @@
from __future__ import annotations
import fcntl
-import importlib
import json
import os
from pathlib import Path
import pty
import re
import select
-import signal
import struct
import subprocess
-import sys
import termios
import tempfile
import time
@@ -26,6 +23,24 @@ def run(argv: list[str], env: dict[str, str], cwd: Path) -> subprocess.Completed
return result
+def stop_child_tree(child: subprocess.Popen) -> None:
+ import psutil
+ from agent.deadline import kill_process_tree
+
+ if child.poll() is not None:
+ return
+ descendants = psutil.Process(child.pid).children(recursive=True)
+ kill_process_tree(child.pid)
+ child.wait(timeout=10)
+ deadline = time.monotonic() + 10
+ while descendants and time.monotonic() < deadline:
+ descendants = [p for p in descendants if p.is_running() and p.status() != psutil.STATUS_ZOMBIE]
+ if descendants:
+ time.sleep(0.05)
+ if descendants:
+ raise RuntimeError(f"TUI children still alive: {[p.pid for p in descendants]}")
+
+
def tui_smoke(launcher: Path, env: dict[str, str], cwd: Path) -> None:
master, slave = pty.openpty()
fcntl.ioctl(slave, termios.TIOCSWINSZ, struct.pack("HHHH", 40, 120, 0, 0))
@@ -57,14 +72,10 @@ def tui_smoke(launcher: Path, env: dict[str, str], cwd: Path) -> None:
raise RuntimeError("TUI never reached its real setup screen:\n" + captured.decode(errors="replace"))
print("TUI_SETUP_SCREEN_OK", flush=True)
finally:
- if child.poll() is None:
- os.killpg(child.pid, signal.SIGTERM)
try:
- child.wait(timeout=10)
- except subprocess.TimeoutExpired:
- os.killpg(child.pid, signal.SIGKILL)
- child.wait(timeout=10)
- os.close(master)
+ stop_child_tree(child)
+ finally:
+ os.close(master)
def main() -> None:
diff --git a/tests/test_termux_launchers.py b/tests/test_termux_launchers.py
index 6938308efbb..d2b4bd3bb5c 100644
--- a/tests/test_termux_launchers.py
+++ b/tests/test_termux_launchers.py
@@ -71,3 +71,33 @@ def test_postinst_refuses_foreign_path_and_prerm_preserves_it(tmp_path):
assert subprocess.run(command, env=env).returncode != 0
subprocess.run([shutil.which("sh"), str(control / "prerm"), "remove"], check=True, env=env)
assert link.read_text(encoding="utf-8") == "foreign launcher"
+
+
+def test_verifier_stops_detached_descendants_before_cleanup(tmp_path):
+ import psutil
+ from scripts.termux.validate_installed import stop_child_tree
+
+ code = (
+ "import subprocess, sys, time\n"
+ "child = subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(120)'], "
+ "start_new_session=True)\n"
+ "print(child.pid, flush=True)\n"
+ "time.sleep(120)\n"
+ )
+ child = subprocess.Popen(
+ [sys.executable, "-c", code], stdout=subprocess.PIPE, text=True,
+ cwd=tmp_path, start_new_session=True,
+ )
+ descendant = psutil.Process(int(child.stdout.readline()))
+ try:
+ stop_child_tree(child)
+ assert child.poll() is not None
+ assert not descendant.is_running() or descendant.status() == psutil.STATUS_ZOMBIE
+ finally:
+ if child.poll() is None:
+ child.kill()
+ if descendant.is_running() and descendant.status() != psutil.STATUS_ZOMBIE:
+ descendant.kill()
+ child.wait(timeout=10)
+ child.stdout.close()