Files
strata/strata_control/winproc.py
T
Niko1221 d5be478937 runtime: the managed owner runtime on Windows (loopback TCP + token, job object, byte-range leases)
strata runtime start/status/stop/verify/restart now work on Windows. The owner transport (loopback-only TCP, 256-bit
token, state file in a private directory) was already there; the supervisor around it was fcntl/getuid/killpg only.

- strata_control/winproc.py: LockFileEx byte-range leases (shared and exclusive, on a byte beyond the data so the file
  stays readable) and process identity (OpenProcess + GetProcessTimes). Liveness never uses os.kill(pid, 0): on
  Windows that is TerminateProcess.
- runtime.py: state directory %LOCALAPPDATA%\Strata\runtime with a protected one-entry ACL (existing ones must be
  unshared); owner-only checks through private_files.is_unshared; the state file's address must be a loopback port;
  the owner host is detached with CREATE_NEW_PROCESS_GROUP|CREATE_NO_WINDOW; the server runs in a kill-on-close job
  object (serve/winjob.py, terminate() added), so the whole tree ends with the host however the host ends; a graceful
  stop closes the server's stdin pipe (--owner-fd 0, serve/owner_guard.py turns the EOF into the Ctrl+C drain path);
  the deadline kill ends the job; the ownership marker records the child's creation time, so a PID that was reused
  by another process is not mistaken for the recorded child. A PID from a state file is only compared, never signalled.
- A connect failure to a lost owner is a typed ProfileError (exit 3) instead of a raw OSError.
- `strata serve` and calibration take the activity lease on Windows too.
- tests/terminal/test_runtime.py runs on Windows (32 tests were skipped): a .cmd stands in for the fake engine, the
  signal tests have Windows variants; new tests: a second client attaches from another process, wrong/stale tokens
  cannot stop the owner, a recorded PID of an unrelated live process is never touched, a reused PID with another
  creation time is not the recorded child, the liveness check does not end its target, the directory ACL, shared and
  exclusive leases.
2026-10-15 12:25:58 +02:00

121 lines
5.5 KiB
Python

"""Windows file-lock and process-identity primitives for the managed runtime (ctypes; no pywin32).
POSIX gets these from ``fcntl.flock`` and ``os.kill(pid, 0)``. Neither exists on Windows, and ``os.kill`` there is
``TerminateProcess``: calling it with signal 0 would END the process, so a liveness check must never be written that way.
* ``try_lock`` / ``unlock``: a byte-range lock (``LockFileEx``) on one byte far beyond the data, so the file stays
readable by other processes while a lock is held (a lock that covered the data would make reads fail). It supports
shared locks, which ``msvcrt.locking`` does not, and is released by the OS when the process ends, however it ends.
* ``process_started`` / ``pid_alive``: whether a process exists and when it was created. A PID recorded in a state
file is only a hint; the creation time is what tells the process that was recorded from a later one that reused
the number.
"""
from __future__ import annotations
import os
WINDOWS = os.name == "nt"
#: the locked byte. Beyond any data file the runtime writes (they are all far smaller).
LOCK_OFFSET = 1 << 16
if WINDOWS:
import ctypes
import msvcrt
from ctypes import wintypes
_k32 = ctypes.WinDLL("kernel32", use_last_error=True)
class _Overlapped(ctypes.Structure):
_fields_ = [("Internal", ctypes.c_void_p), ("InternalHigh", ctypes.c_void_p),
("Offset", wintypes.DWORD), ("OffsetHigh", wintypes.DWORD), ("hEvent", wintypes.HANDLE)]
_k32.LockFileEx.argtypes = [wintypes.HANDLE, wintypes.DWORD, wintypes.DWORD, wintypes.DWORD, wintypes.DWORD,
ctypes.POINTER(_Overlapped)]
_k32.LockFileEx.restype = wintypes.BOOL
_k32.UnlockFileEx.argtypes = [wintypes.HANDLE, wintypes.DWORD, wintypes.DWORD, wintypes.DWORD,
ctypes.POINTER(_Overlapped)]
_k32.UnlockFileEx.restype = wintypes.BOOL
_k32.OpenProcess.argtypes = [wintypes.DWORD, wintypes.BOOL, wintypes.DWORD]
_k32.OpenProcess.restype = wintypes.HANDLE
_k32.CloseHandle.argtypes = [wintypes.HANDLE]
_k32.GetExitCodeProcess.argtypes = [wintypes.HANDLE, ctypes.POINTER(wintypes.DWORD)]
_k32.GetExitCodeProcess.restype = wintypes.BOOL
_k32.GetProcessTimes.argtypes = [wintypes.HANDLE] + [ctypes.POINTER(wintypes.FILETIME)] * 4
_k32.GetProcessTimes.restype = wintypes.BOOL
_LOCKFILE_FAIL_IMMEDIATELY = 0x1
_LOCKFILE_EXCLUSIVE_LOCK = 0x2
_ERROR_LOCK_VIOLATION = 33
_ERROR_IO_PENDING = 997
_PROCESS_QUERY_LIMITED_INFORMATION = 0x1000
_STILL_ACTIVE = 259
_ERROR_ACCESS_DENIED = 5
def _overlapped() -> _Overlapped:
o = _Overlapped()
o.Offset = LOCK_OFFSET & 0xFFFFFFFF
o.OffsetHigh = LOCK_OFFSET >> 32
return o
def try_lock(fd: int, *, exclusive: bool) -> bool:
"""Take the lock without waiting. False when another holder has it."""
handle = msvcrt.get_osfhandle(fd)
flags = _LOCKFILE_FAIL_IMMEDIATELY | (_LOCKFILE_EXCLUSIVE_LOCK if exclusive else 0)
if _k32.LockFileEx(handle, flags, 0, 1, 0, ctypes.byref(_overlapped())):
return True
code = ctypes.get_last_error()
if code in (_ERROR_LOCK_VIOLATION, _ERROR_IO_PENDING):
return False
raise ctypes.WinError(code, "LockFileEx")
def unlock(fd: int) -> None:
_k32.UnlockFileEx(msvcrt.get_osfhandle(fd), 0, 1, 0, ctypes.byref(_overlapped()))
def process_started(pid: int) -> int | None:
"""The creation time (100 ns ticks since 1601) of a running process, or None when there is no such
process, it has exited, or its times cannot be read."""
if type(pid) is not int or pid <= 0:
return None
handle = _k32.OpenProcess(_PROCESS_QUERY_LIMITED_INFORMATION, False, pid)
if not handle:
return None
try:
code = wintypes.DWORD()
if not _k32.GetExitCodeProcess(handle, ctypes.byref(code)) or code.value != _STILL_ACTIVE:
return None
created, exited, kernel, user = (wintypes.FILETIME() for _ in range(4))
if not _k32.GetProcessTimes(handle, ctypes.byref(created), ctypes.byref(exited),
ctypes.byref(kernel), ctypes.byref(user)):
return None
return (created.dwHighDateTime << 32) | created.dwLowDateTime
finally:
_k32.CloseHandle(handle)
def pid_alive(pid: int, started: int | None = None) -> bool:
"""Whether `pid` is a running process, and, when `started` is given, the one created at that time."""
if type(pid) is not int or pid <= 0:
return False
now = process_started(pid)
if now is None:
# OpenProcess can be refused for a protected process that does exist; treat that as present.
handle = _k32.OpenProcess(_PROCESS_QUERY_LIMITED_INFORMATION, False, pid)
if handle:
_k32.CloseHandle(handle)
return False # opened, but exited or unreadable: not running
return ctypes.get_last_error() == _ERROR_ACCESS_DENIED
return started is None or now == started
else:
def try_lock(fd: int, *, exclusive: bool) -> bool:
raise OSError("Windows only")
def unlock(fd: int) -> None:
raise OSError("Windows only")
def process_started(pid: int) -> int | None:
return None
def pid_alive(pid: int, started: int | None = None) -> bool:
raise OSError("Windows only")