process_supervisor.sh 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/bin/sh
  2. # Hermes process supervisor: one per Hermes process, reaping the process groups it owns once it dies.
  3. #
  4. # Usage: sh process_supervisor.sh <parent-pgid>, spawned with start_new_session=True and stdin a
  5. # pipe whose only write end Hermes holds (hermes_cli/process_identity.py, "owner-death supervisor").
  6. # stdin carries one command per line:
  7. # register <pgid> reap this process group when Hermes dies
  8. # unregister <pgid> stop reaping it (Hermes stopped that child itself)
  9. # EOF means Hermes is gone, by any cause including SIGKILL and the OOM killer: SIGTERM every group
  10. # still registered, wait up to 3 s, SIGKILL the survivors, exit. A clean exit is the same EOF, so a
  11. # child Hermes never unregistered dies with it either way.
  12. #
  13. # POSIX sh, not Python: Hermes keeps this process resident for its whole life, and an idle sh costs
  14. # about 0.5 MB USS where an idle interpreter costs about 8 MB. It must also never import Hermes:
  15. # it runs after Hermes may already be dead.
  16. #
  17. # Malformed lines are skipped: a corrupt line must not cost the reaping guarantee for every other
  18. # group. Groups that are never a valid target (pgid <= 1, our own, Hermes's own) are refused at
  19. # registration, so a caller bug costs one unreaped child, never the Hermes process tree.
  20. # Residual risk: groups are reaped by number, so a registered group that exits on its own can have
  21. # its pgid recycled before Hermes prunes it; Hermes prunes empty groups on every registration change.
  22. trap '' INT HUP # a dying parent's SIGINT/SIGHUP must not preempt the reap: EOF is the only signal
  23. parent=$1
  24. # Every register line compares against it: a bad value would silently refuse every registration.
  25. case $parent in ''|*[!0-9]*) echo "hermes process supervisor: bad parent pgid '$parent'; refusing to run" >&2; exit 2 ;; esac
  26. # Run inside the parent's group, a killpg of the parent would take us down before we reap.
  27. kill -0 -$$ 2>/dev/null || {
  28. echo "hermes process supervisor: not a process-group leader; refusing to run" >&2
  29. exit 2
  30. }
  31. reg=' '
  32. while IFS=' ' read -r verb pg rest; do
  33. case $pg in ''|*[!0-9]*) continue ;; esac
  34. [ -z "$rest" ] && [ ${#pg} -le 10 ] || continue
  35. case $verb in
  36. register)
  37. [ "$pg" -gt 1 ] && [ "$pg" -ne $$ ] && [ "$pg" -ne "$parent" ] || continue
  38. case $reg in *" $pg "*) ;; *) reg="$reg$pg " ;; esac ;;
  39. unregister)
  40. keep=' '
  41. for g in $reg; do [ "$g" = "$pg" ] || keep="$keep$g "; done
  42. reg=$keep ;;
  43. esac
  44. done
  45. for g in $reg; do kill -TERM -"$g" 2>/dev/null; done
  46. for _ in 1 2 3; do
  47. live=''
  48. for g in $reg; do kill -0 -"$g" 2>/dev/null && live="$live $g"; done
  49. reg=$live
  50. [ -n "$reg" ] || exit 0
  51. # The default PATH: a broken PATH must not turn the TERM grace into an instant KILL.
  52. command -p sleep 1 2>/dev/null || sleep 1
  53. done
  54. for g in $reg; do kill -KILL -"$g" 2>/dev/null; done
  55. exit 0