diff --git a/lib/desktop.js b/lib/desktop.js index 2ce2949..669b62e 100644 --- a/lib/desktop.js +++ b/lib/desktop.js @@ -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", diff --git a/lib/restart-desktop.js b/lib/restart-desktop.js index beddf09..74ca02a 100644 --- a/lib/restart-desktop.js +++ b/lib/restart-desktop.js @@ -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);