diff --git a/lib/desktop.js b/lib/desktop.js index 0a46226..74ff919 100644 --- a/lib/desktop.js +++ b/lib/desktop.js @@ -1037,8 +1037,34 @@ export function scheduleAsarRenameWhenIdle({ exe, asar } = {}) { } /** 等当前桌面进程退出后把 app.asar 挪走,再启动 exe。解开后的 resources/app 才会被加载。 */ -export function scheduleAsarSwapRestart({ exe, asar, waitPid = process.pid, exit = false } = {}) { +export function scheduleAsarSwapRestart({ exe, asar, waitPid = process.pid, exit = false, runtime = null } = {}) { if (!exe || !asar) throw new Error("缺少桌面 exe 或 app.asar,无法重启"); + // runtime (desktopRuntime) 存在时,先让宿主自己 ack restart,再 process.exit; + // 否则宿主 main.js:3725 的 ChildProcess listener 会把 host 退出当"host stopped" crash + // 弹对话框(2026-10-09 用户点"重启"后弹崩溃的根因 —— 其实 asar swap 走的 PowerShell + // 正常起来了,只是宿主 UX 把"host 退出"=="崩溃")。 + const gracefulExit = () => { + if (!runtime) { + // 没拿到 runtime —— 走旧 process.exit 路径,timeout 加长到 1500ms 给 PowerShell handoff。 + try { console.warn("[dsh-purge] asar swap: planned host exit (no runtime handoff)"); } catch {} + setTimeout(() => process.exit(0), 1500); + return; + } + try { console.warn("[dsh-purge] asar swap: handing off restart to desktopRuntime"); } catch {} + try { + // requestRestart 是宿主自己安排的 restart —— 宿主知道 host 会主动退,不弹 crash。 + Promise.resolve(runtime.requestRestart()).catch((err) => { + try { console.warn("[dsh-purge] desktopRuntime.requestRestart rejected:", err?.message || err); } catch {} + }); + } catch (err) { + try { console.warn("[dsh-purge] desktopRuntime.requestRestart threw:", err?.message || err); } catch {} + } + // 兜底:若宿主 1.8s 内没主动 kill host,我们自己退(避免永远挂着)。 + setTimeout(() => { + try { process.exit(0); } catch {} + }, 1800); + }; + if (sealedSwapKind() === "helper") { const helper = fileURLToPath(new URL("./restart-desktop.js", import.meta.url)); spawnHelper(helper, { @@ -1049,7 +1075,7 @@ export function scheduleAsarSwapRestart({ exe, asar, waitPid = process.pid, exit cwd: path.dirname(exe), waitPid, }, { exit: false }); - if (exit !== false) setTimeout(() => process.exit(0), 800); + if (exit !== false) gracefulExit(); return { scheduled: true }; } const script = path.join(os.tmpdir(), `dsh-purge-asar-swap-${process.pid}.ps1`); @@ -1072,12 +1098,12 @@ export function scheduleAsarSwapRestart({ exe, asar, waitPid = process.pid, exit { detached: true, stdio: "ignore", windowsHide: true, shell: false }, ); // 没挂 'error' listener,PowerShell 不在 PATH / 被 AV 拦就是 unhandled 'error' event, - // 会被 Node 当 uncaughtException 直接把宿主崩掉(2026-10-09 crash 根因)。 + // 会被 Node 当 uncaughtException 直接把宿主崩掉。 child.on("error", (err) => { try { console.warn("[dsh-purge] asar swap PowerShell spawn failed:", err?.message || err); } catch {} }); child.unref(); - if (exit !== false) setTimeout(() => process.exit(0), 800); + if (exit !== false) gracefulExit(); return { scheduled: true }; } @@ -1135,11 +1161,14 @@ export function scheduleRestart(request, ctx, options = {}) { findDesktopAppExecutable() || ""; const asar = exe ? appAsarPath(exe) : ""; + // ctx 透传给 restartOfficialHarness → scheduleAsarSwapRestart,让 PowerShell 分支 + // 拿到 desktopRuntime 走 requestRestart 优雅退出,避免宿主误报 crash。 if (process.platform === "win32" && exe && isOfficialHarnessExecutable(exe)) { - return restartOfficialHarness({ exe, asar }); + return restartOfficialHarness({ exe, asar, ctx }); } if (exe && asar && asarArchiveIsFile(asar)) { - scheduleAsarSwapRestart({ exe, asar, waitPid: process.pid, exit: true }); + const runtime = desktopRuntimeOf(ctx); + scheduleAsarSwapRestart({ exe, asar, waitPid: process.pid, exit: true, runtime }); return { restarting: true, asarSwap: true }; } const runtime = desktopRuntimeOf(ctx); @@ -1350,6 +1379,9 @@ export function restartOfficialHarness(options = {}) { findDesktopAppExecutable() || ""; if (!exe) return { restarting: false }; + // ctx 可用时 → 拿 desktopRuntime 透传给 PowerShell 分支: + // 让宿主 ack 预期的 restart,避免 host 退出被当成 crash 弹对话框。 + const runtime = options.runtime || (options.ctx ? desktopRuntimeOf(options.ctx) : null); if (!isOfficialHarnessExecutable(exe)) { const asar = options.asar || appAsarPath(exe); if (asar && asarArchiveIsFile(asar)) { @@ -1358,6 +1390,7 @@ export function restartOfficialHarness(options = {}) { asar, waitPid: process.pid, exit: options.exit !== false, + runtime, }); return { restarting: true, asarSwap: true }; } @@ -1399,6 +1432,7 @@ export function restartOfficialHarness(options = {}) { asar, waitPid: process.pid, exit: options.exit !== false, + runtime, }); } catch (error) { const message = error instanceof Error ? error.message : String(error);