fix(#85): host 自己同步杀 Electron 消除 restart 时序 crash dialog

根因:unpacked helper 分支流程
  1. host spawn helper (detached)
  2. host 250ms 后 process.exit(0)
  3. Electron main 收到 host close event → 跑**内存里的旧** close 回调
  4. 旧回调含 'else this.fail(stopped)' → crash dialog 弹
  5. helper awake (sleep 400) → kill Electron
  → T=10ms dialog 已弹,T=400ms+ 才杀 Electron,时序竞争 host 输

即使 main.js 磁盘已 patched(#83+#84 silent),Electron **启动时** 加载的内存
版本是旧的,host close 事件触发的是**内存里的旧回调**,仍 fail(stopped)。
#64 (ASAR_SWAP_PS1) 已经处理了 asar 模式,但 unpacked 模式没享受到。

修复 (lib/desktop.js spawnHelper):
  - 新 killInstallElectronSync(execPath):按安装根路径 (不是 /IM 全名)
    同步 PowerShell Stop-Process,防误伤同名第三方
  - spawnHelper 对 killApp 场景 Win 下先 sync kill Electron,再 spawn helper
  - Electron 死后 close event 没 subscriber → 旧回调不触发 → 没 dialog
  - host 作为 Electron 子进程,Electron parent 死不会连带杀 host (无 job object)
  - host spawn helper + 250ms 后 exit,此时 Electron 已死,安全

修复 (lib/restart-desktop.js killApp 分支):
  - win32 分支把开头 sleep(400) 挪到杀之后
  - 立刻 killOtherDesktopProcesses → sleep 500 → 二次 kill (双保险)
  - 兜底,防 host 的 killInstallElectronSync 漏掉的 pid
This commit is contained in:
叫我小杨同学 committed 2026-10-10 00:07:28 +08:00
1 parent 65f7a3d672
commit e23ba79a13
2 files changed
+63 -9

No files matched your search

+49
View File
@@ -816,11 +816,60 @@ function helperEnv(payload) {
return env;
}
// #85: host 自己同步杀 Electron 全家桶 (按安装根路径,不误伤同名第三方)。
// 必须在 spawnHelper 之前跑 — 让 Electron 在 host exit 之前死,close event 根本
// 没 subscriber,旧内存里的 close 回调(可能还是 stopped 版)不会触发对话框。
// unpacked 模式和 asar 模式都要走这个,保证"任何系统任何人电脑"都不出 crash dialog。
function killInstallElectronSync(execPath) {
if (process.platform !== "win32") return false;
if (!execPath) return false;
const installRoot = path.dirname(execPath);
const exeEsc = execPath.replace(/'/g, "''");
const rootEsc = installRoot.replace(/'/g, "''");
const script = [
"$ErrorActionPreference = 'SilentlyContinue'",
"$exe = '" + exeEsc + "'",
"$root = '" + rootEsc + "'",
"$exeLow = $exe.ToLower()",
"$rootLowPrefix = $root.ToLower().TrimEnd('\\') + '\\'",
"function Stop-HarnessInstall {",
" Get-CimInstance Win32_Process | Where-Object {",
" $ep = $_.ExecutablePath",
" if (-not $ep) { return $false }",
" $low = $ep.ToLower()",
" return ($low -eq $exeLow) -or $low.StartsWith($rootLowPrefix)",
" } | ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue }",
"}",
"Stop-HarnessInstall",
"Start-Sleep -Milliseconds 400",
"Stop-HarnessInstall",
].join("; ");
try {
execFileSync("powershell.exe", [
"-NoProfile", "-WindowStyle", "Hidden", "-ExecutionPolicy", "Bypass",
"-Command", script,
], { timeout: 10000, stdio: "ignore", windowsHide: true });
logInternal("killInstallElectronSync 完成 (install root=" + installRoot + ")");
return true;
} catch (err) {
logInternal("killInstallElectronSync 失败: " + (err?.message || err));
return false;
}
}
function spawnHelper(helper, payload, opts = {}) {
const plan = restartPlan();
const hinted = payload && payload.execPath ? payload.execPath : "";
const execPath = hinted || plan.execPath;
if (!execPath) throw new Error(plan.error || "未找到 DSH Desktop.exe,无法重启桌面应用");
// #85: killApp 场景下 host 自己先同步杀 Electron,再 spawn helper。
// 原来 host spawn helper → 250ms 后 exit → Electron 收到 host close event
// → 跑旧内存 close 回调(可能是 stopped 版) → 弹 crash dialog。
// 现在先同步 kill Electron (close event 没 subscriber) → spawn helper detached
// 做 relaunch → host exit 时 Electron 已死,不弹对话框。
if (payload?.killApp && process.platform === "win32") {
killInstallElectronSync(execPath);
}
const child = spawn(process.execPath, [helper], {
detached: true,
stdio: "ignore",
+14 -9
View File
@@ -417,7 +417,20 @@ function relaunchDesktop() {
let asarReleased = true;
if (info.killApp) {
await sleep(400);
// #85: 先杀 Electron,再 sleep + cleanup。原顺序 sleep 400ms 开头 →
// host 已经 exit → Electron 还活着跑旧 close 回调弹 crash dialog。
// 新顺序:立刻 kill (host 这边 killInstallElectronSync 已经杀过,这里兜底),
// Electron 死得越快,旧回调越没机会跑。
if (process.platform === "darwin") {
await quitDarwinDesktop(info.execPath);
} else if (process.platform === "win32") {
killOtherDesktopProcesses(process.pid);
await sleep(500);
// 二次保险:再杀一次,防第一次漏掉的 pid
killOtherDesktopProcesses(process.pid);
} else {
await quitPosixDesktop(info.execPath);
}
for (const dir of cleanup) rmTree(dir);
if (info.helper) {
try {
@@ -426,14 +439,6 @@ if (info.killApp) {
/* ignore */
}
}
if (process.platform === "darwin") {
await quitDarwinDesktop(info.execPath);
} else if (process.platform === "win32") {
killOtherDesktopProcesses(process.pid);
await sleep(500);
} else {
await quitPosixDesktop(info.execPath);
}
if (info.asar) asarReleased = await releaseSealedAsar(info.asar, info.execPath);
} else {
const gone = await waitPidGone(info.pid);