diff --git a/src/aipass/devpulse/apps/handlers/watchdog/registry.py b/src/aipass/devpulse/apps/handlers/watchdog/registry.py index a237dbf7..60b95f8d 100644 --- a/src/aipass/devpulse/apps/handlers/watchdog/registry.py +++ b/src/aipass/devpulse/apps/handlers/watchdog/registry.py @@ -173,10 +173,45 @@ def _is_zombie_linux(pid: int) -> bool: return False +def _pid_alive_windows(pid: int) -> bool: + """Windows-safe liveness via OpenProcess + GetExitCodeProcess (mirrors agent.py).""" + import ctypes + from ctypes import wintypes + + PROCESS_QUERY_LIMITED_INFORMATION = 0x1000 + STILL_ACTIVE = 259 + + kernel32 = ctypes.windll.kernel32 # type: ignore[attr-defined] # Windows-only + kernel32.OpenProcess.argtypes = [wintypes.DWORD, wintypes.BOOL, wintypes.DWORD] + kernel32.OpenProcess.restype = wintypes.HANDLE + kernel32.GetExitCodeProcess.argtypes = [wintypes.HANDLE, ctypes.POINTER(wintypes.DWORD)] + kernel32.GetExitCodeProcess.restype = wintypes.BOOL + kernel32.CloseHandle.argtypes = [wintypes.HANDLE] + kernel32.CloseHandle.restype = wintypes.BOOL + + handle = kernel32.OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, False, pid) + if not handle: + return False + try: + exit_code = wintypes.DWORD() + if not kernel32.GetExitCodeProcess(handle, ctypes.byref(exit_code)): + return False + return exit_code.value == STILL_ACTIVE + finally: + kernel32.CloseHandle(handle) + + def is_pid_alive(pid: int) -> bool: - """Return True if the process exists and is not a zombie.""" + """Return True if the process exists and is not a zombie. Windows-safe.""" if not isinstance(pid, int) or pid <= 0: return False + if sys.platform == "win32": + # os.kill(pid, 0) TERMINATES the target on Windows — use OpenProcess. + try: + return _pid_alive_windows(pid) + except Exception as exc: + logger.info("[watchdog.registry] PID %s Windows check failed (assuming alive): %s", pid, exc) + return True try: os.kill(pid, 0) except ProcessLookupError as exc: