From 228f8f78073dc4ed0e63fff01c19596c50115d40 Mon Sep 17 00:00:00 2001 From: Xu Lingrui Date: Wed, 3 Jun 2026 00:02:55 +0800 Subject: [PATCH] fix: Windows PID check --- openspace/communication/gateway_runtime.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/openspace/communication/gateway_runtime.py b/openspace/communication/gateway_runtime.py index 60c92cb..0d48928 100644 --- a/openspace/communication/gateway_runtime.py +++ b/openspace/communication/gateway_runtime.py @@ -38,14 +38,30 @@ def _is_pid_alive(pid: int) -> bool: try: if sys.platform == "win32": import ctypes - kernel32 = ctypes.windll.kernel32 + from ctypes import wintypes + + kernel32 = ctypes.WinDLL("kernel32", use_last_error=True) PROCESS_QUERY_LIMITED_INFORMATION = 0x1000 STILL_ACTIVE = 259 + ERROR_ACCESS_DENIED = 5 + + 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: + if ctypes.get_last_error() == ERROR_ACCESS_DENIED: + return True return False try: - exit_code = ctypes.c_ulong() + exit_code = wintypes.DWORD() if kernel32.GetExitCodeProcess(handle, ctypes.byref(exit_code)): return exit_code.value == STILL_ACTIVE return False