Skip to content

Commit dfba247

Browse files
jpheinclaude
andcommitted
fix: cross-platform PID check — os.kill(pid, 0) TERMINATES on Windows
Real bug surfaced on CI for this PR. On POSIX, os.kill(pid, 0) is the canonical no-op existence probe. On Windows, Python's os.kill maps to TerminateProcess(handle, sig), which *terminates* the target with exit code sig. os.kill(pid, 0) therefore kills the target with exit code 0 — silently destroying our mine child (or, as happened in test_mine_already_running_live_pid, the pytest process itself). Fix: split into _pid_alive(pid) helper with a Windows branch using ctypes.windll.kernel32.OpenProcess + GetExitCodeProcess. PROCESS_QUERY_LIMITED_INFORMATION opens a handle only if the PID exists; STILL_ACTIVE (259) distinguishes running from exited processes. No new dependencies — stdlib ctypes. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent fe6b889 commit dfba247

1 file changed

Lines changed: 34 additions & 5 deletions

File tree

mempalace/hooks_cli.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,46 @@ def _get_mine_dir(transcript_path: str = "") -> str:
153153
_MINE_PID_FILE = STATE_DIR / "mine.pid"
154154

155155

156+
def _pid_alive(pid: int) -> bool:
157+
"""Cross-platform existence check for a PID.
158+
159+
On POSIX, ``os.kill(pid, 0)`` is the well-known no-op existence probe.
160+
On Windows, ``os.kill`` maps to ``TerminateProcess(handle, sig)`` and
161+
would *terminate* the target process with exit code ``sig`` — using
162+
it here would kill our own mine child (or worse, the caller itself).
163+
Use ``OpenProcess`` + ``GetExitCodeProcess`` via ctypes instead.
164+
"""
165+
if sys.platform == "win32":
166+
import ctypes
167+
from ctypes import wintypes
168+
169+
PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
170+
STILL_ACTIVE = 259
171+
kernel32 = ctypes.windll.kernel32
172+
handle = kernel32.OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, False, pid)
173+
if not handle:
174+
return False
175+
try:
176+
code = wintypes.DWORD()
177+
if not kernel32.GetExitCodeProcess(handle, ctypes.byref(code)):
178+
return False
179+
return code.value == STILL_ACTIVE
180+
finally:
181+
kernel32.CloseHandle(handle)
182+
try:
183+
os.kill(pid, 0)
184+
return True
185+
except (OSError, ValueError):
186+
return False
187+
188+
156189
def _mine_already_running() -> bool:
157190
"""Return True if a background mine process from a previous hook fire is still alive."""
158191
try:
159192
pid = int(_MINE_PID_FILE.read_text().strip())
160-
os.kill(pid, 0) # signal 0 = existence check, no actual signal sent
161-
return True
162193
except (OSError, ValueError):
163-
# OSError covers: FileNotFoundError (no pid file), ProcessLookupError
164-
# (dead PID on POSIX), PermissionError (not our process), and
165-
# WinError 87 / "invalid parameter" (dead or unknown PID on Windows).
166194
return False
195+
return _pid_alive(pid)
167196

168197

169198
def _spawn_mine(cmd: list) -> None:

0 commit comments

Comments
 (0)