Files
hermes-agent/tests/test_session_daemon_reaper.py
Teknium 3793c9fac3 test(hx013): wait for conditions, not the clock, in gateway authority tests
Replace fixed sleeps with condition waits (tests.waiting) or bounded polls:
- tests/gateway/test_managed_worker_images.py
- tests/gateway/test_multiplex_hot_serve_live.py
- tests/gateway/test_mutation_prepare_off_loop.py (the stall probe keeps its
  fixed tick, which is what it measures, but now stops on an Event; the
  warm-up sleep waits for the first tick)
- tests/gateway/test_relay_authority.py
- tests/gateway/test_retire_settles_waiters.py (teardown waits for the
  authority's tasks to finish before closing the DB)
- tests/gateway/test_session_mutation_compress.py
- tests/gateway/test_session_mutation_model.py (the contended-receipt trigger
  now blocks on an Event released once the follower submit is parked behind
  the mutation lock, instead of sleeping 0.4s)
- tests/gateway/test_terminal_commit_crash.py
- tests/gateway/test_worker_lifecycle_execution.py
- tests/test_session_daemon_reaper.py (waits until both detached children are
  visible with their HERMES_HOME in /proc)

No waivers.

(cherry picked from commit fa36f7f093437a1c7449ffab1bf74ebddfad65f6)
2026-10-11 15:00:30 -07:00

48 lines
2.1 KiB
Python

"""The session teardown stops processes left running against this run's Hermes homes.
A test that runs a real ``hermes chat -q`` child gets a grandchild ``gateway run`` in its own
session; no per-test subprocess patch sees it, and pytest's tmp cleanup removes only directories.
"""
from __future__ import annotations
import os
import subprocess
import sys
from types import SimpleNamespace
import pytest
from tests import conftest as suite_conftest
from tests.waiting import wait_until
def _detached(env_home, marker):
code = "import time\nwhile True: time.sleep(0.2)\n"
env = {**os.environ, "HERMES_HOME": str(env_home), "LIVE_REAP_MARKER": marker}
return subprocess.Popen([sys.executable, "-c", code], env=env, start_new_session=True,
stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
@pytest.mark.platforms("linux")
@pytest.mark.live_system_guard_bypass # signals only the two children this test spawned
def test_session_teardown_terminates_daemons_under_the_session_basetemp_only(tmp_path):
basetemp, elsewhere = tmp_path / "basetemp", tmp_path / "elsewhere"
(basetemp / "t0" / "hermes").mkdir(parents=True)
elsewhere.mkdir()
inside = _detached(basetemp / "t0" / "hermes", "inside")
outside = _detached(elsewhere, "outside")
try:
# Both children have exec'd with their HERMES_HOME (what the reaper reads from /proc).
wait_until(lambda: {inside.pid, outside.pid} <= set(suite_conftest._processes_with_hermes_home_under(
[basetemp, elsewhere])), timeout=10, what='both detached children started')
config = SimpleNamespace(_tmp_path_factory=SimpleNamespace(_basetemp=basetemp))
reaped = suite_conftest._reap_session_hermes_processes(config)
assert inside.pid in reaped and outside.pid not in reaped
assert inside.wait(timeout=5) is not None
assert outside.poll() is None, "a process outside this session's homes was signalled"
finally:
for proc in (inside, outside):
if proc.poll() is None:
proc.kill()
proc.wait(timeout=5)