From e23ba79a13b6fd6230f5d7413b6c7bfa819db104 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=AB=E6=88=91=E5=B0=8F=E6=9D=A8=E5=90=8C=E5=AD=A6?= Date: Sat, 10 Oct 2026 00:07:28 +0800 Subject: [PATCH] =?UTF-8?q?fix(#85):=20host=20=E8=87=AA=E5=B7=B1=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E6=9D=80=20Electron=20=E6=B6=88=E9=99=A4=20restart=20?= =?UTF-8?q?=E6=97=B6=E5=BA=8F=20crash=20dialog?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根因: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 --- lib/desktop.js | 49 ++++++++++++++++++++++++++++++++++++++++++ lib/restart-desktop.js | 23 ++++++++++++-------- 2 files changed, 63 insertions(+), 9 deletions(-) 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);