fix(gateway): ignore invoking CLI ancestor during PID scan

This commit is contained in:
刘昊 2026-04-23 06:14:21 +08:00
parent a1d57292af
commit 9f7f9aafab
2 changed files with 27 additions and 0 deletions

View file

@ -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 []

View file

@ -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)