fix(desktop): detach relaunched Desktop from the hand-off console + UTF-8 child streams
First real-world run of the #82328/#82366 hand-off (2026-08-09, ryanc) surfaced two defects: 1. The console window never closes after the update finishes -- and closing it manually KILLS the freshly relaunched GUI. Root cause: Start-DesktopRelaunch spawned Hermes.exe as a child of the console PowerShell. Electron/Chromium calls AttachConsole(ATTACH_PARENT_ PROCESS) at boot, so the new Desktop latched onto the hand-off's console: the console can't close while an attached process lives, and closing it takes the attached GUI down with it. Fix: create the process via WMI (Win32_Process.Create) -- parent becomes WmiPrvSE, no console to inherit or attach, same detachment explorer.exe gives a normal launch. Start-Process fallback retained (tethered Desktop beats no Desktop). 2. Both the console and the progress box render hermes update's UTF-8 glyphs (checkmarks, arrows) as mojibake. PS 5.1 defaults redirected child streams to the OEM codepage. Fix: StandardOutput/ErrorEncoding = UTF8 on the child, PYTHONIOENCODING/PYTHONUTF8 so Python emits UTF-8, and [Console]::OutputEncoding = UTF8 for our own echo. Verified live on the incident machine: WMI-created process parents to WmiPrvSE.exe (not the shell); UTF-8 glyph round-trip through the exact ProcessStartInfo shape reads back byte-correct (15/15 chars). PS 5.1 parse clean, check-windows-footguns clean.
This commit is contained in:
@@ -50,6 +50,12 @@ param(
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Continue"
|
||||
# Render UTF-8 glyphs (checkmarks, arrows) correctly in our own console echo
|
||||
# too; the legacy conhost default OEM codepage shows them as mojibake.
|
||||
try {
|
||||
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
||||
$OutputEncoding = [System.Text.Encoding]::UTF8
|
||||
} catch {}
|
||||
$HermesHome = Split-Path -Parent $InstallRoot
|
||||
$MarkerPath = Join-Path $HermesHome ".hermes-update-in-progress"
|
||||
$LogDir = Join-Path $HermesHome "logs"
|
||||
@@ -148,10 +154,38 @@ function Remove-MarkerIfOwned {
|
||||
function Start-DesktopRelaunch {
|
||||
if ($RelaunchExe -and (Test-Path -LiteralPath $RelaunchExe)) {
|
||||
Write-HandoffLog "relaunching desktop: $RelaunchExe"
|
||||
# DO NOT spawn Hermes.exe as our child: Electron/Chromium calls
|
||||
# AttachConsole(ATTACH_PARENT_PROCESS) at boot, so a Desktop launched
|
||||
# directly from this console PowerShell latches onto OUR console --
|
||||
# the console window then outlives the script (it can't close while
|
||||
# an attached process lives), and closing it kills the freshly
|
||||
# relaunched GUI with it. Create the process via WMI instead: the
|
||||
# parent becomes WmiPrvSE.exe and there is no console to inherit or
|
||||
# attach -- same detachment explorer.exe gives a normal launch.
|
||||
$spawned = $false
|
||||
try {
|
||||
Start-Process -FilePath $RelaunchExe -WorkingDirectory (Split-Path -Parent $RelaunchExe) | Out-Null
|
||||
$workDir = Split-Path -Parent $RelaunchExe
|
||||
$r = Invoke-CimMethod -ClassName Win32_Process -MethodName Create -Arguments @{
|
||||
CommandLine = ('"{0}"' -f $RelaunchExe)
|
||||
CurrentDirectory = $workDir
|
||||
} -ErrorAction Stop
|
||||
if ($r -and $r.ReturnValue -eq 0) {
|
||||
Write-HandoffLog "desktop relaunched detached (pid $($r.ProcessId))"
|
||||
$spawned = $true
|
||||
} else {
|
||||
Write-HandoffLog "WARNING: WMI relaunch returned $($r.ReturnValue); falling back"
|
||||
}
|
||||
} catch {
|
||||
Write-HandoffLog "WARNING: desktop relaunch failed: $($_.Exception.Message)"
|
||||
Write-HandoffLog "WARNING: WMI relaunch failed: $($_.Exception.Message); falling back"
|
||||
}
|
||||
if (-not $spawned) {
|
||||
try {
|
||||
# Fallback keeps the old behavior (console tie-in and all) --
|
||||
# a tethered Desktop beats no Desktop.
|
||||
Start-Process -FilePath $RelaunchExe -WorkingDirectory (Split-Path -Parent $RelaunchExe) | Out-Null
|
||||
} catch {
|
||||
Write-HandoffLog "WARNING: desktop relaunch failed: $($_.Exception.Message)"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -173,6 +207,15 @@ function Invoke-StreamedHermes([string]$Exe, [string[]]$HermesArgs, [string]$Tag
|
||||
$psi.UseShellExecute = $false
|
||||
$psi.RedirectStandardOutput = $true
|
||||
$psi.RedirectStandardError = $true
|
||||
# hermes update prints UTF-8 (checkmarks, arrows, box glyphs). PS 5.1
|
||||
# defaults these readers to the OEM codepage, which mangles every
|
||||
# multi-byte glyph into mojibake in the console AND the progress box.
|
||||
$psi.StandardOutputEncoding = [System.Text.Encoding]::UTF8
|
||||
$psi.StandardErrorEncoding = [System.Text.Encoding]::UTF8
|
||||
# And ask the child to actually EMIT UTF-8: Python decides its stdio
|
||||
# encoding from the console codepage when attached to one.
|
||||
$psi.EnvironmentVariables["PYTHONIOENCODING"] = "utf-8"
|
||||
$psi.EnvironmentVariables["PYTHONUTF8"] = "1"
|
||||
$psi.CreateNoWindow = $true
|
||||
$proc = [System.Diagnostics.Process]::Start($psi)
|
||||
$outWriter = [System.IO.File]::CreateText($outFile)
|
||||
|
||||
Reference in New Issue
Block a user