Skip to content

fix(gateway): restrict ppid==1 systemd detection to Linux only - #25566

Closed
alaamohanad169-ship-it wants to merge 2 commits into
NousResearch:mainfrom
alaamohanad169-ship-it:fix/systemd-detection-macos
Closed

fix(gateway): restrict ppid==1 systemd detection to Linux only#25566
alaamohanad169-ship-it wants to merge 2 commits into
NousResearch:mainfrom
alaamohanad169-ship-it:fix/systemd-detection-macos

Conversation

@alaamohanad169-ship-it

Copy link
Copy Markdown
Contributor

Summary

Fixes #25508 — When shutdown_forensics.py detects ppid == 1, it assumes the process runs under systemd. On macOS, PID 1 is launchd (Apple's service manager), not systemd. This causes the gateway to incorrectly use systemd-specific shutdown logic on macOS.

Root Cause

In gateway/shutdown_forensics.py line 143:

ctx["under_systemd"] = bool(invocation_id) or ppid == 1

The ppid == 1 check is platform-agnostic, but PID 1 only implies systemd on Linux.

Fix

Restrict the ppid == 1 heuristic to Linux only:

ctx["under_systemd"] = bool(invocation_id) or (sys.platform.startswith("linux") and ppid == 1)

The INVOCATION_ID environment variable check remains platform-independent (it's only set by systemd on Linux, so no false positive on macOS).

Test Plan

  • 3 new tests in tests/gateway/test_shutdown_forensics.py:
    • test_under_systemd_false_on_macos_when_ppid_is_one — macOS + ppid==1 → under_systemd=False
    • test_under_systemd_true_on_linux_when_ppid_is_one — Linux + ppid==1 → under_systemd=True
    • test_under_systemd_invocation_id_overrides_platformINVOCATION_ID always wins
  • All 33 tests in test_shutdown_forensics.py pass (30 existing + 3 new)

The installer creates ~/.hermes/.env via cp or touch, both of which
inherit the process umask. On Ubuntu (umask 0022) the result is 0644;
on some server environments with umask 0002 the result is 0664. No
explicit chmod was applied after file creation, leaving API keys and
tokens visible to other users on the system.

Add chmod 0600 immediately after .env creation in copy_config_templates(),
with a || true fallback for NixOS/containers where the activation script
owns permissions. Also tightens permissions when the file already exists,
so users with 0664 on disk are hardened on next upgrade.

Fixes #25477
When shutdown_forensics.py detects ppid == 1, it assumes the process
runs under systemd. On macOS, PID 1 is launchd, not systemd. This
causes the gateway to incorrectly use systemd-specific shutdown logic
on macOS.

Restrict the ppid == 1 heuristic to Linux only:
  sys.platform.startswith("linux") and ppid == 1

The INVOCATION_ID env var check remains platform-independent (it's only
set by systemd on Linux, so no false positive on macOS).

Fixes #25508

Test plan:
- test_under_systemd_false_on_macos_when_ppid_is_one
- test_under_systemd_true_on_linux_when_ppid_is_one
- test_under_systemd_invocation_id_overrides_platform
- All 33 tests in test_shutdown_forensics.py pass
@alt-glitch alt-glitch added type/bug Something isn't working comp/gateway Gateway runner, session dispatch, delivery duplicate This issue or pull request already exists P3 Low — cosmetic, nice to have labels May 14, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Duplicate of #25525 — same fix restricting ppid == 1 systemd detection to Linux only in shutdown_forensics.py. Also bundles an unrelated scripts/install.sh change (chmod 0600 on .env).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery duplicate This issue or pull request already exists P3 Low — cosmetic, nice to have type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: shutdown_forensics incorrectly sets under_systemd=True on macOS (ppid==1 is launchd, not systemd)

2 participants