Skip to content

The agent that grows with you

fix(update): Windows process probes build their ctypes bindings once

kshitijk4poor8fc50f055167
diff --git a/hermes_cli/update_lock.py b/hermes_cli/update_lock.py
index 247641b3885..aeaead5f271 100644
--- a/hermes_cli/update_lock.py
+++ b/hermes_cli/update_lock.py
@@ -154,7 +154,10 @@ def _pid_alive(pid: int) -> bool:
# --- process creation time ---------------------------------------------------------------
+@cache
def _windows_kernel32():
+ """kernel32 with OpenProcess/GetProcessTimes/CloseHandle typed, built once: update-fence polls
+ reach it whenever psutil cannot read a marker pid's creation time."""
import ctypes
from ctypes import wintypes
@@ -364,11 +367,13 @@ def _handoff_pid() -> int | None:
return pid if pid > 0 else None
-def _windows_parent_pid(pid: int) -> int | None:
- """The parent of ``pid`` from a Toolhelp32 process snapshot (stdlib ctypes).
+@cache
+def _windows_toolhelp_api():
+ """kernel32 with the Toolhelp32 process walk typed, plus PROCESSENTRY32W, built once.
- Windows keeps a dead parent's pid in the snapshot and reuses pids, so, like
- psutil, a "parent" created after the child is a recycled pid, not our parent.
+ Like :func:`_windows_lockfile_api`: a struct built per call leaves its ``ctypes.POINTER`` in
+ ctypes' pointer-type cache forever before Python 3.14. A private DLL instance, so the walk's
+ prototypes always name the struct class returned with them.
"""
import ctypes
from ctypes import wintypes
@@ -382,13 +387,26 @@ def _windows_parent_pid(pid: int) -> int | None:
("dwFlags", wintypes.DWORD), ("szExeFile", ctypes.c_wchar * 260),
]
- kernel32 = _windows_kernel32()
+ kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
kernel32.CreateToolhelp32Snapshot.argtypes = [wintypes.DWORD, wintypes.DWORD]
kernel32.CreateToolhelp32Snapshot.restype = wintypes.HANDLE
for walk in (kernel32.Process32FirstW, kernel32.Process32NextW):
walk.argtypes = [wintypes.HANDLE, ctypes.POINTER(PROCESSENTRY32W)]
walk.restype = wintypes.BOOL
+ kernel32.CloseHandle.argtypes = [wintypes.HANDLE]
+ kernel32.CloseHandle.restype = wintypes.BOOL
+ return kernel32, PROCESSENTRY32W
+
+
+def _windows_parent_pid(pid: int) -> int | None:
+ """The parent of ``pid`` from a Toolhelp32 process snapshot (stdlib ctypes).
+
+ Windows keeps a dead parent's pid in the snapshot and reuses pids, so, like
+ psutil, a "parent" created after the child is a recycled pid, not our parent.
+ """
+ import ctypes
+ kernel32, PROCESSENTRY32W = _windows_toolhelp_api()
snapshot = kernel32.CreateToolhelp32Snapshot(0x2, 0) # TH32CS_SNAPPROCESS
if not snapshot or snapshot == ctypes.c_void_p(-1).value:
return None
diff --git a/tests/hermes_cli/test_update_lock.py b/tests/hermes_cli/test_update_lock.py
index 9e2855c22fa..77d8ea1e0d8 100644
--- a/tests/hermes_cli/test_update_lock.py
+++ b/tests/hermes_cli/test_update_lock.py
@@ -1024,3 +1024,24 @@ def test_whole_second_creation_time_in_our_own_second_is_us(marker):
assert update_lock.judge_marker(marker.read_bytes())[0] == "ours"
# A whole second from an EARLIER second is a killed update's pid reused: still not us.
assert update_lock.incarnation_live(os.getpid(), float(math.floor(own)) - 1) is False
+
+
+def test_repeated_windows_process_probes_build_no_new_ctypes_objects(monkeypatch):
+ """Fence polls read a marker pid's creation time through GetProcessTimes whenever psutil
+ cannot, and the psutil-free takeover child walks Toolhelp32 for its parent. Once warm, neither
+ builds a DLL wrapper or a pointer type again (before Python 3.14 a pointer type to a fresh
+ struct stays in ctypes' cache forever)."""
+ import ctypes
+
+ created = update_lock._windows_create_filetime(os.getpid())
+ parent = update_lock._windows_parent_pid(os.getpid())
+ assert created and parent
+ built = []
+ real_pointer, real_dll = ctypes.POINTER, ctypes.WinDLL
+ monkeypatch.setattr(ctypes, "POINTER", lambda cls: built.append(cls) or real_pointer(cls))
+ monkeypatch.setattr(ctypes, "WinDLL", lambda *a, **k: built.append(a) or real_dll(*a, **k))
+ for _ in range(20):
+ assert update_lock._windows_create_filetime(os.getpid()) == created
+ assert update_lock._windows_parent_pid(os.getpid()) == parent
+ assert built == []