From 04d07b2c624a06503b3f1ae6708fba27c0bddec3 Mon Sep 17 00:00:00 2001 From: butubb <1422726308@qq.com> Date: Fri, 4 Sep 2026 16:15:54 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20uiautodev=20=E6=AE=8B=E7=95=99=E8=BF=9B?= =?UTF-8?q?=E7=A8=8B=E6=B8=85=E7=90=86=E9=98=B2=E8=AF=AF=E6=9D=80=E2=80=94?= =?UTF-8?q?=E2=80=94=E5=AE=B9=E5=99=A8=E9=87=8D=E5=90=AF=E5=90=8E=20PID=20?= =?UTF-8?q?namespace=20=E9=87=8D=E5=BB=BA=E3=80=81=E6=AE=8B=E7=95=99=20uia?= =?UTF-8?q?uto.pid=20=E7=9A=84=20PID=20=E5=80=BC=E4=BC=9A=E8=A2=AB=20MCP/?= =?UTF-8?q?=E5=85=B6=E5=AE=83=E8=BF=9B=E7=A8=8B=E5=A4=8D=E7=94=A8=EF=BC=8C?= =?UTF-8?q?=E5=8E=9F=E9=80=BB=E8=BE=91=E7=9B=B4=E6=8E=A5=20os.kill=20?= =?UTF-8?q?=E5=8F=AF=E8=83=BD=20SIGTERM=20=E6=9D=80=E6=8E=89=E5=90=8C?= =?UTF-8?q?=E5=AE=B9=E5=99=A8=E7=9A=84=20MCP=20server=EF=BC=88=E8=A7=82?= =?UTF-8?q?=E5=AF=9F=E5=88=B0=20web=20=E5=AE=B9=E5=99=A8=2012s=20=E5=B4=A9?= =?UTF-8?q?=E6=BA=83=E5=BE=AA=E7=8E=AF=EF=BC=89=EF=BC=9B=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=20kill=20=E5=89=8D=E6=A0=A1=E9=AA=8C=20/proc//cmdline=20?= =?UTF-8?q?=E5=90=AB=20uiautodev?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web_server.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/web_server.py b/web_server.py index f4c726c..d1892d8 100644 --- a/web_server.py +++ b/web_server.py @@ -84,14 +84,30 @@ _UIAUTO_PID_FILE = os.path.join( _uiauto_proc = None +def _pid_cmdline_has(pid, keyword): + """校验 PID 对应的进程命令行含 keyword(容器重启后 PID 复用会误杀, + 杀前必须确认目标真是残留的 uiautodev,而不是 MCP/web 等其它进程)。""" + try: + with open(f"/proc/{pid}/cmdline", "rb") as f: + cmd = f.read().replace(b"\x00", b" ").decode(errors="replace") + return keyword in cmd + except Exception: + return False + + def _kill_stale_uiauto(): - """清理上次 web_server 残留的 uiautodev 进程(读取 PID 文件)。""" + """清理上次 web_server 残留的 uiautodev 进程(读取 PID 文件)。 + + 容器重启后 PID namespace 重建、PID 会被复用(MCP/其它进程可能拿到与 + uiauto.pid 相同的值),因此 kill 前先校验命令行确为 uiautodev,防止 + 误杀同容器内的 MCP server 等进程导致连锁崩溃。 + """ if not os.path.exists(_UIAUTO_PID_FILE): return try: with open(_UIAUTO_PID_FILE) as f: pid = int(f.read().strip()) - if pid > 0 and pid != os.getpid(): + if pid > 0 and pid != os.getpid() and _pid_cmdline_has(pid, "uiautodev"): try: os.kill(pid, 0) # 探测进程是否存活 os.kill(pid, 15) # 终止残留