Skip to content

The agent that grows with you

fix(desktop-update): flush the hand-off log on every exit, encode it only when it opens

Teknium1733a4a653ac
diff --git a/scripts/desktop-update/windows.ps1 b/scripts/desktop-update/windows.ps1
index a364b20c221..8ace25578f6 100644
--- a/scripts/desktop-update/windows.ps1
+++ b/scripts/desktop-update/windows.ps1
@@ -121,7 +121,6 @@ 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
@@ -138,7 +137,12 @@ function Sync-HandoffLog {
$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() }
+ try {
+ # Encoded only once the file is open: a held log retried on every poll of a long step
+ # must not re-encode the whole growing queue each time.
+ $bytes = [System.Text.Encoding]::UTF8.GetBytes((($script:PendingLogLines -join "`r`n") + "`r`n"))
+ $fs.Write($bytes, 0, $bytes.Length)
+ } finally { $fs.Dispose() }
} catch {
return $false
} finally {
@@ -179,12 +183,19 @@ function Write-HandoffLog([string]$Message) {
New-Item -ItemType Directory -Path $LogDir -Force -ErrorAction SilentlyContinue | Out-Null
try { . (Join-Path $PSScriptRoot 'marker.ps1') } catch {
Write-HandoffLog "Update aborted: $PSScriptRoot\marker.ps1 could not be loaded ($($_.Exception.Message)). Nothing was changed. Repair the installation and try again."
+ Complete-HandoffLog
exit 3
}
-# Helper op (SPEC 6): one verdict line, before any UI, result or relaunch.
-if ($MarkerOp -ceq 'custody') { Invoke-MarkerCustody $CustodyOf; exit 0 } # Start-MarkerCustodian's watcher
-if ($MarkerOp) { exit (Invoke-MarkerOpCli $MarkerOp $DesktopPid $HandoffRun) }
+# Helper op (SPEC 6): one verdict line, before any UI, result or relaunch. Each releases the
+# marker lock before it returns; the log flush comes after. A reclaim/withdraw flush is short
+# because Electron bounds the whole helper (MARKER_HELPER_TIMEOUT_MS); its verdict is already out.
+if ($MarkerOp -ceq 'custody') { Invoke-MarkerCustody $CustodyOf; Complete-HandoffLog; exit 0 } # Start-MarkerCustodian's watcher
+if ($MarkerOp) {
+ $markerOpCode = Invoke-MarkerOpCli $MarkerOp $DesktopPid $HandoffRun
+ Complete-HandoffLog 3
+ exit $markerOpCode
+}
# The Desktop's identity is pinned now: a reused pid later is not "still open".
$script:DesktopCt = $null
@@ -194,7 +205,7 @@ if ($DesktopPid -gt 0) {
$script:DesktopSeenAlive = $desktopProbe.Alive
$script:DesktopCt = $desktopProbe.Ct
}
-if (-not $SelfTestUi -and -not $SelfTestPipeDrain) {
+if (-not $SelfTestUi -and -not $SelfTestPipeDrain -and -not $SelfTestLogConcurrency) {
$script:MarkerClaim = Invoke-MarkerClaim
}
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 9e67498254e..77e49f67e73 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
@@ -14,7 +14,7 @@ import subprocess
import pytest
from tests.installation_launcher_fixture import publish_fixture_launcher
-from tests.scripts.desktop_update.windows_handoff_support import _log
+from tests.scripts.desktop_update.windows_handoff_support import _hold_against_writers, _log, _release
from tests.waiting import wait_until
ROOT = Path(__file__).resolve().parent.parent.parent.parent
@@ -95,31 +95,6 @@ if __name__ == '__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)
-
-
@pytest.mark.platforms("windows")
def test_lines_logged_while_another_process_holds_the_log_land_once_it_lets_go(tmp_path: Path) -> None:
install = tmp_path / "checkout"
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 0f98e8eec7d..ff3cef1eb43 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
@@ -59,6 +59,10 @@ def test_concurrent_writers_lose_no_handoff_log_lines(tmp_path: Path) -> None:
# so the test leaves nothing behind.
env["TEMP"] = os.fspath(tmp_path)
env["TMP"] = os.fspath(tmp_path)
+ # The self-test touches no update state: no marker claim (which its exit would never release).
+ home = tmp_path / "home"
+ home.mkdir()
+ env["HERMES_HOME"] = os.fspath(home)
argv = [
os.fspath(powershell),
@@ -90,3 +94,5 @@ def test_concurrent_writers_lose_no_handoff_log_lines(tmp_path: Path) -> None:
diagnosis = result.stdout[-6000:] + result.stderr[-6000:]
if "LOG-CONCURRENCY SELF-TEST: PASS" not in result.stdout or result.returncode != 0:
pytest.fail(diagnosis, pytrace=False)
+ left = sorted(p.name for p in home.iterdir() if p.name != "logs")
+ assert not left, f"the self-test left update state in HERMES_HOME: {left}"
diff --git a/tests/scripts/desktop_update/test_desktop_update_windows_log_exit_flush.py b/tests/scripts/desktop_update/test_desktop_update_windows_log_exit_flush.py
new file mode 100644
index 00000000000..b7dba5da444
--- /dev/null
+++ b/tests/scripts/desktop_update/test_desktop_update_windows_log_exit_flush.py
@@ -0,0 +1,91 @@
+"""Every production exit of ``windows.ps1`` flushes the hand-off log lines it could not write yet.
+
+A line logged while another process holds ``desktop-update-handoff.log`` against writers waits in
+``Write-HandoffLog``'s queue. The main update drains that queue (bounded) in its ``finally``; the
+marker helper ops and the early marker-load failure exit straight away, so on the base their only
+log line vanished whenever the log was held at that moment. Each test holds the log, waits for the
+line the script prints right after its failed append (a helper op's verdict, the console echo of
+the abort), lets go, and requires the line in the file once the process exits.
+"""
+from __future__ import annotations
+
+from pathlib import Path
+import shutil
+import subprocess
+import time
+
+import pytest
+
+from tests.scripts.desktop_update.windows_handoff_support import (
+ MARKER,
+ POWERSHELL,
+ SCRIPT,
+ _dead_pid,
+ _env,
+ _hold_against_writers,
+ _log,
+ _release,
+)
+
+
+def _held_log(home: Path) -> int:
+ log = home / "logs" / "desktop-update-handoff.log"
+ log.parent.mkdir(parents=True)
+ log.write_bytes(b"")
+ return _hold_against_writers(log)
+
+
+def _finish(proc: subprocess.Popen, handle: int) -> None:
+ if handle:
+ _release(handle)
+ try:
+ proc.wait(timeout=60)
+ except subprocess.TimeoutExpired:
+ subprocess.run(["taskkill", "/T", "/F", "/PID", str(proc.pid)], capture_output=True, check=False)
+ proc.wait(timeout=30)
+
+
+def test_a_marker_op_log_line_lands_after_the_log_is_released(tmp_path: Path) -> None:
+ (tmp_path / MARKER).write_bytes(f"{_dead_pid()}\n{int(time.time())}\nct:5.000\n".encode())
+ handle = _held_log(tmp_path)
+ proc = subprocess.Popen(
+ [POWERSHELL, "-NoProfile", "-NonInteractive", "-ExecutionPolicy", "Bypass", "-File", str(SCRIPT),
+ "-MarkerOp", "reclaim", "-InstallRoot", str(tmp_path / "hermes-agent")],
+ cwd=tmp_path, env=_env(tmp_path), stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
+ text=True, encoding="utf-8", errors="replace")
+ assert proc.stdout is not None
+ try:
+ # The verdict is printed after the op logged (and failed to append) its line.
+ assert proc.stdout.readline() == "reclaimed\n"
+ _release(handle)
+ handle = 0
+ assert proc.wait(timeout=60) == 0
+ finally:
+ _finish(proc, handle)
+ assert "marker-op reclaim: removed a " in _log(tmp_path), _log(tmp_path)
+
+
+def test_the_marker_load_failure_line_lands_after_the_log_is_released(tmp_path: Path) -> None:
+ # A copy of the hand-off without marker.ps1 beside it: the early "could not be loaded" exit.
+ alone = tmp_path / "script"
+ alone.mkdir()
+ shutil.copy2(SCRIPT, alone / "windows.ps1")
+ home = tmp_path / "home"
+ handle = _held_log(home)
+ proc = subprocess.Popen(
+ [POWERSHELL, "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", str(alone / "windows.ps1"),
+ "-InstallRoot", str(home / "hermes-agent"), "-NoUi"],
+ cwd=tmp_path, env=_env(home), stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
+ text=True, encoding="utf-8", errors="replace")
+ assert proc.stdout is not None
+ try:
+ # Write-HandoffLog echoes to the console after its append failed.
+ assert "marker.ps1 could not be loaded" in proc.stdout.readline()
+ _release(handle)
+ handle = 0
+ assert proc.wait(timeout=60) == 3
+ finally:
+ _finish(proc, handle)
+ assert "marker.ps1 could not be loaded" in _log(home), _log(home)
diff --git a/tests/scripts/desktop_update/windows_handoff_support.py b/tests/scripts/desktop_update/windows_handoff_support.py
index 8cdb9600371..54ec5f8216d 100644
--- a/tests/scripts/desktop_update/windows_handoff_support.py
+++ b/tests/scripts/desktop_update/windows_handoff_support.py
@@ -151,3 +151,28 @@ def _wait_for_log(home: Path, needle: str, timeout: int = 90) -> str:
assert time.monotonic() < deadline, f'{needle!r} never logged: {_log(home)}'
time.sleep(0.1)
return _log(home)
+
+
+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)