From 284cbe3cbcb8755fea6d800f47aeac7363256e7b Mon Sep 17 00:00:00 2001 From: BearHuddleston Date: Sat, 28 Nov 2026 15:31:03 +0000 Subject: [PATCH] fix(install): keep the stale-process sighting when a re-read fails windows-stale-processes.ps1 wrote stale-processes.json only after the settle poll. If a later Get-CimInstance call threw, the error left the script before the write, so the full record of the process it had already seen (untruncated command line, CreationDate) was lost, and a process that exits before the finally-block evidence collector runs cannot be read again (Greptile and Sol on #161139). Catch a failed re-read, keep the last records read, save the report with the error as queryError, and then fail the check with that error. A failed first read still throws as before: nothing was seen yet. The new platforms("windows") test fails on the previous head (no stale-processes.json) and passes here on Windows PowerShell 5.1. --- .../e2e-assets/windows-stale-processes.ps1 | 15 +++++++++--- .../install/test_windows_stale_processes.py | 23 +++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/tests/install/e2e-assets/windows-stale-processes.ps1 b/tests/install/e2e-assets/windows-stale-processes.ps1 index 50cb5287ba..94a0b84d90 100644 --- a/tests/install/e2e-assets/windows-stale-processes.ps1 +++ b/tests/install/e2e-assets/windows-stale-processes.ps1 @@ -2,6 +2,7 @@ # When any are seen initially, poll up to -SettleSeconds for post-swap teardown while # recording full process identity (ProcessId, ParentProcessId, Name, CreationDate, # ExecutablePath, CommandLine) to both stdout and proof/stale-processes.json. +# A query that fails while waiting still saves what the first one saw, then fails the check. param( [Parameter(Mandatory = $true)][string]$OldInstallLocation, [Parameter(Mandatory = $true)][string]$ProofDir, @@ -30,11 +31,17 @@ Write-Host " [stale] $($initial.Count) old payload process(es) alive under $($O foreach ($record in $initial) { Write-Host " $(Format-ProcessLine $record)" } $remaining = $initial +$queryError = $null if ($SettleSeconds -gt 0) { $deadline = (Get-Date).AddSeconds($SettleSeconds) - while ($remaining.Count -gt 0 -and (Get-Date) -lt $deadline) { - if ($PollMilliseconds -gt 0) { Start-Sleep -Milliseconds $PollMilliseconds } - $remaining = @(Find-StaleProcesses) + try { + while ($remaining.Count -gt 0 -and (Get-Date) -lt $deadline) { + if ($PollMilliseconds -gt 0) { Start-Sleep -Milliseconds $PollMilliseconds } + $remaining = @(Find-StaleProcesses) + } + } catch { + # Keep the last records read: a process that has exited meanwhile cannot be read again. + $queryError = $_.Exception.Message } } @@ -47,6 +54,7 @@ $report = [ordered]@{ checkedAt = $checkedAt.ToString('o') settleSeconds = $SettleSeconds settled = $settled + queryError = $queryError initial = [object[]]@($initial) remaining = [object[]]@($remaining) } @@ -57,6 +65,7 @@ if ($settled) { Write-Host " [stale] old payload processes exited within ${elapsed}s (saved stale-processes.json)" return } +if ($queryError) { throw "Could not re-read old payload processes while waiting for teardown (saved stale-processes.json): $queryError" } Write-Host " [stale] $($remaining.Count) old payload process(es) still alive after ${SettleSeconds}s (saved stale-processes.json):" foreach ($record in $remaining) { Write-Host " $(Format-ProcessLine $record)" } diff --git a/tests/scripts/install/test_windows_stale_processes.py b/tests/scripts/install/test_windows_stale_processes.py index 376ff4d26a..ba270a7273 100644 --- a/tests/scripts/install/test_windows_stale_processes.py +++ b/tests/scripts/install/test_windows_stale_processes.py @@ -103,3 +103,26 @@ function Get-CimInstance {{ [CmdletBinding()] param([string]$ClassName) assert [p["ProcessId"] for p in report["initial"]] == [9001] assert [p["ProcessId"] for p in report["remaining"]] == [9001] assert report["remaining"][0]["ExecutablePath"].endswith(r"\python.exe") + + +def test_query_error_while_settling_still_saves_the_sighting_and_fails(tmp_path): + fake_cim = rf""" +$global:stale_calls = 0 +function Get-CimInstance {{ [CmdletBinding()] param([string]$ClassName) + $global:stale_calls++ + if ($global:stale_calls -gt 1) {{ throw 'WMI query failed' }} + @([pscustomobject]@{{ ProcessId = 5150; ParentProcessId = 1044; Name = 'python.exe'; CreationDate = $at; ExecutablePath = '{OLD_LOC}\app\resources\agent-payload\tools\python\python.exe'; CommandLine = 'python.exe -m hermes_cli.main gateway run' }}) +}} +""" + result, proof = _run_checker(tmp_path, fake_cim, settle_seconds=3, poll_ms=25) + + combined = result.stdout + result.stderr + assert result.returncode != 0 + assert "WMI query failed" in combined + assert (proof / "stale-processes.json").exists(), combined + report = json.loads((proof / "stale-processes.json").read_text(encoding="utf-8-sig")) + assert report["settled"] is False + assert report["queryError"] == "WMI query failed" + assert [p["ProcessId"] for p in report["initial"]] == [5150] + assert report["initial"][0]["CommandLine"] == "python.exe -m hermes_cli.main gateway run" + assert [p["ProcessId"] for p in report["remaining"]] == [5150]