fix(gateway): route delivery-ledger owner-liveness probe through _pid_exists (#41662)
The `_owner_alive` fallback in gateway/delivery_ledger.py (taken whenever a process start time is unreadable) probed liveness with a raw `os.kill(pid, 0)`. On Windows that is NOT a no-op: CPython maps sig=0 to `GenerateConsoleCtrlEvent(0, pid)` (bpo-14484), so probing a LIVE pid whose start time psutil could not read would Ctrl+C the target's entire console group. The prior `# windows-footgun: ok` annotation only justified the EPERM-means-alive exception semantics, not the Ctrl+C side effect. Route the probe through `gateway.status._pid_exists` (psutil-first, ctypes OpenProcess fallback on Windows), preserving EPERM-means-alive. A POSIX-only raw-probe fallback remains for the unreachable case where gateway.status cannot be imported; on Windows that path reports dead rather than firing a sig-0 probe. Tests patch `gateway.status._pid_exists` per the windows-native-support pattern, including a regression guard asserting os.kill is never used for the probe. Sabotage-verified (revert → red, restore → green). Part of #41662 (the os.kill half; watchdog half tracked separately).
This commit is contained in:
@@ -158,16 +158,32 @@ def _owner_alive(pid: Any, started_at: Any) -> bool:
|
||||
current_start = None
|
||||
if current_start is None:
|
||||
# No such process (or unreadable) — treat unreadable-but-extant
|
||||
# processes as alive only if the pid exists.
|
||||
# processes as alive only if the pid exists. Route through the
|
||||
# cross-platform probe: ``os.kill(pid, 0)`` on Windows is NOT a
|
||||
# no-op (bpo-14484 — CPython maps sig=0 to
|
||||
# ``GenerateConsoleCtrlEvent(0, pid)``), so a raw probe here could
|
||||
# Ctrl+C the gateway's own console group whenever psutil failed to
|
||||
# read the start time of a live pid. ``_pid_exists`` keeps the
|
||||
# EPERM-means-alive semantics (exists but owned by another user).
|
||||
try:
|
||||
os.kill(pid, 0) # windows-footgun: ok — EPERM counts as alive below
|
||||
except ProcessLookupError:
|
||||
return False
|
||||
except PermissionError:
|
||||
from gateway.status import _pid_exists
|
||||
except Exception:
|
||||
if os.name == "nt":
|
||||
# Never fall back to a raw sig-0 probe on Windows.
|
||||
return False
|
||||
try:
|
||||
os.kill(pid, 0) # windows-footgun: ok — POSIX-only fallback branch
|
||||
except ProcessLookupError:
|
||||
return False
|
||||
except PermissionError:
|
||||
return True
|
||||
except OSError:
|
||||
return False
|
||||
return True
|
||||
except OSError:
|
||||
try:
|
||||
return bool(_pid_exists(pid))
|
||||
except Exception:
|
||||
return False
|
||||
return True
|
||||
if started_at is None:
|
||||
return True
|
||||
try:
|
||||
|
||||
@@ -297,3 +297,59 @@ class TestUnconnectedPlatformKeepsItsBudget:
|
||||
)
|
||||
assert _row("ob-1")["attempts"] == 0
|
||||
|
||||
|
||||
|
||||
class TestOwnerAlivePidProbe:
|
||||
"""_owner_alive's no-start-time fallback must route through
|
||||
gateway.status._pid_exists, never a raw ``os.kill(pid, 0)`` probe.
|
||||
|
||||
On Windows ``os.kill(pid, 0)`` is NOT a no-op: CPython maps sig=0 to
|
||||
``GenerateConsoleCtrlEvent(0, pid)`` (bpo-14484), so probing a LIVE pid
|
||||
whose start time psutil could not read would Ctrl+C its console group.
|
||||
Pattern per the windows-native-support reference: patch
|
||||
``gateway.status._pid_exists``, not ``os.kill``.
|
||||
"""
|
||||
|
||||
def _no_start_time(self, monkeypatch):
|
||||
from gateway import status
|
||||
|
||||
monkeypatch.setattr(status, "get_process_start_time", lambda pid: None)
|
||||
|
||||
def test_alive_when_pid_exists(self, monkeypatch):
|
||||
from gateway import status
|
||||
|
||||
self._no_start_time(monkeypatch)
|
||||
monkeypatch.setattr(status, "_pid_exists", lambda pid: True)
|
||||
assert dl._owner_alive(12345, 999) is True
|
||||
|
||||
def test_dead_when_pid_gone(self, monkeypatch):
|
||||
from gateway import status
|
||||
|
||||
self._no_start_time(monkeypatch)
|
||||
monkeypatch.setattr(status, "_pid_exists", lambda pid: False)
|
||||
assert dl._owner_alive(12345, 999) is False
|
||||
|
||||
def test_raw_os_kill_probe_never_used(self, monkeypatch):
|
||||
"""Regression guard: the probe must not touch os.kill when
|
||||
gateway.status._pid_exists is importable (i.e. always in-tree)."""
|
||||
from gateway import status
|
||||
|
||||
self._no_start_time(monkeypatch)
|
||||
calls = []
|
||||
monkeypatch.setattr(status, "_pid_exists", lambda pid: calls.append(pid) or True)
|
||||
monkeypatch.setattr(
|
||||
dl.os, "kill", lambda *a, **k: (_ for _ in ()).throw(AssertionError("raw os.kill probe used"))
|
||||
)
|
||||
assert dl._owner_alive(4242, 999) is True
|
||||
assert calls == [4242]
|
||||
|
||||
def test_probe_exception_means_dead(self, monkeypatch):
|
||||
from gateway import status
|
||||
|
||||
self._no_start_time(monkeypatch)
|
||||
|
||||
def boom(pid):
|
||||
raise RuntimeError("probe blew up")
|
||||
|
||||
monkeypatch.setattr(status, "_pid_exists", boom)
|
||||
assert dl._owner_alive(12345, 999) is False
|
||||
|
||||
Reference in New Issue
Block a user