From 7fddfe3913d6036637c7931b1d4a6089d84cd27a Mon Sep 17 00:00:00 2001 From: iigzx Date: Sat, 31 Oct 2026 21:51:32 +0800 Subject: [PATCH] =?UTF-8?q?fix(desktop):=20=E6=8D=A2=E5=8C=85=E5=85=88?= =?UTF-8?q?=E6=9D=80=20Electron=20main=EF=BC=8C=E6=8C=AA=E4=B8=8D=E5=8A=A8?= =?UTF-8?q?=E6=97=B6=E6=8A=8A=E5=8E=9F=E7=89=88=E6=8B=89=E8=B5=B7=E6=9D=A5?= =?UTF-8?q?=20(#197)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - ASAR_SWAP_PS1 先按安装根路径 + 非 --type= 识别并 kill Electron main,再扫安装目录其余进程:main 是唯一会弹「host stopped」的对话框来源,先杀它就不会出现 host 死了 main 还活着的半死状态 - 挪包失败的分支补上 Start-Process:用户至少拿到原版应用,不再面对空桌面;失败原因写入 %TEMP%\\dsh-purge-desktop.log - 导出 ASAR_SWAP_PS1 并新增 test/desktop-asar-swap.test.js(3 例,守住顺序与两条重启分支) - 全量 node --test 94/94 通过 --- lib/desktop.js | 21 ++++++++++++++++++++- test/desktop-asar-swap.test.js | 25 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 test/desktop-asar-swap.test.js diff --git a/lib/desktop.js b/lib/desktop.js index fb77468..e5b487f 100644 --- a/lib/desktop.js +++ b/lib/desktop.js @@ -1010,7 +1010,7 @@ function desktopRuntimeOf(ctx) { return runtime && typeof runtime.requestRestart === "function" ? runtime : null; } -const ASAR_SWAP_PS1 = [ +export const ASAR_SWAP_PS1 = [ "param(", " [Parameter(Mandatory = $true)][int]$WaitPid,", " [Parameter(Mandatory = $true)][string]$AsarPath,", @@ -1037,6 +1037,16 @@ const ASAR_SWAP_PS1 = [ "# 来得及处理 host 退出事件就被 kill,UI 根本不存在,无法弹 crash dialog。", "# PowerShell 本身是 detached 独立进程(spawn 时 detached:true, stdio:ignore),", "# 杀 Electron 不会连带杀 PowerShell。", + "# #197b: 先杀 Electron main。main 是唯一会弹「host stopped」对话框的进程:", + "# 若先杀 host,main 还活着就会弹框,然后脚本若在挪 asar 前失败,用户就卡在", + "# 「host 死了、main 半死、asar 没动」的状态(2026-10-31 实测)。先杀 main 就没框。", + "$mains = @(Get-CimInstance Win32_Process -ErrorAction SilentlyContinue | Where-Object {", + " $ep = $_.ExecutablePath", + " if (-not $ep) { return $false }", + " ($ep.ToLower() -eq $exeNorm) -and ($_.CommandLine -notmatch '--type=')", + "})", + "foreach ($p in $mains) { Stop-Process -Id $p.ProcessId -Force -ErrorAction SilentlyContinue }", + "Start-Sleep -Milliseconds 400", "Stop-HarnessInstall", "Start-Sleep -Milliseconds 500", "Stop-HarnessInstall", @@ -1080,7 +1090,15 @@ const ASAR_SWAP_PS1 = [ " Remove-Item Env:ELECTRON_RUN_AS_NODE -ErrorAction SilentlyContinue", " Start-Process -FilePath $ExePath", "} else {", + " # #197b: 挪不动(asar 仍被占用)时不要把用户扔在空桌面上——把原版拉起来,", + " # 留下 rename-pending 让下一次空闲重试;失败原因写进 dsh-purge 日志。", " Set-Content -LiteralPath $lock -Value ((Get-Date).ToString('o')) -Encoding ascii", + " try {", + " $log = Join-Path $env:TEMP 'dsh-purge-desktop.log'", + " Add-Content -LiteralPath $log -Value ((Get-Date).ToUniversalTime().ToString('o') + ' [asar-swap] move failed; relaunching original app (asar still sealed)')", + " } catch {}", + " Remove-Item Env:ELECTRON_RUN_AS_NODE -ErrorAction SilentlyContinue", + " Start-Process -FilePath $ExePath", "}", "", ].join("\r\n"); @@ -1940,3 +1958,4 @@ export function repairOfficialCliAfterUnpack(exe = findDesktopAppExecutable()) { } return report; } + diff --git a/test/desktop-asar-swap.test.js b/test/desktop-asar-swap.test.js new file mode 100644 index 0000000..04f1d73 --- /dev/null +++ b/test/desktop-asar-swap.test.js @@ -0,0 +1,25 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { ASAR_SWAP_PS1 } from "../lib/desktop.js"; + +// #197b:桌面端点「应用」时 host 被杀、main 还活着 → 弹「host stopped」, +// 且 asar 没挪动 → 应用变成半死状态。这里守住两条顺序性约束。 + +test("先杀 Electron main,再杀安装目录其余进程", () => { + const mainKill = ASAR_SWAP_PS1.indexOf("Stop-Process -Id $p.ProcessId"); + // 只看「作为独立一行的调用」,不要命中函数定义 `function Stop-HarnessInstall` 与缩进的内部调用 + const sweep = ASAR_SWAP_PS1.indexOf("\r\nStop-HarnessInstall\r\n"); + assert.ok(mainKill > 0, "缺少 main 进程 kill"); + assert.ok(sweep > 0, "缺少 Stop-HarnessInstall 调用"); + assert.ok(sweep > mainKill, "Stop-HarnessInstall 必须在 main kill 之后"); +}); + +test("main 进程的识别排除渲染/GPU 子进程(--type=)", () => { + assert.match(ASAR_SWAP_PS1, /CommandLine -notmatch '--type='/); +}); + +test("挪包失败时也要把原版拉起来,不留空桌面", () => { + const relaunch = ASAR_SWAP_PS1.split("Start-Process -FilePath $ExePath").length - 1; + assert.equal(relaunch, 2, "成功/失败两个分支都要重启 exe"); + assert.match(ASAR_SWAP_PS1, /move failed; relaunching original app/); +});