Skip to content

The agent that grows with you

fix(gateway): route delivery-ledger owner-liveness probe through _pid_exists (#41662)

Teknium11ccbb4b6f8b
diff --git a/gateway/delivery_ledger.py b/gateway/delivery_ledger.py
index e17f92b8ab4..a0b5c617831 100644
--- a/gateway/delivery_ledger.py
+++ b/gateway/delivery_ledger.py
@@ -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:
diff --git a/tests/gateway/test_delivery_ledger.py b/tests/gateway/test_delivery_ledger.py
index f9421f7e6ba..10fb86867c2 100644
--- a/tests/gateway/test_delivery_ledger.py
+++ b/tests/gateway/test_delivery_ledger.py
@@ -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