test(desktop-update): retry the protected-owner pick when the pid vanishes

The x64 Windows CI lane failed test_owner_creation_time_is_read_for_
system_processes[True] with CalledProcessError from the creation-time
helper: on a busy runner (16 workers) the scan can pick a short-lived
SYSTEM process that exits — or WMI refuses the re-read — between the scan
and the query, leaving a null CreationDate and a non-zero powershell exit.

_creation_time now returns None instead of raising on a vanished or
unreadable pid, and the scenario picks its owner through
_live_protected_owner(): pick, read, and exclude each vanished pid,
retrying, so the marker scenario starts from a protected process that
actually outlives it. Both ct_matches arms use the same live owner —
the A1 verdict only means anything for a live one.

Full marker file on a native Windows 11 arm64 host: 19 passed, 0 failed.

(cherry picked from commit c03ec83d7f616e8bf21e596b04f5c325b8466ab1)
This commit is contained in:
ethernet
2026-10-30 13:03:16 -04:00
committed by BearHuddleston
parent 4d5caff093
commit 1427067f05

View File

@@ -44,18 +44,24 @@ def sleeper():
proc.wait()
def _creation_time(pid: int) -> str:
def _creation_time(pid: int) -> str | None:
"""The pid's CIM creation time in marker format, or None when the pid died
or WMI refused the query — a busy runner's short-lived SYSTEM processes
vanish between the scan that picked them and this read."""
# Limited query rights (what Python/Rust readers use): works for every process.
out = subprocess.run(
['powershell', '-NoProfile', '-Command',
f"$c = (Get-CimInstance Win32_Process -Filter 'ProcessId={pid}').CreationDate; "
"[DateTimeOffset]::new($c.ToUniversalTime()).ToUnixTimeMilliseconds().ToString()"],
capture_output=True, text=True, timeout=60, check=True,
).stdout.strip()
return f'{int(out) / 1000:.3f}'
try:
out = subprocess.run(
['powershell', '-NoProfile', '-Command',
f"$c = (Get-CimInstance Win32_Process -Filter 'ProcessId={pid}').CreationDate; "
"[DateTimeOffset]::new($c.ToUniversalTime()).ToUnixTimeMilliseconds().ToString()"],
capture_output=True, text=True, timeout=60, check=True,
).stdout.strip()
return f'{int(out) / 1000:.3f}'
except (subprocess.CalledProcessError, ValueError):
return None
def _protected_pid() -> int:
def _protected_pid(exclude: set[int] | frozenset[int] = frozenset()) -> int:
"""A live SYSTEM/protected pid whose creation time Windows PowerShell's Get-Process .StartTime
cannot read from this token while Win32_Process.CreationDate (CIM) can: the A1 case.
@@ -81,7 +87,7 @@ def _protected_pid() -> int:
candidates = []
for row in rows:
pid, ticks, start_time_readable, name = row.strip().split(' ', 3)
if int(pid) in (0, 4, os.getpid()) or not int(ticks):
if int(pid) in (0, 4, os.getpid()) or not int(ticks) or int(pid) in exclude:
continue
if start_time_readable == '0':
candidates.append((0, name not in preferred, int(ticks), int(pid)))
@@ -99,6 +105,25 @@ def _protected_pid() -> int:
'query rights: ' + ', '.join(rows[:40]))
def _live_protected_owner() -> tuple[int, str]:
"""A protected pid that is STILL alive and CIM-readable at the moment the
marker scenario starts, with its creation time.
On a busy CI runner the scan can pick a short-lived SYSTEM process that
exits (or a WMI hiccup refuses the re-read) before the test uses it —
the pick-and-read pair is retried, excluding each vanished pid, so the
owner is one that actually outlives the test."""
excluded: set[int] = set()
for _ in range(5):
owner = _protected_pid(exclude=excluded)
creation_time = _creation_time(owner)
if creation_time is not None:
return owner, creation_time
excluded.add(owner)
pytest.fail('no live SYSTEM/protected process stayed CIM-readable across attempts: '
+ ', '.join(str(pid) for pid in sorted(excluded)))
def _dead_pid() -> int:
proc = subprocess.Popen([sys.executable, '-c', 'pass'])
proc.wait()
@@ -161,8 +186,8 @@ def test_live_foreign_owner_refuses_and_reused_pid_is_reclaimed(
def test_owner_creation_time_is_read_for_system_processes(tmp_path: Path, ct_matches: bool) -> None:
"""A1: a marker naming a SYSTEM/protected pid (a reused pid after a reboot) is judged by its
real creation time, not treated as live forever because StartTime is access-denied."""
owner = _protected_pid()
ct = _creation_time(owner) if ct_matches else '1000.000'
owner, creation_time = _live_protected_owner()
ct = creation_time if ct_matches else '1000.000'
body = f'{owner}\n{int(time.time())}\nct:{ct}\n'.encode()
(tmp_path / MARKER).write_bytes(body)
pid, code, out = _run(tmp_path, '-SelfTestMarker', '-NoMarkerCleanup')