"""Offline Linux signals: actual lifecycle/gateway, fixture runtime and provider I/O. Run in the immutable candidate with --network none. This does not construct the asset-backed speech engine or prove native pipeline shutdown. """ import asyncio import json import os from pathlib import Path import signal import subprocess import sys from threading import Event, Thread import time def child(stop_fails): import httpx import services.local_s2s.lifecycle as lifecycle from services.local_s2s.components import ComponentServer captured, logs = [], [] cancelled, client_done, stopped = Event(), Event(), Event() previous = {s: signal.getsignal(s) for s in (signal.SIGINT, signal.SIGTERM)} async def remote(request): print(json.dumps(dict(stage='inflight')), flush=True) try: await asyncio.Event().wait() finally: cancelled.set() def start(key, timeout): guard = ComponentServer(key, port=0, timeout=timeout, transport=httpx.MockTransport(remote)) thread = Thread(target=guard.serve_forever, daemon=True) thread.start() assert guard.ready.wait(2) captured.extend((guard, thread)) return guard, thread def request(): try: with httpx.Client(timeout=20, trust_env=False) as client: client.post(f'http://127.0.0.1:{captured[0].server_port}/v1/audio/speech', content=b'{}', headers={'Authorization': 'Bearer local-components'}) except httpx.HTTPError: pass finally: client_done.set() class Runtime: def __init__(self, stop): self.event = stop def start(self): Thread(target=request, daemon=True).start() def wait(self): self.event.wait() def stop(self): stopped.set() if stop_fails: raise RuntimeError('controlled runtime stop failure') def emit(value, **kwargs): logs.append(json.loads(value)) print(value, **kwargs) lifecycle.start_components = start outcome = lifecycle.run_owned(Runtime, 'offline-fixture-key', 20, emit=emit) guard, thread = captured assert outcome == int(stop_fails) assert stopped.is_set() and cancelled.wait(1) and client_done.wait(1) assert not thread.is_alive() and not guard.requests and guard.socket.fileno() == -1 assert all(signal.getsignal(s) == handler for s, handler in previous.items()) close = logs[-1] assert close['stage'] == 'close' assert close['cleanup']['runtime_stop'] == int(stop_fails) assert all(v == 0 for k, v in close['cleanup'].items() if k != 'runtime_stop') assert close['request_counts']['accepted'] == close['request_counts']['released'] == 1 assert close['request_counts']['cancelled'] == 1 assert close['request_counts']['shutdown_pending'] == 0 assert close['request_counts'].get('close_errors', 0) == 0 print(json.dumps(dict(stage='verified', stop_fails=stop_fails, signal_handlers_restored=True, fixture_io_cancelled=True)), flush=True) def main(): results = [] for sig in (signal.SIGINT, signal.SIGTERM): for stop_fails in (False, True): lines, ready = [], Event() process = subprocess.Popen([sys.executable, '-u', str(Path(__file__).resolve()), '--child', str(int(stop_fails))], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) def read(): for line in process.stdout: lines.append(line) if '"stage": "inflight"' in line: ready.set() reader = Thread(target=read, daemon=True) reader.start() try: assert ready.wait(8), ''.join(lines) began = time.monotonic() os.kill(process.pid, sig) assert process.wait(timeout=8) == 0, ''.join(lines) reader.join(1) elapsed = time.monotonic() - began events = [json.loads(line) for line in lines] assert events[-1]['stage'] == 'verified' results.append(dict(signal=sig.name, stop_fails=stop_fails, signal_to_exit_seconds=round(elapsed, 4), passed=True, observations=events)) finally: if process.poll() is None: process.kill() process.wait(timeout=3) reader.join(1) process.stdout.close() print(json.dumps(dict(cases=results, paid_calls=0, scope='real lifecycle and gateway; fixture runtime and upstream HTTP'))) if __name__ == '__main__': if len(sys.argv) > 1 and sys.argv[1] == '--child': child(bool(int(sys.argv[2]))) else: main()