fix: Windows compatibility for _is_pid_alive in communication gateway

- Use Windows API (OpenProcess/GetExitCodeProcess) on Windows platform
- Fall back to os.kill(pid, 0) on Unix-like systems
- Fixes #90

The os.kill(pid, 0) approach fails on Windows with OSError [WinError 87],
preventing the communication gateway from starting on Windows.
This commit is contained in:
cbsincool 2026-05-27 11:42:01 +08:00
parent 25b98602f2
commit fbf494a3e3

View file

@ -36,7 +36,23 @@ def _is_pid_alive(pid: int) -> bool:
if pid <= 0:
return False
try:
os.kill(pid, 0)
if sys.platform == "win32":
import ctypes
kernel32 = ctypes.windll.kernel32
PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
STILL_ACTIVE = 259
handle = kernel32.OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, False, pid)
if not handle:
return False
try:
exit_code = ctypes.c_ulong()
if kernel32.GetExitCodeProcess(handle, ctypes.byref(exit_code)):
return exit_code.value == STILL_ACTIVE
return False
finally:
kernel32.CloseHandle(handle)
else:
os.kill(pid, 0)
except ProcessLookupError:
return False
except PermissionError: