fix: exclude ancestor PIDs from gateway process scan (#13242)#19146
Closed
cixuuz wants to merge 1 commit into
Closed
fix: exclude ancestor PIDs from gateway process scan (#13242)#19146cixuuz wants to merge 1 commit into
cixuuz wants to merge 1 commit into
Conversation
) _scan_gateway_pids() uses ps-based pattern matching to find running gateways. When invoked from the CLI (e.g. `hermes gateway status`), the calling process itself matches gateway patterns, causing false positives — the CLI is mistakenly counted as a running gateway. Add _get_ancestor_pids() that walks the process tree from the current PID up to init (PID 1). Merge this set into exclude_pids at the top of _scan_gateway_pids() so the entire ancestor chain is filtered out. This complements the existing os.getpid() exclusion in _append_unique_pid() by also covering parent/grandparent processes (e.g. when hermes is invoked via a wrapper script or shell). Closes NousResearch#13242
Collaborator
Contributor
|
Salvaged via #19586 onto current main - your commit authorship was preserved. Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #13242 —
_scan_gateway_pids()false-positives on the calling CLI process.Problem
_scan_gateway_pids()usespspattern matching to find running gateways. When invoked from the CLI (e.g.hermes gateway status), the calling process command line containshermes gateway, matching the scan patterns. While_append_unique_pid()already excludesos.getpid(), it does not exclude parent/ancestor processes — so wrapper scripts, shell invocations, or nested process trees can still produce false positives.Fix
_get_ancestor_pids()that walks the process tree from the current PID up to init (PID 1), capped at 64 iterations._scan_gateway_pids(), merge the ancestor set intoexclude_pidsso the entire chain is filtered out before any pattern matching.This is a hardening fix — the primary duplicate-instance guard in
gateway/run.pyalready uses PID-file-based detection (get_running_pid()), so the self-detection issue mostly manifests in status/stop paths that fall back to process scanning.Testing
_get_parent_pid()correctly walks the chain on Linux (uses/proc/{pid}/statuswithps -o ppid=fallback)._is_pid_ancestor_of_current_process()helper (used by_request_gateway_self_restart) validates the same walk logic.