修安装包卸载卡死的真凶:attach_console 在无控制台时阻塞

排查过程(一步步排除,记录下来免得以后重踩):

  1. 现象:从控制面板卸载,进度条永远走不完。
  2. 先怀疑 input():安装器用 runhidden 调 --uninstall,没有 stdin,
     input() 会一直等。加了 isatty 保护 —— **没解决**。
  3. 再怀疑 Popen 没重定向 stdio:_schedule_self_delete 起的 cmd
     继承了父进程句柄,capture_output 会等管道 EOF。加了
     DEVNULL 重定向 —— **还是卡**。
  4. 又发现 _kill_running 用 taskkill /IM ykxmz.exe 把正在卸载的
     自己杀了(--uninstall 就跑在 ykxmz.exe 里),
     导致后面的"删自启/删数据"没执行。改成 /FI "PID ne self" ——
     自启能删了,**卸载仍然卡**。
  5. 最后看进程状态才定位:卡住的 ykxmz.exe 只有 **1 个线程、
     0.16 秒 CPU、19MB 内存** —— 明显停在启动早期,根本没干活。
     那就是 cli_uninstall 第一行的 attach_console():
     它对 --windowed 程序调 AllocConsole(),再 open("CONIN$")。
     在没有控制台的隐藏进程里,这一步**永久阻塞**。

修法:加 --quiet。安装包的卸载器用
      ykxmz.exe --uninstall --keep-dir --quiet
      --quiet 下完全不碰控制台(不 AllocConsole、不 input),
      结果写进 uninstall.log 供排查。
      实测:不带 --quiet 无限卡住;带了 **0.6 秒返回**。

顺带修的两处(也是实测发现的):
  * 安装器关不掉以管理员身份运行的旧实例 -> Inno 弹"无法关闭所有
    应用程序",静默下被压成"终止",**整个安装回滚**。
    改成 PrepareToInstall 里自己 taskkill /F。
  * 测试脚本把注册表路径写成 HKCU\...(少了冒号),
    PowerShell 静默返回空,"开机自启"永远误报失败。

最终实测(test_installer_cycle.py,20 项):
    静默安装   退出码 0,7.1 秒
    配对码     正确写入 C:\ProgramData\ykxmz\data\settings.json
    进程/端口  在跑,8765 监听
    开机自启   已设置
    静默卸载   1.2 秒
    卸载清理   安装目录 / 数据目录 / 注册表 / 进程 / 自启 全部干净
    20 项全通过

套件新增一个阶段:「安装包完整周期」。
This commit is contained in:
xmz7891
2026-10-07 02:21:36 +08:00
parent 1e73a789d1
commit c7c0484a27
5 changed files with 211 additions and 17 deletions
+126
View File
@@ -0,0 +1,126 @@
# -*- coding: utf-8 -*-
r'''把安装包完整周期测试加进套件,并提交这一轮的关键修复。'''
from __future__ import annotations
import os
import subprocess
import sys
from pathlib import Path
ROOT = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote")
VERIFY = ROOT / "_tests" / "verify_all.ps1"
GIT = r"C:\Program Files\Git\bin\git.exe"
BOM = b"\xef\xbb\xbf"
STAGE = '''
# ---------------------------------------------------------------- 3b5
Banner "3b5/7 安装包完整周期(装 -> 验 -> 卸 -> 再验)"
# 这一步真的装一遍、再卸一遍。踩过的四个卡死 bug 全靠它才发现:
# input() 无 stdin 阻塞 / Popen 不重定向 stdio /
# taskkill 把自己杀了 / 关不掉管理员权限的旧实例导致安装回滚。
# 注意:需要管理员权限,且会真的往 Program Files 装东西。
$stageOut = & python (Join-Path $root "_build\\test_installer_cycle.py") 2>&1
Show-Fails $stageOut
Record "安装包完整周期" ($LASTEXITCODE -eq 0)
'''
MSG = """修安装包卸载卡死的真凶:attach_console 在无控制台时阻塞
排查过程(一步步排除,记录下来免得以后重踩):
1. 现象:从控制面板卸载,进度条永远走不完。
2. 先怀疑 input():安装器用 runhidden 调 --uninstall,没有 stdin,
input() 会一直等。加了 isatty 保护 —— **没解决**。
3. 再怀疑 Popen 没重定向 stdio:_schedule_self_delete 起的 cmd
继承了父进程句柄,capture_output 会等管道 EOF。加了
DEVNULL 重定向 —— **还是卡**。
4. 又发现 _kill_running 用 taskkill /IM ykxmz.exe 把正在卸载的
自己杀了(--uninstall 就跑在 ykxmz.exe 里),
导致后面的"删自启/删数据"没执行。改成 /FI "PID ne self" ——
自启能删了,**卸载仍然卡**。
5. 最后看进程状态才定位:卡住的 ykxmz.exe 只有 **1 个线程、
0.16 秒 CPU、19MB 内存** —— 明显停在启动早期,根本没干活。
那就是 cli_uninstall 第一行的 attach_console():
它对 --windowed 程序调 AllocConsole(),再 open("CONIN$")。
在没有控制台的隐藏进程里,这一步**永久阻塞**。
修法:加 --quiet。安装包的卸载器用
ykxmz.exe --uninstall --keep-dir --quiet
--quiet 下完全不碰控制台(不 AllocConsole、不 input),
结果写进 uninstall.log 供排查。
实测:不带 --quiet 无限卡住;带了 **0.6 秒返回**。
顺带修的两处(也是实测发现的):
* 安装器关不掉以管理员身份运行的旧实例 -> Inno 弹"无法关闭所有
应用程序",静默下被压成"终止",**整个安装回滚**。
改成 PrepareToInstall 里自己 taskkill /F。
* 测试脚本把注册表路径写成 HKCU\\...(少了冒号),
PowerShell 静默返回空,"开机自启"永远误报失败。
最终实测(test_installer_cycle.py,20 项):
静默安装 退出码 0,7.1 秒
配对码 正确写入 C:\\ProgramData\\ykxmz\\data\\settings.json
进程/端口 在跑,8765 监听
开机自启 已设置
静默卸载 1.2 秒
卸载清理 安装目录 / 数据目录 / 注册表 / 进程 / 自启 全部干净
20 项全通过
套件新增一个阶段:「安装包完整周期」。
"""
def add_stage() -> None:
raw = VERIFY.read_bytes()
text = raw.decode("utf-8-sig")
if "test_installer_cycle.py" in text:
print(" 套件里已有周期测试阶段")
return
anchor = "# ---------------------------------------------------------------- 3c"
if anchor not in text:
anchor = "# ---------------------------------------------------------------- 4"
if anchor not in text:
print(" [失败] 找不到锚点")
return
text = text.replace(anchor, STAGE.lstrip("\n") + anchor, 1)
VERIFY.write_bytes(BOM + text.replace("\r\n", "\n").encode("utf-8"))
print(" 已加「安装包完整周期」阶段")
def commit() -> None:
print()
subprocess.run([GIT, "add", "-A"], cwd=str(ROOT), check=False)
msg_file = ROOT / "_build" / "_cmsg4.txt"
msg_file.write_bytes(MSG.encode("utf-8"))
r = subprocess.run([GIT, "commit", "-q", "-F", str(msg_file)],
cwd=str(ROOT), capture_output=True, text=True,
encoding="utf-8", errors="replace")
msg_file.unlink(missing_ok=True)
print(" commit exit=%s" % r.returncode)
show = subprocess.run([GIT, "log", "--oneline", "-1"], cwd=str(ROOT),
capture_output=True, text=True, encoding="utf-8",
errors="replace")
print(" " + (show.stdout or "").strip())
env = dict(os.environ)
env["GIT_SSH_COMMAND"] = r'"C:\Program Files\Git\usr\bin\ssh.exe"'
push = subprocess.run([GIT, "push", "origin", "main"], cwd=str(ROOT),
capture_output=True, text=True, encoding="utf-8",
errors="replace", env=env)
for line in (push.stderr or push.stdout or "").strip().splitlines()[-2:]:
print(" " + line)
status = subprocess.run([GIT, "status", "--short"], cwd=str(ROOT),
capture_output=True, text=True, encoding="utf-8",
errors="replace")
print(" 工作区: %s" % ("干净 ✓" if not (status.stdout or "").strip()
else (status.stdout or "").strip()[:160]))
def main() -> int:
add_stage()
commit()
return 0
if __name__ == "__main__":
sys.exit(main())
+4 -1
View File
@@ -31,7 +31,10 @@ DATA = Path(r"C:\ProgramData\ykxmz")
EXE = APP / "ykxmz.exe"
REG = (r"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
r"\{8F3C1D42-7A5E-4B91-9C6D-2E8F4A1B7C30}_is1")
RUN_KEY = r"HKCU\Software\Microsoft\Windows\CurrentVersion\Run"
# ⚠ PowerShell 的注册表 provider 路径**必须带冒号**(HKCU:\...)。
# 写成 HKCU\... 不是合法路径,Get-ItemProperty 静默返回空,
# 于是"开机自启"这一项永远误报失败。(这个坑我自己踩了一次。)
RUN_KEY = r"HKCU:\Software\Microsoft\Windows\CurrentVersion\Run"
CODE = "517284"
passed = failed = 0