Skip to content

The agent that grows with you

test: await the never-resumed update child's death on a process handle

Teknium781f8bbd7819
diff --git a/tests/scripts/desktop_update/test_desktop_update_windows_marker_lock.py b/tests/scripts/desktop_update/test_desktop_update_windows_marker_lock.py
index c29f39b8a0d..b8de57ef924 100644
--- a/tests/scripts/desktop_update/test_desktop_update_windows_marker_lock.py
+++ b/tests/scripts/desktop_update/test_desktop_update_windows_marker_lock.py
@@ -17,7 +17,7 @@ from tests.scripts.desktop_update.windows_handoff_support import (
POWERSHELL,
HOLD_CLI,
_creation_time,
- _alive,
+ _ProcessWatch,
_dead_pid,
_script,
_finish,
@@ -137,12 +137,16 @@ def test_script_killed_before_publishing_the_delegate_runs_no_update(tmp_path: P
'-Parent', str(script.pid), '-Seconds', '60'],
capture_output=True, text=True, timeout=90, check=False)
assert child.returncode == 0, 'the update child never appeared'
- child_pid = int(child.stdout)
- subprocess.run(['taskkill', '/F', '/PID', str(script.pid)], capture_output=True, check=True)
- script.wait(timeout=30)
- time.sleep(3)
+ watch = _ProcessWatch(int(child.stdout))
+ try:
+ subprocess.run(['taskkill', '/F', '/PID', str(script.pid)], capture_output=True, check=True)
+ script.wait(timeout=30)
+ # The script's death closes the child's job; kill-on-close then terminates the child
+ # asynchronously, so await its exit. Once it is gone it can never write `ran`.
+ assert watch.exited_within(60), 'the never-resumed update child outlived its script'
+ finally:
+ watch.close()
assert not ran.exists(), 'the update child ran before it was published as the delegate'
- assert not _alive(child_pid), 'the never-resumed update child outlived its script'
lines = marker.read_bytes().decode().split('\n')
assert lines[0] == _custodian(home, script.pid), lines
assert not any(line.startswith('delegate:') for line in lines), lines
diff --git a/tests/scripts/desktop_update/windows_handoff_support.py b/tests/scripts/desktop_update/windows_handoff_support.py
index d0b7a86a79e..8cdb9600371 100644
--- a/tests/scripts/desktop_update/windows_handoff_support.py
+++ b/tests/scripts/desktop_update/windows_handoff_support.py
@@ -47,9 +47,29 @@ def _creation_time(pid: int) -> str:
return f'{int(out) / 1000:.3f}'
-def _alive(pid: int) -> bool:
- return str(pid) in subprocess.run(['tasklist', '/FI', f'PID eq {pid}', '/NH'],
- capture_output=True, text=True, check=False).stdout
+class _ProcessWatch:
+ """A SYNCHRONIZE handle on a process, opened while it lives. The handle pins the pid (Windows
+ cannot hand it to a new process while it is open) and is signalled exactly when the process
+ has exited, so a death that lands asynchronously (a job's kill-on-close) is awaited, not
+ guessed at with a sleep and a pid probe that a recycled pid or a lingering entry fools."""
+
+ def __init__(self, pid: int) -> None:
+ import ctypes
+ from ctypes import wintypes
+ self._k = ctypes.WinDLL('kernel32', use_last_error=True)
+ self._k.OpenProcess.argtypes = [wintypes.DWORD, wintypes.BOOL, wintypes.DWORD]
+ self._k.OpenProcess.restype = wintypes.HANDLE
+ self._k.WaitForSingleObject.argtypes = [wintypes.HANDLE, wintypes.DWORD]
+ self._k.WaitForSingleObject.restype = wintypes.DWORD
+ self._k.CloseHandle.argtypes = [wintypes.HANDLE]
+ self._handle = self._k.OpenProcess(0x00100000 | 0x1000, False, pid) # SYNCHRONIZE | QUERY_LIMITED
+ assert self._handle, f'could not open pid {pid} (winerror={ctypes.get_last_error()})'
+
+ def exited_within(self, seconds: float) -> bool:
+ return self._k.WaitForSingleObject(self._handle, int(seconds * 1000)) == 0 # WAIT_OBJECT_0
+
+ def close(self) -> None:
+ self._k.CloseHandle(self._handle)
def _dead_pid() -> int: