fix: prevent systemd restart loop from stale/colliding PID files - #582
Closed
Bartok9 wants to merge 1 commit into
Closed
fix: prevent systemd restart loop from stale/colliding PID files#582Bartok9 wants to merge 1 commit into
Bartok9 wants to merge 1 commit into
Conversation
Fixes NousResearch#576 This PR addresses the systemd restart loop issue caused by: 1. Stale PID files left behind after gateway crash (SIGKILL) 2. PID collision when a different process reuses the same PID number Changes: - Enhanced get_running_pid() to verify the process is actually a hermes gateway, not just any process with that PID - Added _is_hermes_gateway_process() helper that checks /proc/{pid}/cmdline on Linux and uses 'ps' on macOS - Added --replace flag for 'hermes gateway run' to force takeover of existing PID locks - Updated systemd unit template to use --replace for idempotent startup - Updated launchd plist template similarly - Added StartLimitIntervalSec/StartLimitBurst to prevent aggressive restart loops - Added ExecStop directive for cleaner service shutdown - Added tests for PID file handling The key insight: when checking if a gateway is already running, we now verify both that the PID exists AND that it's actually a hermes gateway process. This prevents false positives from PID reuse after crashes.
Contributor
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.
Fixes #576
Problem
When Hermes is run under systemd, the service can enter restart loops due to:
Solution
1. Robust PID verification
Enhanced
get_running_pid()to verify the process is actually a hermes gateway, not just any process with that PID. The new_is_hermes_gateway_process()helper:/proc/{pid}/cmdlinedirectly (fast, no subprocess)pscommand2. Added
--replaceflagNew
hermes gateway run --replaceoption that:3. Improved service templates
systemd unit:
--replaceby defaultExecStopfor clean shutdownStartLimitIntervalSec/StartLimitBurstto prevent aggressive restart loopsAfter=network.targettoAfter=network-online.targetRestartSec=15(was 10)HERMES_HOMEenvironment variablelaunchd plist:
--replaceby defaultThrottleInterval=15HERMES_HOMEenvironment variable4. Better error messages
When duplicate instance is detected, the error message now suggests:
hermes gateway run --replacefor force takeoverhermes gateway install --forcefor service reinstallationTesting
Added
tests/gateway/test_status.pywith tests for:All tests pass.
For users experiencing this issue
After updating, reinstall the service to get the new template:
Or manually start with: