#684 (devpulse): registry.py is_pid_alive Windows-guards its os.kill(pid,0) — win32 early-returns to OpenProcess+GetExitCodeProcess (os.kill(pid,0)=TerminateProcess on Windows, KILLS the target). #682 checker no longer flags it; 26 registry tests green. git_lock_tool.py:120 deferred to a separate cleanup (pre-existing tool debt: 42 prints/no-meta/architecture fail the gate).
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user