From 9f7f9aafabc393dc4746831825be01cdecfd0468 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=98=8A?= Date: Thu, 23 Apr 2026 06:14:21 +0800 Subject: [PATCH] fix(gateway): ignore invoking CLI ancestor during PID scan --- hermes_cli/gateway.py | 2 ++ .../hermes_cli/test_update_gateway_restart.py | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/hermes_cli/gateway.py b/hermes_cli/gateway.py index 59bd37d11..f3199cabc 100644 --- a/hermes_cli/gateway.py +++ b/hermes_cli/gateway.py @@ -280,6 +280,8 @@ def _scan_gateway_pids(exclude_pids: set[int], all_profiles: bool = False) -> li if any(pattern in command for pattern in patterns) and ( all_profiles or _matches_current_profile(command) ): + if _is_pid_ancestor_of_current_process(pid): + continue _append_unique_pid(pids, pid, exclude_pids) except (OSError, subprocess.TimeoutExpired): return [] diff --git a/tests/hermes_cli/test_update_gateway_restart.py b/tests/hermes_cli/test_update_gateway_restart.py index 2a2bc962d..666b1b78b 100644 --- a/tests/hermes_cli/test_update_gateway_restart.py +++ b/tests/hermes_cli/test_update_gateway_restart.py @@ -819,6 +819,31 @@ class TestFindGatewayPidsExclude: assert pids == [100] + def test_excludes_ancestor_process_tree(self, monkeypatch): + monkeypatch.setattr(gateway_cli, "is_windows", lambda: False) + monkeypatch.setattr(gateway_cli, "_get_service_pids", lambda: set()) + + def fake_run(cmd, **kwargs): + return subprocess.CompletedProcess( + cmd, 0, + stdout=( + "300 /usr/bin/python -m hermes_cli.main gateway run --replace\n" + "200 /usr/bin/python /repo/gateway/run.py\n" + ), + stderr="", + ) + + parent_map = {500: 400, 400: 300, 300: 1} + + monkeypatch.setattr(gateway_cli.subprocess, "run", fake_run) + monkeypatch.setattr("os.getpid", lambda: 500) + monkeypatch.setattr(gateway_cli, "_get_parent_pid", lambda pid: parent_map.get(pid)) + + pids = gateway_cli.find_gateway_pids() + + assert 300 not in pids + assert pids == [200] + # --------------------------------------------------------------------------- # Gateway mode writes exit code before restart (#8300)