fix(launcher): detach portable updater via Start-Process on Windows

Spawning powershell.exe directly with Node detached: true sets DETACHED_PROCESS (no console), causing Windows PowerShell 5.1 ConsoleHost to exit silently with code 0 before running -File. Spawning without detached lets it run, but Electron tears down attached children on app.quit(). Launch the apply script via a short hidden PowerShell Start-Process helper before quitting, properly quoting paths with spaces or non-ASCII characters, and clean up staged files if launching fails.
This commit is contained in:
N0VAN0M4LY committed 2026-12-03 07:48:44 +08:00
1 parent 18637cf1ff
commit 01bafb6db7
2 files changed
+47 -15

No files matched your search

@@ -22,7 +22,9 @@ import {
FeedUnreachableError, readReleaseAsset, releaseAssetChunks, RELEASE_MANIFEST_MAX_BYTES, RELEASE_SIGNATURE_MAX_BYTES, requestReleaseAsset,
} from '../patch-pack/release-assets.ts';
import { installerDigest, launcherSignaturePayload, portableSignaturePayload } from './installer-integrity.ts';
import { isPortableLauncher, launchPortableUpdater, preparePortableInstall } from './portable-update.ts';
import {
cleanupPreparedPortableInstall, isPortableLauncher, launchPortableUpdater, preparePortableInstall,
} from './portable-update.ts';
type UpdateMode = 'installer' | 'portable';
@@ -215,7 +217,12 @@ export function installAppUpdate(version: string, launcherBusy: () => boolean):
try {
if (offer.mode === 'portable') {
const prepared = await preparePortableInstall(path, offer.manifest.version, updatesRoot());
launchPortableUpdater(prepared);
try {
await launchPortableUpdater(prepared);
} catch (launchError) {
await cleanupPreparedPortableInstall(prepared);
throw launchError;
}
app.quit();
return;
}
@@ -7,7 +7,7 @@
* only obsolete files listed in the previous portable inventory (never touching unrelated user
* files in the same folder), and restarts the updated executable.
*/
import { execFile, spawn } from 'node:child_process';
import { execFile } from 'node:child_process';
import { randomUUID } from 'node:crypto';
import * as nodeFs from 'node:fs';
import { existsSync } from 'node:fs';
@@ -283,16 +283,41 @@ export async function preparePortableInstall(
}
}
export function launchPortableUpdater(prepared: PreparedPortableUpdate): void {
const child = spawn('powershell.exe', [
'-NoProfile', '-NonInteractive', '-WindowStyle', 'Hidden', '-ExecutionPolicy', 'Bypass',
'-File', prepared.scriptPath, prepared.planPath,
], {
cwd: dirname(prepared.scriptPath),
detached: true,
stdio: 'ignore',
windowsHide: true,
shell: false,
});
child.unref();
export async function cleanupPreparedPortableInstall(prepared: PreparedPortableUpdate): Promise<void> {
const rawFs = rawFsPromises();
await rawFs.rm(prepared.stagingDir, { recursive: true, force: true }).catch(() => undefined);
await rm(prepared.planPath, { force: true }).catch(() => undefined);
}
/**
* Launch the portable apply script in a hidden PowerShell process that survives Electron exit.
*
* On Windows, spawning powershell.exe directly with Node's `detached: true` sets DETACHED_PROCESS
* (no console), causing Windows PowerShell 5.1 ConsoleHost to exit silently with code 0 before
* running `-File`. Spawning without `detached` lets PowerShell run, but Electron tears down
* attached children on `app.quit()`. Using a short hidden PowerShell helper with `Start-Process`
* gives the updater its own hidden console and cleanly detaches it before the launcher quits.
*/
export async function launchPortableUpdater(prepared: PreparedPortableUpdate): Promise<void> {
const workDir = dirname(prepared.scriptPath);
const launcherPath = join(workDir, `launch-portable-${randomUUID()}.ps1`);
const script = [
'param([string]$ScriptPath, [string]$PlanPath, [string]$WorkDir)',
"$ErrorActionPreference = 'Stop'",
'$argsList = @(',
" '-NoProfile', '-NonInteractive', '-WindowStyle', 'Hidden', '-ExecutionPolicy', 'Bypass',",
' \'-File\', "`"$ScriptPath`"", "`"$PlanPath`""',
')',
"Start-Process -FilePath 'powershell.exe' -WorkingDirectory $WorkDir -WindowStyle Hidden -ArgumentList $argsList",
].join('\r\n');
// UTF-8 BOM ensures Windows PowerShell 5.1 decodes non-ASCII paths properly.
await writeFile(launcherPath, `\uFEFF${script}\r\n`, 'utf8');
try {
await execute('powershell.exe', [
'-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass',
'-File', launcherPath, prepared.scriptPath, prepared.planPath, workDir,
], { cwd: workDir, windowsHide: true, timeout: 30_000 });
} finally {
await rm(launcherPath, { force: true }).catch(() => undefined);
}
}