Skip to content

The agent that grows with you

fix(windows): stop psutil liveness probes from holding the GIL

zwmaupin726ea2573361
diff --git a/gateway/status.py b/gateway/status.py
index be08be0b5ff..e23f140a44d 100644
--- a/gateway/status.py
+++ b/gateway/status.py
@@ -945,13 +945,18 @@ def _pid_exists(pid: int) -> bool:
psutil, then ctypes ``OpenProcess`` (Windows) / ``os.kill(pid, 0)`` (POSIX). Zombies report
dead: treating one as alive makes --replace wait forever under systemd Restart=always."""
pid = int(pid)
+ if os.name == "nt":
+ # psutil's Windows liveness path (psutil_pid_is_running -> EnumProcesses, and
+ # Process.status -> NtQuerySystemInformation) does NOT release the GIL, so one slow native
+ # call freezes every other Python thread in the process — the Desktop backend and gateway
+ # stall for minutes under load (#142085). The ctypes probe below releases the GIL and is
+ # cheaper. Windows has no POSIX zombies, so the status read this skips did not apply here.
+ return pid_exists_stdlib(pid)
try:
import psutil # type: ignore
# Best-effort zombie check: status-read failures fall through to pid_exists().
- # Windows has no POSIX zombies, and this probe costs ~7 ms per call — once per
- # registry entry inside the session file lock (#115578). Skip it on Windows and
- # let pid_exists() below (or the ctypes fallback) decide.
- probe_zombie = os.name != "nt"
+ # This probe is POSIX-only (the Windows branch returned above), where psutil's status read
+ # is cheap and the zombie distinction is the whole point.
try:
# A zombie (defunct) process is still in the process table, so ``psutil.pid_exists()`` returns
# True for it — but it is already dead: SIGKILL has no effect and it cannot be a running
@@ -961,7 +966,7 @@ def _pid_exists(pid: int) -> bool:
# #42126). Report zombies as dead so the takeover proceeds. Best-effort: any failure to read
# status (partial/stub psutil, access denied, transient race) falls through to the authoritative
# ``pid_exists()`` below rather than raising.
- if probe_zombie and psutil.Process(pid).status() == psutil.STATUS_ZOMBIE:
+ if psutil.Process(pid).status() == psutil.STATUS_ZOMBIE:
return False
except getattr(psutil, "NoSuchProcess", ()):
return False
diff --git a/hermes_cli/process_identity.py b/hermes_cli/process_identity.py
index 76ba688c731..6f4a48d7f56 100644
--- a/hermes_cli/process_identity.py
+++ b/hermes_cli/process_identity.py
@@ -304,6 +304,14 @@ def _pid_alive_matches(
proc = psutil.Process(int(pid))
# ``strict`` is positive proof (an exact epoch birth); a clock step only withholds it.
if strict:
+ # Windows: skip the status() read entirely. psutil's Windows status path
+ # (NtQuerySystemInformation) does not release the GIL, so under load it freezes every
+ # other Python thread in the process for minutes (#142085) — and Windows has no POSIX
+ # zombies, so the STATUS_ZOMBIE comparison had nothing to contribute there anyway. The
+ # non-strict branch right below already makes this exact skip.
+ if os.name == "nt":
+ return (create_time is not None and process_birth(proc) == create_time
+ and proc.is_running())
return (create_time is not None and process_birth(proc) == create_time
and proc.is_running() and proc.status() != psutil.STATUS_ZOMBIE)
# A zombie keeps its create_time until its parent reaps it, but it is already dead: the
diff --git a/tests/gateway/test_status.py b/tests/gateway/test_status.py
index 163160dcaac..1fdf4892d4a 100644
--- a/tests/gateway/test_status.py
+++ b/tests/gateway/test_status.py
@@ -742,6 +742,28 @@ class TestPidExistsZombieProbe:
assert status._pid_exists(os.getpid()) is True
assert calls == []
+ @pytest.mark.platforms("windows")
+ def test_windows_never_touches_psutil_at_all(self, monkeypatch):
+ """#142085: psutil's Windows liveness path does not release the GIL, so one slow
+ ``EnumProcesses``/``NtQuerySystemInformation`` call freezes every other Python thread in
+ the process (Desktop backend and gateway stall for minutes under load). The Windows
+ branch must go straight to the ctypes probe and never import psutil."""
+ psutil = pytest.importorskip("psutil")
+ calls = []
+ real_pid_exists = psutil.pid_exists
+
+ def spy(pid):
+ calls.append(pid)
+ return real_pid_exists(pid)
+
+ monkeypatch.setattr(psutil, "pid_exists", spy)
+ # Also assert Process.status was never reached (the zombie probe is POSIX-only anyway).
+ self._spy_status(monkeypatch)
+
+ assert status._pid_exists(os.getpid()) is True
+ assert status._pid_exists(999999) is False
+ assert calls == []
+
@pytest.mark.platforms("linux")
def test_posix_still_probes_zombie_status(self, monkeypatch):
# Control: on POSIX a zombie still answers pid_exists(), so the probe must survive.
diff --git a/tests/hermes_cli/test_process_identity.py b/tests/hermes_cli/test_process_identity.py
index 5435374f84a..c6f8cf1638d 100644
--- a/tests/hermes_cli/test_process_identity.py
+++ b/tests/hermes_cli/test_process_identity.py
@@ -263,6 +263,49 @@ def test_spawner_is_dead_tristate():
assert pi.spawner_is_dead(_entry(1, 1.0)) is None
+class TestStrictBranchStatusRead:
+ """#142085: the strict branch of ``_pid_alive_matches`` read ``Process.status()`` on Windows,
+ where psutil's NtQuerySystemInformation path does not release the GIL — under load that froze
+ every other Python thread in the process for minutes. The non-strict branch already skipped
+ the read on Windows; the strict branch must too (Windows has no POSIX zombies, so the
+ STATUS_ZOMBIE comparison had nothing to contribute there). Tested on the real host: faking
+ ``os.name`` on POSIX proves nothing about the cost on Windows."""
+
+ @staticmethod
+ def _spy_status(monkeypatch):
+ psutil = pytest.importorskip("psutil")
+ calls = []
+ real_status = psutil.Process.status
+
+ def spy(self):
+ calls.append(self.pid)
+ return real_status(self)
+
+ monkeypatch.setattr(psutil.Process, "status", spy)
+ return calls
+
+ @pytest.mark.platforms("windows")
+ def test_windows_strict_branch_skips_status(self, monkeypatch):
+ calls = self._spy_status(monkeypatch)
+ psutil = pytest.importorskip("psutil")
+ pid = os.getpid()
+ create_time = pi.process_birth(psutil.Process(pid))
+
+ assert pi._pid_alive_matches(pid, create_time, strict=True) is True
+ assert calls == []
+
+ @pytest.mark.platforms("posix")
+ def test_posix_strict_branch_still_probes_status(self, monkeypatch):
+ # Control: on POSIX a zombie keeps its create_time, so the status read is load-bearing.
+ calls = self._spy_status(monkeypatch)
+ psutil = pytest.importorskip("psutil")
+ pid = os.getpid()
+ create_time = pi.process_birth(psutil.Process(pid))
+
+ assert pi._pid_alive_matches(pid, create_time, strict=True) is True
+ assert calls == [pid]
+
+
# ---------------------------------------------------------------------------
# Updater rung: _ledger_reapable_backend_pids
# ---------------------------------------------------------------------------