LSP, the code kernel and the slash worker each had their own "release the group once nothing in it is left" (a killpg(pgid, 0) probe; the kernel's version also waited for reaps). They are now one helper, process_identity.release_process_group_once_empty(pgid, proc=, timeout=). It polls the leader so a zombie leader does not keep the group alive, waits up to `timeout` for killed members to be reaped by init, and keeps covering a group that still has a member. LSP's async close path probes once (timeout=0), as before. The slash worker used to probe exactly once right after SIGKILL. On a loaded macOS host (load 25+) init had not reaped the killed members yet, so a group that was about to be empty stayed registered (test_close_stops_the_whole_group_then_releases_it, flaky). It now waits up to 1 s, like the kernel did. The kernel survivor test raced too: under load the cell child's `trap '' TERM` could land after the teardown's SIGTERM, and the 5 s escalation could SIGKILL it. The child now signals once its trap is installed, and the test turns escalation off (the escalation is not what it pins). New tests/hermes_cli/test_process_identity_release_once_empty.py: an unreaped empty group is released, a live member keeps it covered, a late exit is waited for, and Windows never releases. Teeth: forcing the helper to always release, or to never release, turns 5 tests red in the 4 touched files. macOS: the 9 owned-child files, 78 tests, 3 runs in a row, 0 flaky.
61 lines
2.1 KiB
Python
61 lines
2.1 KiB
Python
"""process_identity.release_process_group_once_empty: release only an empty group, waiting for reaps."""
|
|
|
|
import os
|
|
import signal
|
|
import subprocess
|
|
import sys
|
|
import threading
|
|
|
|
import pytest
|
|
|
|
from hermes_cli import process_identity
|
|
|
|
pytestmark = pytest.mark.platforms("posix")
|
|
|
|
|
|
@pytest.fixture
|
|
def released(monkeypatch):
|
|
calls: list = []
|
|
monkeypatch.setattr(process_identity, "release_process_groups", lambda pgids: calls.append(list(pgids)))
|
|
return calls
|
|
|
|
|
|
def _leader(script: str) -> subprocess.Popen:
|
|
return subprocess.Popen(["sh", "-c", script], stdin=subprocess.DEVNULL,
|
|
start_new_session=True) # detached-spawn: ok — test stand-in, killed in finally
|
|
|
|
|
|
def test_an_empty_group_is_released_even_with_an_unreaped_leader(released):
|
|
leader = _leader("exit 0")
|
|
# Not wait()ed: the zombie leader keeps killpg(pgid, 0) succeeding until proc.poll() reaps it.
|
|
assert process_identity.release_process_group_once_empty(leader.pid, proc=leader, timeout=5)
|
|
assert released == [[leader.pid]]
|
|
|
|
|
|
def test_a_group_with_a_live_member_stays_covered(released):
|
|
leader = _leader("trap '' TERM; exec sleep 60")
|
|
try:
|
|
assert not process_identity.release_process_group_once_empty(leader.pid, proc=leader, timeout=0.2)
|
|
assert released == []
|
|
finally:
|
|
os.killpg(leader.pid, signal.SIGKILL) # windows-footgun: ok — posix-marked test
|
|
leader.wait(timeout=10)
|
|
|
|
|
|
def test_it_waits_for_a_member_that_exits_late(released):
|
|
leader = _leader("exec sleep 60")
|
|
threading.Timer(0.3, lambda: os.killpg(leader.pid, signal.SIGKILL)).start() # windows-footgun: ok — posix-marked test
|
|
try:
|
|
assert process_identity.release_process_group_once_empty(leader.pid, proc=leader, timeout=10)
|
|
assert released == [[leader.pid]]
|
|
finally:
|
|
if leader.poll() is None:
|
|
leader.kill()
|
|
leader.wait(timeout=10)
|
|
|
|
|
|
def test_windows_never_releases(monkeypatch, released):
|
|
monkeypatch.setattr(process_identity.os, "name", "nt")
|
|
assert not process_identity.release_process_group_once_empty(sys.maxsize, timeout=0)
|
|
assert released == []
|