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.
This commit is contained in:
BearHuddleston
2026-11-28 15:31:03 +00:00
committed by Bear Huddleston
parent 3f6d55b2cc
commit 284cbe3cbc
2 changed files with 35 additions and 3 deletions

View File

@@ -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)" }

View File

@@ -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]