fix(desktop): 换包先杀 Electron main,挪不动时把原版拉起来 (#197)

- 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 通过
This commit is contained in:
iigzx committed 2026-10-31 21:51:32 +08:00
1 parent 7b3f8ede49
commit c236e689f3
2 files changed
+45 -1

No files matched your search

+20 -1
View File
@@ -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;
}
+25
View File
@@ -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/);
});