Skip to content

The agent that grows with you

fix(desktop-update): a hand-off log line that meets another holder waits instead of vanishing

Teknium3d304a2816d2
diff --git a/scripts/desktop-update/windows.ps1 b/scripts/desktop-update/windows.ps1
index 03e91bb4ddc..a364b20c221 100644
--- a/scripts/desktop-update/windows.ps1
+++ b/scripts/desktop-update/windows.ps1
@@ -114,17 +114,14 @@ try {
$handoffLogHash = [BitConverter]::ToString($handoffSha.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($LogPath.ToLowerInvariant())), 0, 8).Replace("-", "")
} finally { $handoffSha.Dispose() }
$script:HandoffLogMutexName = "Global\HermesHandoffLog-$handoffLogHash"
-
-function Write-HandoffLog([string]$Message) {
- $line = "{0:yyyy-MM-ddTHH:mm:ssK} {1}" -f (Get-Date), $Message
- # Add-Content is unsafe once a second writer is live: PowerShell caches a
- # content writer per path, so the second writer (an overlapping hand-off
- # process, or any future runspace writer) gets a handle the first already
- # consumed -- Windows PowerShell 5.1 floods "stream is not readable"
- # errors while PowerShell 7 silently loses lines (#126152). A named mutex
- # plus one FileStream per write serializes appends across processes and
- # runspaces; the bounded wait and the empty catch keep the "logging must
- # never break the update" contract.
+# Lines the log could not take yet, oldest first (Write-HandoffLog).
+$script:PendingLogLines = New-Object System.Collections.Generic.List[string]
+
+function Sync-HandoffLog {
+ # Append every pending line in one write, oldest first. False while another process still
+ # holds the log: the lines stay queued for the next attempt instead of being dropped.
+ if ($script:PendingLogLines.Count -eq 0) { return $true }
+ $bytes = [System.Text.Encoding]::UTF8.GetBytes((($script:PendingLogLines -join "`r`n") + "`r`n"))
$held = $false
try {
$mutex = $script:HandoffLogMutex
@@ -136,13 +133,41 @@ function Write-HandoffLog([string]$Message) {
}
try { $held = $mutex.WaitOne(5000) }
catch [System.Threading.AbandonedMutexException] { $held = $true } # a writer died mid-append; the wait still granted ownership
- $bytes = [System.Text.Encoding]::UTF8.GetBytes($line + [Environment]::NewLine)
- $fs = New-Object System.IO.FileStream($LogPath, [System.IO.FileMode]::Append, [System.IO.FileAccess]::Write, [System.IO.FileShare]::ReadWrite)
- try { $fs.Write($bytes, 0, $bytes.Length); $fs.Flush() } finally { $fs.Dispose() }
+ # AppendData with full sharing: never conflicts with a reader, and lands at end-of-file
+ # atomically even beside a writer that skipped the mutex (its wait timed out).
+ $fs = New-Object System.IO.FileStream($LogPath, [System.IO.FileMode]::Append,
+ [System.Security.AccessControl.FileSystemRights]::AppendData,
+ ([System.IO.FileShare]::ReadWrite -bor [System.IO.FileShare]::Delete), 4096, [System.IO.FileOptions]::None)
+ try { $fs.Write($bytes, 0, $bytes.Length) } finally { $fs.Dispose() }
} catch {
+ return $false
} finally {
if ($held) { try { $script:HandoffLogMutex.ReleaseMutex() } catch {} }
}
+ $script:PendingLogLines.Clear()
+ return $true
+}
+
+function Complete-HandoffLog([int]$Seconds = 10) {
+ # Before exiting: a holder that outlives the run would otherwise take the last lines with it.
+ $deadline = (Get-Date).AddSeconds($Seconds)
+ while (-not (Sync-HandoffLog) -and (Get-Date) -lt $deadline) { Start-Sleep -Milliseconds 100 }
+}
+
+function Write-HandoffLog([string]$Message) {
+ $line = "{0:yyyy-MM-ddTHH:mm:ssK} {1}" -f (Get-Date), $Message
+ # Add-Content is unsafe once a second writer is live: PowerShell caches a
+ # content writer per path, so the second writer (an overlapping hand-off
+ # process, or any future runspace writer) gets a handle the first already
+ # consumed -- Windows PowerShell 5.1 floods "stream is not readable"
+ # errors while PowerShell 7 silently loses lines (#126152). A named mutex
+ # serializes appends across processes and runspaces. Any other holder (a
+ # scanner on the just-closed file, a reader opened without write sharing)
+ # still fails an append: that line waits in the queue and lands, in order,
+ # with the next append that succeeds. A relayed stage is marked seen before
+ # it is logged, so a dropped line there was never written again.
+ $script:PendingLogLines.Add($line)
+ [void](Sync-HandoffLog)
if ($MarkerOp) { return } # a helper op's stdout is its one verdict line
if ($script:ConsoleInput -and [HermesHandoff.ConsoleInput]::Selecting()) { return }
Write-Host $line
@@ -1248,6 +1273,7 @@ function Invoke-HermesStep([string]$Exe, [string[]]$HermesArgs, [string]$Tag) {
}
if ($script:Ui) { [System.Windows.Forms.Application]::DoEvents() }
Update-MarkerHeartbeat
+ [void](Sync-HandoffLog)
if ((Get-Date) -ge $nextStageCheck) {
Sync-ChildStage
$nextStageCheck = (Get-Date).AddSeconds(1)
@@ -1422,7 +1448,7 @@ exit 7
# Writes straight to the console stream, holding nothing: a step that is
# merely loud. `hermes update` is this shape -- the Electron/vite build
# alone is megabytes. Few large lines rather than many small ones on
- # purpose: Write-HandoffLog is one Add-Content per line and runs inside the
+ # purpose: Write-HandoffLog is one file append per line and runs inside the
# measured window, so line-heavy output would time the logger instead of
# the drain.
$floodSource = @'
@@ -1591,23 +1617,25 @@ if ($SelfTestLogConcurrency) {
# would leave the runspace without a callable definition.
$fnAst = [System.Management.Automation.Language.Parser]::ParseFile($PSCommandPath, [ref]$null, [ref]$null).FindAll({
param($node)
- $node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -eq "Write-HandoffLog"
+ $node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -in @("Write-HandoffLog", "Sync-HandoffLog", "Complete-HandoffLog")
}, $true)
- if (-not $fnAst -or $fnAst.Count -eq 0) {
- Write-Host "LOG-CONCURRENCY SELF-TEST: FAIL -- Write-HandoffLog definition not found in this script"
+ if (-not $fnAst -or $fnAst.Count -ne 3) {
+ Write-Host "LOG-CONCURRENCY SELF-TEST: FAIL -- the hand-off log writer definitions were not found in this script"
exit 1
}
- $fnText = $fnAst[0].Extent.Text
+ $fnText = (@($fnAst) | ForEach-Object { $_.Extent.Text }) -join "`r`n`r`n"
$body = @'
param([string]$TargetLog, [string]$MutexName, [int]$Count, [int]$WriterId)
$LogPath = $TargetLog
$script:HandoffLogMutexName = $MutexName
$script:HandoffLogMutex = $null
+$script:PendingLogLines = New-Object System.Collections.Generic.List[string]
$script:ConsoleInput = $false
'@ + $fnText + @'
for ($i = 0; $i -lt $Count; $i++) { Write-HandoffLog ("w$WriterId line$i") }
+Complete-HandoffLog
'@
$pool = [runspacefactory]::CreateRunspacePool(1, $writers)
$pool.Open()
@@ -2018,5 +2046,6 @@ try {
}
}
if ($null -ne $savedConsoleInputMode) { [HermesHandoff.ConsoleInput]::Restore($savedConsoleInputMode) }
+ Complete-HandoffLog
}
exit $finalCode
diff --git a/tests/scripts/desktop_update/test_desktop_update_windows_child_stage.py b/tests/scripts/desktop_update/test_desktop_update_windows_child_stage.py
index c4489c72e5a..9e67498254e 100644
--- a/tests/scripts/desktop_update/test_desktop_update_windows_child_stage.py
+++ b/tests/scripts/desktop_update/test_desktop_update_windows_child_stage.py
@@ -68,3 +68,93 @@ def test_handoff_shows_the_stage_the_update_publishes(tmp_path: Path) -> None:
except subprocess.TimeoutExpired:
subprocess.run(["taskkill", "/T", "/F", "/PID", str(script.pid)], capture_output=True, check=False)
script.wait(timeout=30)
+
+
+
+HELD_LOG_CLI = """
+import os, sys
+from pathlib import Path
+def main():
+ if '--version' in sys.argv:
+ print('Install directory: ' + str(Path(__file__).resolve().parents[1])); return 0
+ if '--help' in sys.argv:
+ print('--keep-stash'); return 0
+ if sys.argv[1:2] == ['update']:
+ sys.path.insert(0, os.environ['HANDOFF_REPO'])
+ for name in [m for m in sys.modules if m == 'hermes_cli' or m.startswith('hermes_cli.')]:
+ del sys.modules[name]
+ from hermes_cli.update_stage import publish_stage
+ from tests.waiting import wait_until
+ Path(os.environ['HANDOFF_STARTED']).write_text('', encoding='utf-8')
+ wait_until(Path(os.environ['HANDOFF_RELEASED']).exists, timeout=60, what='the log to be released')
+ publish_stage('Packaging the desktop app')
+ wait_until(Path(os.environ['HANDOFF_HOLD']).exists, timeout=60, what='the test to release the update')
+ return 0
+if __name__ == '__main__':
+ sys.exit(main())
+"""
+
+
+def _hold_against_writers(path: Path) -> int:
+ """Open ``path`` the way a scanner or a .NET reader does: read access, shared for reading only,
+ so every append fails with a sharing violation until the handle closes."""
+ import ctypes
+ from ctypes import wintypes
+
+ kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
+ kernel32.CreateFileW.argtypes = [wintypes.LPCWSTR, wintypes.DWORD, wintypes.DWORD, wintypes.LPVOID,
+ wintypes.DWORD, wintypes.DWORD, wintypes.HANDLE]
+ kernel32.CreateFileW.restype = wintypes.HANDLE
+ generic_read, file_share_read, open_existing = 0x80000000, 0x1, 3
+ handle = kernel32.CreateFileW(str(path), generic_read, file_share_read, None, open_existing, 0, None)
+ assert handle not in (None, wintypes.HANDLE(-1).value), ctypes.get_last_error()
+ return handle
+
+
+def _release(handle: int) -> None:
+ import ctypes
+ from ctypes import wintypes
+
+ kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
+ kernel32.CloseHandle.argtypes = [wintypes.HANDLE]
+ assert kernel32.CloseHandle(handle)
+
+
+def test_lines_logged_while_another_process_holds_the_log_land_once_it_lets_go(tmp_path: Path) -> None:
+ install = tmp_path / "checkout"
+ publish_fixture_launcher(install, HELD_LOG_CLI)
+ home = tmp_path / "home"
+ log = home / "logs" / "desktop-update-handoff.log"
+ log.parent.mkdir(parents=True)
+ log.write_bytes(b"")
+ started, released, hold = tmp_path / "started", tmp_path / "released", tmp_path / "release-update"
+ handle = _hold_against_writers(log)
+ script = subprocess.Popen(
+ ["powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", str(SCRIPT),
+ "-InstallRoot", str(install), "-NoUi"],
+ cwd=tmp_path, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
+ env={**os.environ, "HERMES_HOME": str(home), "HERMES_RUNTIME_DIR": str(tmp_path / "empty-store"),
+ "HANDOFF_STARTED": str(started), "HANDOFF_RELEASED": str(released), "HANDOFF_HOLD": str(hold),
+ "HANDOFF_REPO": str(ROOT)},
+ )
+ try:
+ # Everything up to the update's own start was logged while the log was held.
+ wait_until(lambda: started.exists() or script.poll() is not None, timeout=90, what="the update to start")
+ _release(handle)
+ handle = 0
+ released.write_text("", encoding="utf-8")
+ wait_until(lambda: "stage: Packaging the desktop app" in _log(home) or script.poll() is not None,
+ timeout=90, what="the hand-off to relay the update's stage")
+ text = _log(home)
+ assert "running: python" in text and "stage: Packaging the desktop app" in text, (
+ "lines logged while the log was held were lost:\n" + text[-3000:])
+ finally:
+ if handle:
+ _release(handle)
+ hold.write_text("", encoding="utf-8")
+ try:
+ script.wait(timeout=120)
+ except subprocess.TimeoutExpired:
+ subprocess.run(["taskkill", "/T", "/F", "/PID", str(script.pid)], capture_output=True, check=False)
+ script.wait(timeout=30)
diff --git a/tests/scripts/desktop_update/test_desktop_update_windows_log_concurrency.py b/tests/scripts/desktop_update/test_desktop_update_windows_log_concurrency.py
index c1f43074fd3..0f98e8eec7d 100644
--- a/tests/scripts/desktop_update/test_desktop_update_windows_log_concurrency.py
+++ b/tests/scripts/desktop_update/test_desktop_update_windows_log_concurrency.py
@@ -71,8 +71,11 @@ def test_concurrent_writers_lose_no_handoff_log_lines(tmp_path: Path) -> None:
]
result = subprocess.run(
argv,
+ check=False,
capture_output=True,
text=True,
+ encoding="utf-8",
+ errors="replace",
timeout=300,
env=env,
cwd=os.fspath(REPO_ROOT),