fix(desktop): 点重启弹崩溃根因 - 让宿主 ack 预期退出,别裸 process.exit

现象:点"应用"→ 弹"重启"对话框 → 点重启 → 弹"host stopped"崩溃对话框。
实际 PowerShell asar swap 正常启动了,只是宿主 main.js:3725 的 ChildProcess
listener 把 host 子进程退出一律当"崩溃"报,并把 host stderr 全 dump 给用户看
(2026-10-09 crash log 栈顶 `at ChildProcess.<anonymous> ... main.js:3725:36`
+ `maybeClose` + `_handle.onexit` 就是这条路径)。

修法:
1. scheduleAsarSwapRestart 加 options.runtime(desktopRuntime 实例)。
   有 runtime → 先 await runtime.requestRestart() 让宿主主动安排 restart,
   宿主知道 host 会主动退就不弹 crash;setTimeout 1.8s 兜底 process.exit。
   没 runtime → 兜底 process.exit 1.5s(比原 0.8s 长,给 PowerShell handoff
   更多时间)。两条路径都挂日志标记 planned exit 便于排查。
2. restartOfficialHarness 加 options.ctx / options.runtime,内部 desktopRuntimeOf
   拿到 runtime 后透传给 PowerShell 和 helper 两个 scheduleAsarSwapRestart 分支。
3. scheduleRestart 把 ctx 透传给 restartOfficialHarness,asar swap fallback 分支
   也拿 runtime 走同一条优雅退出路径。

版本号保持 1.1.67,不发新版。
This commit is contained in:
叫我小杨同学 committed 2026-10-09 14:14:53 +08:00
1 parent 567aba33a3
commit e3fbbd1fd0
1 file changed
+40 -6
+40 -6
View File
@@ -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);