From bab949e16ad27c28536cb1574aebad79ff312131 Mon Sep 17 00:00:00 2001 From: xmk23333 Date: Thu, 15 Jan 2026 17:53:21 +0800 Subject: [PATCH] fix: ensure admin restart waits for new process before quitting --- src/main/core/permissions.ts | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/main/core/permissions.ts b/src/main/core/permissions.ts index 1bb519e..0a73882 100644 --- a/src/main/core/permissions.ts +++ b/src/main/core/permissions.ts @@ -260,31 +260,29 @@ export async function restartAsAdmin(forTun: boolean = true): Promise { const args = process.argv.slice(1) const restartArgs = forTun ? [...args, '--admin-restart-for-tun'] : args - try { - const escapedExePath = exePath.replace(/'/g, "''") - const argsString = restartArgs.map((arg) => arg.replace(/'/g, "''")).join("', '") + const escapedExePath = exePath.replace(/'/g, "''") + const argsString = restartArgs.map((arg) => arg.replace(/'/g, "''")).join("', '") - const command = - restartArgs.length > 0 - ? `powershell -NoProfile -Command "Start-Process -FilePath '${escapedExePath}' -ArgumentList '${argsString}' -Verb RunAs"` - : `powershell -NoProfile -Command "Start-Process -FilePath '${escapedExePath}' -Verb RunAs"` + const command = + restartArgs.length > 0 + ? `powershell -NoProfile -Command "Start-Process -FilePath '${escapedExePath}' -ArgumentList '${argsString}' -Verb RunAs -Wait:$false; exit 0"` + : `powershell -NoProfile -Command "Start-Process -FilePath '${escapedExePath}' -Verb RunAs -Wait:$false; exit 0"` - managerLogger.info('Restarting as administrator with command', command) + managerLogger.info('Restarting as administrator with command', command) + return new Promise((resolve, reject) => { exec(command, { windowsHide: true }, (error, _stdout, stderr) => { if (error) { managerLogger.error('PowerShell execution error', error) managerLogger.error('stderr', stderr) - } else { - managerLogger.info('PowerShell command executed successfully') + reject(new Error(`Failed to restart as administrator: ${error.message}`)) + return } + managerLogger.info('PowerShell command executed successfully, quitting app') + setTimeout(() => app.quit(), 500) + resolve() }) - - app.quit() - } catch (error) { - managerLogger.error('Failed to restart as administrator', error) - throw new Error(`Failed to restart as administrator: ${error}`) - } + }) } export async function requestTunPermissions(): Promise {