|
| 1 | +#! /usr/bin/python3 |
| 2 | + |
| 3 | +##--------------------------------------------------------------------\\ |
| 4 | +# hackrfpy 'tests/test_deadman.py' |
| 5 | +# The OS dead-man: a HARD-killed parent (SIGKILL / TerminateProcess -- |
| 6 | +# the deaths the atexit backstop cannot see) must not orphan a |
| 7 | +# handle-mode child. Each test spawns a real intermediate parent |
| 8 | +# process that starts a protected stub handle, hard-kills that parent, |
| 9 | +# and asserts the OS ends the child: |
| 10 | +# Linux : PR_SET_PDEATHSIG delivers SIGINT -- the CLEAN interrupt, |
| 11 | +# so the child's handler runs and the marker records |
| 12 | +# "SIGINT": the dead-man IS the flush path. |
| 13 | +# Windows : the Job Object's KILL_ON_JOB_CLOSE terminates the child |
| 14 | +# tree when the parent's handles close. TerminateProcess |
| 15 | +# runs no handler, so death itself is the assertion. |
| 16 | +# macOS : skipped -- no OS primitive; the atexit backstop remains |
| 17 | +# the only net there (documented in core.py). |
| 18 | +# |
| 19 | +# |
| 20 | +# Author(s): Lauren Linkous |
| 21 | +##--------------------------------------------------------------------\\ |
| 22 | + |
| 23 | +import os |
| 24 | +import signal |
| 25 | +import subprocess |
| 26 | +import sys |
| 27 | +import time |
| 28 | + |
| 29 | +import pytest |
| 30 | + |
| 31 | +from conftest import _write_stub |
| 32 | + |
| 33 | + |
| 34 | +_SRC = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "src")) |
| 35 | + |
| 36 | + |
| 37 | +def _pid_alive(pid): |
| 38 | + if sys.platform == "win32": |
| 39 | + import ctypes |
| 40 | + _PQLI = 0x1000 # PROCESS_QUERY_LIMITED_INFORMATION |
| 41 | + _STILL_ACTIVE = 259 |
| 42 | + k32 = ctypes.windll.kernel32 |
| 43 | + h = k32.OpenProcess(_PQLI, False, pid) |
| 44 | + if not h: |
| 45 | + return False |
| 46 | + try: |
| 47 | + code = ctypes.c_ulong() |
| 48 | + if not k32.GetExitCodeProcess(h, ctypes.byref(code)): |
| 49 | + return False |
| 50 | + return code.value == _STILL_ACTIVE |
| 51 | + finally: |
| 52 | + k32.CloseHandle(h) |
| 53 | + try: |
| 54 | + os.kill(pid, 0) |
| 55 | + return True |
| 56 | + except ProcessLookupError: |
| 57 | + return False |
| 58 | + except PermissionError: |
| 59 | + return True |
| 60 | + |
| 61 | + |
| 62 | +def _wait_dead(pid, timeout=8.0): |
| 63 | + deadline = time.monotonic() + timeout |
| 64 | + while time.monotonic() < deadline: |
| 65 | + if not _pid_alive(pid): |
| 66 | + return True |
| 67 | + time.sleep(0.1) |
| 68 | + return False |
| 69 | + |
| 70 | + |
| 71 | +_PARENT_TEMPLATE = """\ |
| 72 | +import sys, time |
| 73 | +sys.path.insert(0, {src!r}) |
| 74 | +from hackrfpy import HackRF |
| 75 | +h = HackRF(tools_dir={tools!r}) |
| 76 | +p = h._run(["transfer", "-r", "x.iq"], mode="handle", kind={kind!r}) |
| 77 | +# wait until the stub says "started": its signal handlers are installed |
| 78 | +# BEFORE its stdout lines, so seeing this means an interrupt from here on |
| 79 | +# runs the handler -- without this wait, a fast parent-kill can land during |
| 80 | +# the stub's interpreter startup and die handler-less (a test race, not a |
| 81 | +# dead-man failure). Live small-write draining is what makes this wait |
| 82 | +# possible (the read1() drain fix). |
| 83 | +deadline = time.monotonic() + 10.0 |
| 84 | +while time.monotonic() < deadline: |
| 85 | + if b"started" in b"".join(p._out_chunks): |
| 86 | + break |
| 87 | + time.sleep(0.05) |
| 88 | +print(p._proc.pid, flush=True) |
| 89 | +time.sleep(120) # murdered long before this returns |
| 90 | +""" |
| 91 | + |
| 92 | + |
| 93 | +def _spawn_parent_with_child(tmp_path, marker, kind="tx"): |
| 94 | + _write_stub(str(tmp_path), "hackrf_transfer", |
| 95 | + stdout_lines=["started"], idle=True, marker=marker) |
| 96 | + script = tmp_path / "parent.py" |
| 97 | + script.write_text(_PARENT_TEMPLATE.format( |
| 98 | + src=_SRC, tools=str(tmp_path), kind=kind)) |
| 99 | + parent = subprocess.Popen([sys.executable, str(script)], |
| 100 | + stdout=subprocess.PIPE, text=True) |
| 101 | + line = parent.stdout.readline().strip() |
| 102 | + assert line.isdigit(), f"parent never reported a child pid: {line!r}" |
| 103 | + return parent, int(line) |
| 104 | + |
| 105 | + |
| 106 | +@pytest.mark.skipif(sys.platform == "darwin", |
| 107 | + reason="no OS dead-man primitive on macOS; atexit " |
| 108 | + "backstop only (see core.py)") |
| 109 | +def test_hard_killed_parent_cannot_orphan_child(tmp_path): |
| 110 | + marker = str(tmp_path / "sig") |
| 111 | + parent, child_pid = _spawn_parent_with_child(tmp_path, marker, kind="tx") |
| 112 | + assert _pid_alive(child_pid) |
| 113 | + # the death atexit cannot see: no cleanup code in the parent runs |
| 114 | + parent.kill() |
| 115 | + parent.wait(timeout=10) |
| 116 | + assert _wait_dead(child_pid), ( |
| 117 | + "child survived a hard-killed parent -- the OS dead-man did not " |
| 118 | + "fire; an orphaned transmitter would still be on the air") |
| 119 | + if sys.platform.startswith("linux"): |
| 120 | + # pdeathsig delivers the CLEAN interrupt: the handler ran and |
| 121 | + # recorded it, so even the dead-man path flushes output |
| 122 | + deadline = time.monotonic() + 3.0 |
| 123 | + while time.monotonic() < deadline and not os.path.exists(marker): |
| 124 | + time.sleep(0.05) |
| 125 | + assert os.path.exists(marker) |
| 126 | + assert open(marker).read().strip() == "SIGINT" |
| 127 | + |
| 128 | + |
| 129 | +@pytest.mark.skipif(sys.platform == "darwin", |
| 130 | + reason="no OS dead-man primitive on macOS") |
| 131 | +def test_rx_opt_out_is_not_deadman_protected(tmp_path): |
| 132 | + # backstop_rx=False is the documented fire-and-forget escape hatch; the |
| 133 | + # dead-man honors the same gate, so an opted-out RX child SURVIVES its |
| 134 | + # parent. (Only RX can opt out; TX is always protected.) |
| 135 | + _write_stub(str(tmp_path), "hackrf_transfer", |
| 136 | + stdout_lines=["started"], idle=True) |
| 137 | + script = tmp_path / "parent.py" |
| 138 | + script.write_text(_PARENT_TEMPLATE.format( |
| 139 | + src=_SRC, tools=str(tmp_path), kind="rx").replace( |
| 140 | + "p = h._run(", "h.backstop_rx = False\np = h._run(")) |
| 141 | + parent = subprocess.Popen([sys.executable, str(script)], |
| 142 | + stdout=subprocess.PIPE, text=True) |
| 143 | + child_pid = int(parent.stdout.readline().strip()) |
| 144 | + parent.kill() |
| 145 | + parent.wait(timeout=10) |
| 146 | + time.sleep(1.5) # give a wrong dead-man time to fire |
| 147 | + try: |
| 148 | + assert _pid_alive(child_pid), ( |
| 149 | + "opted-out RX child was reaped -- the dead-man ignored the " |
| 150 | + "backstop_rx gate") |
| 151 | + finally: |
| 152 | + # we deliberately orphaned it; clean up (idle ceiling would also end |
| 153 | + # it within 30 s, but be a good citizen on CI) |
| 154 | + if _pid_alive(child_pid): |
| 155 | + if sys.platform == "win32": |
| 156 | + subprocess.run(["taskkill", "/PID", str(child_pid), "/F"], |
| 157 | + capture_output=True) |
| 158 | + else: |
| 159 | + os.kill(child_pid, signal.SIGKILL) |
0 commit comments